private void save_Click(object sender, EventArgs e)
{
ACCOUNT.oo.Open();
string QRY = "insert into size(SIZENO,SIZE,COVERAGE,WEIGHT)
values('" + size_id.Text + "','" +txt_size.Text + "','" +txt_coverage.Text
+ "','" +txt_weight.Text + "')";
OleDbCommand ODB = new OleDbCommand(QRY,ACCOUNT.oo);
ODB.ExecuteNonQuery();
MessageBox.Show("Inserted Sucessfully..");
ACCOUNT.oo.Close();
}
回答
它看起來像您的數據庫列是用於數字數據,並且您插入的值被引用。刪除單引號可以解決這個問題。
你也應該參數化查詢,例如:
private void save_Click(object sender, EventArgs e)
{
ACCOUNT.oo.Open();
string QRY = "insert into size(SIZENO,SIZE,COVERAGE,WEIGHT) values(?,?,?,?)";
using(OleDbCommand ODB = new OleDbCommand(QRY, ACCOUNT.oo))
{
// change the OleDbType based on your actual data types
ODB.Parameters.Add("SIZENO", OleDbType.Integer).Value = int.Parse(size_id.Text);
ODB.Parameters.Add("SIZE", OleDbType.Integer).Value = int.Parse(txt_size.Text);
ODB.Parameters.Add("COVERAGE", OleDbType.Integer).Value = int.Parse(txt_coverage.Text);
ODB.Parameters.Add("WEIGHT", OleDbType.Integer).Value = int.Parse(txt_weight.Text);
ODB.ExecuteNonQuery();
}
MessageBox.Show("Inserted Sucessfully..");
ACCOUNT.oo.Close();
}
OleDbCommand不支持MS SQL所提供的* named *參數(例如'@ size_id')。但是,您可以通過其索引訪問參數。所以SQL片段將是'... values(?,?,?,?)';和C#一個'... ODB.Parameters.Add(size_id.Text); ...' –
將'IDisposable'放入'using':'使用(OleDbCommand ODB = new ...){...}' –
將SQL打印爲*單行*時*不可讀*。使用'@「...」'字符串格式使您的查詢文本變得更加多元化,並且通過更加靈活。 –
- 1. MYSQL INSERT INTO ERROR
- 2. MySQL的INSERT INTO ... SELECT#1054 ERROR
- 3. 「INSERT INTO STATEMENT」的錯誤在哪裏?
- 4. INSERT INTO VS SELECT INTO
- 5. N'INSERT INTO與INSERT INTO
- 6. 「insert into statement」中的語法錯誤
- 7. INSERT INTO PostgreSQL
- 8. SQL Insert into Select Statement with 2 Criteria
- 9. adsdb INSERT INTO
- 10. MySQLI Prepared statement will not insert into DB
- 11. insert statement error vb.net
- 12. INSERT VS INSERT INTO
- 13. 如何INSERT INTO JOIN
- 14. 通過HTML INSERT INTO
- 15. INSERT INTO - cmd.ExecuteNonQuery() - VB.Net
- 16. 使用INSERT INTO
- 17. jOOQ - INSERT INTO ... SELECT ... RETURNING
- 18. INSERT INTO Visual Studio 2010
- 19. INSERT INTO using WHERE
- 20. INSERT INTO在Ucanaccess
- 21. mysql:使用INSERT INTO ... SET時出現ERROR 1054使用INSERT INTO ... SET來填充表
- 22. INSERT INTO不PROCEDURE
- 23. INSERT INTO SELECT CASE
- 24. INSERT INTO PHP MyAdmin
- 25. INSERT INTO在Oracle
- 26. INSERT INTO SELECT疑難解答
- 27. INSERT INTO數+ 1
- 28. INSERT INTO與SELECT
- 29. INSERT INTO SELECT UPDATE
- 30. 當列INSERT INTO
如果設置一個破發點,什麼是'QRY'的實際價值?也看看參數化您的查詢。字符串連接是最糟糕的。 – sab669
SIZENO,SIZE,COVERAGE,WEIGHT所有數字?刪除已添加的單引號或至少對於數字列。 –