San*_*nad 0 c# sql sqlconnection visual-studio
在我的程序中,我有两个文本输入字段(在窗体中)和一个按钮,用于将这些值添加/保存到数据库表中.问题是,一旦我点击按钮,它就不会将输入插入数据库,而是显示我在下面作为图像附加的错误.
我的计划有什么问题?
我的工作代码:
public partial class Form1 : Form
{
//original Connection string is exactly the following:
//Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename="C:\Users\Sanad Al Nahaj\Documents\thesaurus.mdf";Integrated Security=True;Connect Timeout=30
SqlConnection conn = new SqlConnection(@"Data Source = (LocalDB)\MSSQLLocalDB; AttachDbFilename=C:\Users\Sanad Al Nahaj\Documents\thesaurus.mdf;Integrated Security = True; Connect Timeout = 30");
public Form1()
{
InitializeComponent();
}
//Save button
private void button1_Click(object sender, EventArgs e)
{
conn.Open();//error pops up here after clicking the button
SqlCommand command = conn.CreateCommand();
command.CommandType = CommandType.Text;
command.CommandText = "insert into Table values('"+wordbox.Text+"','"+synonymbox.Text+"')";
command.ExecuteNonQuery();
conn.Close();
MessageBox.Show("Word and Synonym added!");
}
private void display_Click(object sender, EventArgs e)
{
//implement
}
}
Run Code Online (Sandbox Code Playgroud)
错误:
数据库看起来像:
更新:
我对Using模式的修改(参考CDove的答案):
var command = new SqlCommand();
using (command = new SqlCommand(
"insert into......)
))
Run Code Online (Sandbox Code Playgroud)
你需要做四件事.首先,解决这个问题:
"insert into [Table] values('"+wordbox.Text+"','"+synonymbox.Text+"')"
Run Code Online (Sandbox Code Playgroud)
在Microsoft SQL中,如果我没记错的话,values()插入语法需要首先明确声明列.此外,"表"是一个保留字,因此您需要将其放在括号中以将该字用作表名.将来,请避免在表模式中使用保留字.
"insert into [Table] (word, synonym) values ('"+wordbox.Text+"','"+synonymbox.Text+"')"
Run Code Online (Sandbox Code Playgroud)
其次,不要使用字符串连接来构建查询.改为创建参数.
"insert into [Table] (word, synonym) values (@word,@syn)"
Run Code Online (Sandbox Code Playgroud)
然后
command.Parameters.AddWithValue("@word", wordbox.Text);
command.Parameters.AddWithValue("@syn", synonymbox.Text);
command.ExecuteNonQuery();
Run Code Online (Sandbox Code Playgroud)
第三,不要缓存你的连接.这就是代码顶部的作用,为您提供一个微管理连接:
SqlConnection conn = new SqlConnection(@"Data Source = (LocalDB)\MSSQLLocalDB; AttachDbFilename=C:\Users\Sanad Al Nahaj\Documents\thesaurus.mdf;Integrated Security = True; Connect Timeout = 30");
Run Code Online (Sandbox Code Playgroud)
理想情况下,您从web.config或app.config中读取此内容,我们将使用您的硬编码字符串滚动; 留下它只是一个字符串.
string conn = @"Data Source = (LocalDB)\MSSQLLocalDB; AttachDbFilename=C:\Users\Sanad Al Nahaj\Documents\thesaurus.mdf;Integrated Security = True; Connect Timeout = 30";
Run Code Online (Sandbox Code Playgroud)
最后,使用using模式.这不仅是不那么臭的代码,而且还包括隐式.Close()和.Dispose()隐式try-finally方式.
private void button1_Click(object sender, EventArgs e)
{
using(var command = new SqlCommand(
"insert into [Table] (word, synonym) values (@word,@syn)",
new SqlConnection(conn)
))
{
command.Connection.Open();//Since we aren't reopening an old connection, errors are less likely.
command.CommandType = CommandType.Text;
command.Parameters.AddWithValue("@word", wordbox.Text);
command.Parameters.AddWithValue("@syn", synonymbox.Text);
if(command.ExecuteNonQuery() > 0 )
MessageBox.Show("Word and Synonym added!");
}
}
Run Code Online (Sandbox Code Playgroud)
请注意,我检查的值ExecuteNonQuery.那是因为这个函数返回受影响的行数; 如果计数为0,则不添加单词和同义词.
注意:这完全取决于我的午餐时间,所以请亲自测试,看看它是如何为您服务的.