Tus*_*har 2 c# sql-server asp.net
我必须使用insert query在表中插入值...存储在数据库中的表有3列:1.Date(DateTime)2.SensorValue(Float)3.差异(Float)现在每列的值来自datagridview .....这是插入的按钮代码
con.ConnectionString = "Data Source=.\\SQLEXPRESS;AttachDbFilename=C:\\dbsave.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True";
if (con.State == ConnectionState.Closed)
{
con.Open();
}
for (Int32 i = 0; i < dataGridView1.Rows.Count-1; i++)
{
String query1 =
"INSERT INTO " + tbName +
" ( Date, SensorValue, Difference) " + "VALUES (" +
dataGridView1.Rows[i].Cells[0].Value + "," +
dataGridView1.Rows[i].Cells[1].Value + "," +
dataGridView1.Rows[i].Cells[2].Value + ")";
SqlCommand cmd1 = new SqlCommand(query1, con);
cmd1.ExecuteNonQuery();
}
con.Close();
MessageBox.Show("The table has been saved");
Run Code Online (Sandbox Code Playgroud)
在执行查询时出错... ....日期列中的第一个条目是值:12/05/2012 14:32:00 ....所以基本上sql不接受放置的冒号有14 ....我怎么能解决这个问题?请帮忙
更新您的查询并添加撇号:
String query1 =
"INSERT INTO " + tbName +
" ( Date, SensorValue, Difference) " + "VALUES ('" +
dataGridView1.Rows[i].Cells[0].Value + "'," +
dataGridView1.Rows[i].Cells[1].Value + "," +
dataGridView1.Rows[i].Cells[2].Value + ")";
SqlCommand cmd1 = new SqlCommand(query1, con);
Run Code Online (Sandbox Code Playgroud)
但我同意Liath,参数更安全.
| 归档时间: |
|
| 查看次数: |
1434 次 |
| 最近记录: |