在vs2005中向SQL数据库插入数据

部分代码如下,语法没错误,可以运行,label显示了“数据插入”,但数据库里却没有插入到数据,求高手指点 protected void Button3_Click(object sender, EventArgs e) { string s3 = "insert into car_info (car_type,driver,company,weight,state) values (" + TextBox4.Text + "," + TextBox5.Text + "," + TextBox6.Text + "," + TextBox7.Text + "," + TextBox8.Text + ")"; SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString); con.Open(); SqlCommand cmd2 = new SqlCommand(s3, con); Label1.Text = "数据已插入"; con.Close(); }

你压根就没执行插入工作,把sqlcommand实例化后就直接把label1的text显示为数据已插入,当然数据库没内容了,改为: con.Open(); SqlCommand cmd2 = new SqlCommand(s3, con); if (cmd2.ExecuteNonQuery() > 0) Label1.Text = "数据已插入"; else Label1.Text = "插入失败"; con.Close();

满意请采纳
温馨提示:答案为网友推荐,仅供参考
相似回答