我尝试从 Windows 窗体应用程序更新我的数据库。信息显示在数据 GridView 中。当我点击更新按钮时,出现异常:
Connection string property has not been initialized
和
cmdBuilder.DataAdapter.UpdateCommand = null
我的代码:
public static string connectionString = @"Data Source=(local)\SQLEXPRESS;Initial Catalog=Clothing_DB;Integrated Security=True;Connect Timeout=15;Encrypt=False;TrustServerCertificate=True;ApplicationIntent=ReadWrite;MultiSubnetFailover=False";
public void setNewSizes()
{
using (SqlConnection conn = new SqlConnection(connectionString))
{
try
{
conn.Open();
adapterSize.UpdateCommand = cmdBuilder.GetUpdateCommand(); //error here
adapterSize.Update(recivedData.Tables[0]);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, ex.Source);
}
}
}
如何修复它?
最佳答案
您不应释放在 SqlDataAdapter
创建期间创建的 SqlConnection
对象。你不应该使用新的连接,也不需要打开和关闭它,SqlDataAdapter.Update(yourTable)
会为你做这件事。
关于c# - 连接字符串属性尚未初始化 SqlCommandBuilder,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43470424/