0

大家好,我有这个代码:

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 表客户端中显示它们

4

3 回答 3

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();
......

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

于 2013-07-10T11:07:35.667 回答
3

你还没有执行命令。

prikaz.ExecuteNonQuery();
于 2013-07-10T11:08:36.720 回答
3

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

spojeni.Open();
prikaz.ExecuteNonQuery();
于 2013-07-10T11:11:43.423 回答