我正在使用 Visual Studio 2008 c# 构建数据库,当我尝试将新记录插入数据库时,
ExecuteNonQuery
似乎尚未初始化。我复制我的代码,希望任何人都可以帮助我,因为我是新人。
private void button1_Click(object sender, EventArgs e)
{
SqlConnection cn = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\Usuario\Documents\Visual Studio 2010\Projects\Nova\Nova\Database1.mdf;Integrated Security=True;User Instance=True");
SqlCommand cmd = new SqlCommand();
cn.Open();
cmd.CommandText = "insert into Database1.mdf(Codigo,Nombre,Cantidad,Tipo) values('"+comboBox1.Text+"','"+textBox3.Text+"','"+textBox1.Text+"','"+comboBox2.Text+"')";
cmd.ExecuteNonQuery();
cmd.Clone();
cn.Close();
MessageBox.Show("Acabas de agregar un producto");
}
您尚未设置与命令的连接:
cmd.Connection = cn;
您的代码中有很多问题:
insert into
语句需要目标数据表而不是名称
MDF 文件第四:您需要将连接与命令关联起来(很容易 在 SqlCommand 构造函数)
完成using(SqlConnection cn = new SqlConnection(.......))
using(SqlCommand cmd = new SqlCommand("insert into table_name(Codigo,Nombre,Cantidad,Tipo)" +
"values (@cod, @nom,@can,@tipo)", con))
{
cn.Open();
cmd.Parameters.AddWithValue("@cod", comboBox1.Text);
cmd.Parameters.AddWithValue("@nom", textBox3.Text);
cmd.Parameters.AddWithValue("@can", textBox1.Text);
cmd.Parameters.AddWithValue("@tipo", comboBox2.Text);
cmd.ExecuteNonQuery();
MessageBox.Show("Acabas de agregar un producto");
}
编辑 下面由@RemusRusanu 添加的链接提供的信息非常重要。使用 AddWithValue 虽然方便,但可能会影响查询的性能。正确的方法应该是使用正确定义的具有显式数据类型和参数大小的 SqlParameter。 举个例子
SqlParameter p = new SqlParameter("@cod", SqlDbType.NVarChar, 255).Value = comboBox1.Text;
cmd.Parameters.Add(p);
但是,当然,这需要您检查列的确切数据类型和大小。
您没有通过连接初始化您的
SqlCommand
。另外,你真的应该用 using
将这里的所有内容括起来。并考虑使用参数化命令来避免 SQL 注入。
private void button1_Click(object sender, EventArgs e)
{
using (SqlConnection cn = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\Usuario\Documents\Visual Studio 2010\Projects\Nova\Nova\Database1.mdf;Integrated Security=True;User Instance=True"))
{
using (SqlCommand cmd = new SqlCommand())
{
cmd.CommandText = "insert into databaseTableName (Codigo,Nombre,Cantidad,Tipo) values (@Codigo, @Nombre, @Cantidad, @Tipo)";
cmd.Parameters.AddWithValue("@Codigo", comboBox1.Text);
cmd.Parameters.AddWithValue("@Nombre", textBox3.Text);
cmd.Parameters.AddWithValue("@Cantidad", textBox1.Text);
cmd.Parameters.AddWithValue("@Tipo", comboBox2.Text);
cmd.Connection = cn; //this was where the error originated in the first place.
cn.Open();
cmd.ExecuteNonQuery();
MessageBox.Show("Acabas de agregar un producto");
}
}
}
private void button1_Click(object sender, EventArgs e)
{
string connectionString = "Data Source=.;SQLEXPRESS;AttachDbFilename=C:\Users\Usuario\Documents\Visual Studio 2010\Projects\Nova\Nova\Database1.mdf;Integrated Security=True;User Instance=True")";
SqlConnection cn = new SqlConnection(connectionString);
try
{
string query = "insert into Database1.mdf(Codigo,Nombre,Cantidad,Tipo) values('" + comboBox1.Text + "','" + textBox3.Text + "','" + textBox1.Text + "','" + comboBox2.Text + "')";
SqlCommand cmd = new SqlCommand(query, cn);
cn.Open();
cmd.ExecuteNonQuery();
cmd.Clone();
MessageBox.Show("Acabas de agregar un producto");
}
catch
{
return "Error";
}
finally
{
cn.Close();
}
}