DBConnection objconn = new DBConnection();objconn.connection(); //calling connection System.Data.SqlClient.SqlCommand com = new System.Data.SqlClient.SqlCommand(tbl_srsdata, objconn.con);com.Comma...
DBConnection objconn = new DBConnection();
objconn.connection(); //calling connection
System.Data.SqlClient.SqlCommand com = new System.Data.SqlClient.SqlCommand("tbl_srsdata", objconn.con);
com.CommandType = System.Data.CommandType.Text;
var rows = Table1.Items;
TextBlock field;
TextBlock type;
TextBlock datatype;
TextBlock len;
TextBlock buis;
int id;
id = Redirect.identity;
MessageBox.Show(id.ToString());
for (int i = 0; i < rows.Count; i++)
{
DataGridRow row = (DataGridRow)Table1.ItemContainerGenerator.ContainerFromIndex(i);
for (int j = 0; j < Table1.Columns.Count; j++)
{
field = Table1.Columns[j].GetCellContent(row) as TextBlock;
j++;
type = Table1.Columns[j].GetCellContent(row) as TextBlock;
j++;
datatype = Table1.Columns[j].GetCellContent(row) as TextBlock;
j++;
len = Table1.Columns[j].GetCellContent(row) as TextBlock;
j++;
buis = Table1.Columns[j].GetCellContent(row) as TextBlock;
j++;
com.CommandText = "insert into tbl_srsdata(id,field,[type_of_control],datatype,length,other_buisness_rules) values( " + id + ", " + field.Text + "," + type.Text + "," + datatype.Text + "," + len.Text + "," + buis.Text + ");";
com.ExecuteNonQuery();
}
}
我在最后一行收到错误.错误是:
System.Data.dll中出现“System.Data.SqlClient.SqlException”类型的异常,但未在用户代码中处理.
附加信息:关键字’of’附近的语法不正确.
任何人都可以帮我解决这个问题.
解决方法:
将TextBox中的文本集成到Query中是个坏主意.
您必须添加文本作为参数:
com.CommandText = "insert into tbl_srsdata(id,field,[type_of_control],datatype,length,other_buisness_rules) values(@id,@field,@type,@datatype,@len,@buis)";
com.Parameters.AddWithValue("@id", id);
com.Parameters.AddWithValue("@field", field.Text);
com.Parameters.AddWithValue("@type", type.Text);
com.Parameters.AddWithValue("@datatype", datatype.Text);
com.Parameters.AddWithValue("@len", len.Text);
com.Parameters.AddWithValue("@buis", buis.Text);
com.ExecuteNonQuery();
将文本直接集成到Query可能会导致错误和安全问题.
我不知道你的程序的逻辑是什么.但我建议(这可能是错的)这样写:
if (!Table1.Items.Any())
return;
string strInsertCmd =
"insert into tbl_srsdata(id,field,[type_of_control],datatype,length,other_buisness_rules)"+
"values(@id,@field,@type,@datatype,@len,@buis)";
using (var com = new System.Data.SqlClient.SqlCommand(strInsertCmd, objconn.con))
{
var rows = Table1.Items;
int id;
id = Redirect.identity;
MessageBox.Show(id.ToString());
for (int i = 0; i < rows.Count; i++)
{
DataGridRow row = (DataGridRow)Table1.ItemContainerGenerator.ContainerFromIndex(i);
var field = Table1.Columns[0].GetCellContent(row) as TextBlock;
var type = Table1.Columns[1].GetCellContent(row) as TextBlock;
var datatype = Table1.Columns[2].GetCellContent(row) as TextBlock;
var len = Table1.Columns[3].GetCellContent(row) as TextBlock;
var buis = Table1.Columns[4].GetCellContent(row) as TextBlock;
com.Parameters.Clear();
com.Parameters.AddWithValue("@id", id);
com.Parameters.AddWithValue("@field", field.Text);
com.Parameters.AddWithValue("@type", type.Text);
com.Parameters.AddWithValue("@datatype", datatype.Text);
com.Parameters.AddWithValue("@len", len.Text);
com.Parameters.AddWithValue("@buis", buis.Text);
com.ExecuteNonQuery();
}
}
沃梦达教程
本文标题为:c# – 我试图在sql server中插入一个表但是它给出了一个错误,即关键字’of’附近的语法不正确
基础教程推荐
猜你喜欢
- 如何在C#/ Windows窗体中翻转/旋转标签? 2023-09-19
- C# 实现PPT 每一页转成图片过程解析 2023-02-02
- Unity延时执行的多种方法小结 2023-04-22
- VS2010下生成dll的方法 2022-12-06
- Winform使用FTP实现自动更新 2023-06-27
- C# 在PDF中添加墨迹注释Ink Annotation的步骤详解 2023-05-22
- C#在Winform开发中使用Grid++报表 2023-01-16
- 详解Unity使用ParticleSystem粒子系统模拟药水在血管中流动(粒子碰撞) 2023-04-15
- .net Core 中DateTime在Linux Docker中与Windows时间不一致 2023-09-26
- c#如何使用 XML 文档功能 2023-03-14