我试着写一些数字表在SQL
使用C#
中Visual Studio
。运行此代码时,我将显示一条catch语句:
连接属性尚未初始化。
我在想我的第五行代码可能有问题吗?我对C#
和非常陌生SQL
,并且在识别连接对象时出现错误,但是我能够单击一些下拉菜单并为其分配一些东西来消除该错误。
sSql = "INSERT INTO FAI.emc_dept (DepartmentID, Department) " +
"VALUES (" +
"@DepartmentID," +
"@Department";
SqlCommand ins = new SqlCommand(sSql, FAIDataSet.Connection);
ins.Parameters.AddWithValue("@DepartmentID", "abc");
ins.Parameters.AddWithValue("@Department", "def")
ins.ExecuteNonQuery();
该答案基于以下评论:
cnSqlSvr =新的SqlConnection(sSql);
但是sSql
不是连接字符串。替换为:
cnSqlSvr = new SqlConnection("your connection string");
和这个:
SqlCommand ins = new SqlCommand(sSql, FAIDataSet.Connection);
和:
SqlCommand ins = new SqlCommand(sSql, cnSqlSvr);
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句