[英]ExecuteNonQuery not working in C#
I am building a database using Visual Studio 2008 c# and when I'm a trying to insert a new record into my database it appears that ExecuteNonQuery
has not initialized. 我正在使用Visual Studio 2008 c#构建数据库,当我试图在我的数据库中插入新记录时,似乎ExecuteNonQuery
尚未初始化。 I copy my code, hope anyone can help me in this because I am new. 我复制我的代码,希望任何人都能帮助我,因为我是新手。
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;
You have numerous problems in your code: 您的代码中存在许多问题:
insert into
statement requires a target datatable not the name of the MDF file 第一: insert into
语句需要一个目标数据表而不是MDF文件的名称 Fourth: You need to associate the connection to the command (Easily done at the SqlCommand constructor ) 第四:您需要将连接与命令相关联(在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"); }
EDIT The information provided by the link added by @RemusRusanu below is very important. 编辑由@RemusRusanu添加的链接提供的信息非常重要。 The use of AddWithValue, whilst handy, could hinder the performance of your query. 使用AddWithValue虽然方便,但可能会妨碍查询的性能。 The correct approach should be the usage of a proper defined SqlParameter with both explicit datatype and parameter size. 正确的方法应该是使用具有显式数据类型和参数大小的正确定义的SqlParameter。 As an example 举个例子
SqlParameter p = new SqlParameter("@cod", SqlDbType.NVarChar, 255).Value = comboBox1.Text;
cmd.Parameters.Add(p);
But, of course, this requires that you check the exact datatype and size of your columns. 但是,当然,这需要您检查列的确切数据类型和大小。
You did not initialize your SqlCommand
with a connection. 您没有使用连接初始化SqlCommand
。 Also, you should really enclose everything here with using
. 此外,您应该using
所有内容。 And consider using parametarized commands to avoid SQL Injection. 并考虑使用参数化命令来避免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");
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.