c# - SDA.SelectCommand.ExecuteNonQuery(); System.Data.SqlClient.SqlException:''{' 附近的语法不正确。'
问题描述
这是发生错误的快照,我不知道我做错了什么:
private void Form1_Load(object sender, EventArgs e)
{
con.Open();
String query = "INSERT INTO STOCK_IN { SIN_No., PO_NO., Product_ID, Received_Date, Quantity } VALUES ('"+textBox1.Text+ "','" + textBox2.Text + "','" + textBox3.Text + "','" + textBox4.Text + "','" + textBox5.Text + "')";
SqlDataAdapter SDA = new SqlDataAdapter(query , con);
SDA.SelectCommand.ExecuteNonQuery();
con.Close();
MessageBox.Show("DATA INSERTED SUCCESSFULLY");
}
解决方案
花括号是不正确的语法。也尝试这样的事情来处理这个插入和捕获错误。在 SIN_NO 之后还有一个尾随句点,我不知道这是您的列名的一部分还是错字。
private void Form1_Load(object sender, EventArgs e)
{
String query = "INSERT INTO STOCK_IN(SIN_No., PO_NO., Product_ID, Received_Date, Quantity) VALUES (@val1, @val2, @val3, @val4, @val5)";
SqlDataAdapter sda = new SqlDataAdapter();
try
{
using (SqlCommand cmd = new SqlCommand(query, con))
{
con.Open();
cmd.Parameters.Add("@val1", SqlDbType.VarChar).Value = textBox1.Text;
//Then the same for 2, 3, 4, 5
sda = cmd.ExecuteNonQuery();
}
}
catch (SqlException ex)
{
Console.WriteLine(ex.Message);
}
finally
{
con.Close();
MessageBox.Show("DATA INSERTED SUCCESSFULLY");
}
}
推荐阅读
- reporting-services - D365 本地报告服务器 Web 门户应用程序 500 内部错误
- pandas - Pandas .any() 每列返回一个布尔值
- websocket - How to wait for variables to change before sending the next content , websocket by reactor-netty
- c# - 如何在 WEB API 的 ASP.NET MVC 控制器级别中启用 CORS?
- ios - How do I access the camera in Swift 4?
- c# - 无法获取未共享记录
- spring - Spring Boot Mongodb 在应用程序关闭时全部删除
- mysql - 从node.js获取mysql模块的“服务器关闭连接”错误
- android - Android Crashlytics 停止发送崩溃报告
- python-3.x - Elapsed time in pandas time series