0
使用,ExecuteNonQuery:连接属性尚未初始化。我坚持
的Visual Studio 2012 C# WPF 的SQL Server Compact 4.0
我有我的代码在这里。当我提出我的类别名称就说明
“的ExecuteNonQuery:Connection属性尚未初始化”
plz帮助我。我正在尝试将数据从文本框添加到数据库。
private void btnCategoryAdd_Click(object sender, RoutedEventArgs e)
{
con.Open();
SqlCeCommand com = new SqlCeCommand("INSERT INTO Category_Master(CategoryName) VALUES(@CategoryName)");
com.Parameters.AddWithValue("@CategoryName", tbCategoryName.Text);
try
{
int affectedRows = com.ExecuteNonQuery();
if (affectedRows > 0)
{
System.Windows.Forms.MessageBox.Show("Insert Success !", System.Windows.Forms.Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Information);
tbCategoryName.Text = "";
}
else
{
System.Windows.Forms.MessageBox.Show("Insert Failed !", System.Windows.Forms.Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
catch (Exception ex)
{
System.Windows.Forms.MessageBox.Show(ex.Message, System.Windows.Forms.Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
con.Close();
}
嗨。谢谢。我认为它的工作。但出现另一个错误。列不能包含空值。是因为我的数据库是空的。如何克服这一点。我也有一个名为categoryID的列。这不是没有,主要的和独特的。 – Kamal
Ys。有效。我忘了为唯一ID添加自动增量。 – Kamal
@KaMaLMoHaN很高兴它帮助你.. –