SqlCommand INSERT INTO查询不执行

时间:2013-07-10 11:06:08

标签: c# sql insert sqlcommand executereader

大家好,我有这个代码:

SqlCommand scom = new SqlCommand(
                                "INSERT INTO klient(name,surname) 
                                values(@kname,@ksurname)", 
                                conn);

scom.Parameters.AddWithValue("@kname", kname.Text);
scom.Parameters.AddWithValue("@ksurname", ksurname.Text);
conn.Open();
DataTable dt = new DataTable();
SqlDataAdapter SDA = new SqlDataAdapter("SELECT * FROM klient", spojeni);
SDA.Fill(dt);
conn.Close();

它应该插入来自文本框的数据:kname,ksurname,但它关闭表单而不在MS SQL表中显示它们klient

3 个答案:

答案 0 :(得分:6)

缺少ExecuteNonQuery调用

SqlCommand prikaz = new SqlCommand("INSERT INTO klient(name,surname) values(@kname,@ksurname)", spojeni);

prikaz.Parameters.AddWithValue("@kname", kname.Text);
prikaz.Parameters.AddWithValue("@ksurname", ksurname.Text);
spojeni.Open();
prikaz.ExecuteNonQuery();
......

应该执行一个命令来更新数据库......

答案 1 :(得分:3)

您尚未执行该命令。

prikaz.ExecuteNonQuery();

答案 2 :(得分:3)

上述问题是由于缺少executiontenonquery()语句,在代码中添加此语句

spojeni.Open();
prikaz.ExecuteNonQuery();