C# SqlCommand INSERT INTO 查询不执行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17569051/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
SqlCommand INSERT INTO query does not execute
提问by Marek
Hello guys I have got this code:
大家好,我有这个代码:
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();
It should insert data from textboxes: kname, ksurname, but it closes the form without showing them in MS SQL table klient
它应该从文本框中插入数据:kname、ksurname,但它关闭表单而不在 MS SQL 表 klient 中显示它们
采纳答案by Steve
Missing the ExecuteNonQuery call
缺少 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();
......
A command should be executed to update the database...
应该执行一个命令来更新数据库...
回答by Darren
You haven't executed the command.
你还没有执行命令。
prikaz.ExecuteNonQuery();
回答by mck
The above stated problem is due to the missing executenonquery() statement, add this statement in your code
上述问题是由于缺少 executenonquery() 语句,在您的代码中添加此语句
spojeni.Open();
prikaz.ExecuteNonQuery();

