在SQL Server中执行查询? [英] Executing a Query in SQL Server?
本文介绍了在SQL Server中执行查询?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
尝试
{
string s = INSERT INTO [学生记录](姓名,年龄,班级,性别,监护人姓名,联系方式,地址,电子邮件地址)价值(@姓名,@年龄,@班级,@性别,@ Guardian姓名,@ Contact.No, @地址,@电子邮件地址)跨度>;
SqlConnection con = new SqlConnection(cs);
SqlCommand cmd = new SqlCommand(s,con);
cmd.Connection = con;
cmd.Parameters.AddWithValue( @ Name,textBox1。文本跨度>);
cmd.Parameters.AddWithValue( @ Age,textBox2。文本跨度>);
cmd.Parameters.AddWithValue( @ Class,textBox3。文本跨度>);
cmd.Parameters.AddWithValue( @ Gender,textBox4。文本跨度>);
cmd.Parameters.AddWithValue( @ Guardian's Name,textBox5。文字);
cmd.Parameters.AddWithValue( @ Contact.No,textBox6。 Text );
cmd.Parameters.AddWithValue( @ Address,textBox7。文本跨度>);
cmd.Parameters.AddWithValue( @ Email-Address,textBox8。文本跨度>);
con。 Open ();
cmd.ExecuteNonQuery();
con。关闭();
MessageBox.Show( 已保存!);
}
我的查询无法成功执行。请帮助。:confused:
解决方案
好的,问题看起来好像可能与你分配给事物的名字有关。尝试将其更改为:string s = INSERT INTO [学生记录](姓名) ,年龄,班级,性别,[监护人姓名],[联系人.No],地址,电子邮件地址)价值(@姓名,@年龄,@班级,@性别,@ Guardian,@联系人,@地址,@ Email-地址)跨度>;
SqlConnection con = new SqlConnection(cs);
SqlCommand cmd = new SqlCommand(s,con);
cmd.Connection = con;
cmd.Parameters.AddWithValue( @ Name,textBox1。文本跨度>);
cmd.Parameters.AddWithValue( @ Age,textBox2。文本跨度>);
cmd.Parameters.AddWithValue( @ Class,textBox3。文本跨度>);
cmd.Parameters.AddWithValue( @ Gender,textBox4。文本跨度>);
cmd.Parameters.AddWithValue( @ Guardian,textBox5。文本跨度>);
cmd.Parameters.AddWithValue( @ Contact,textBox6。文本跨度>);
cmd.Parameters.AddWithValue( @ Address,textBox7。文本跨度>);
cmd.Parameters.AddWithValue( @ Email-Address,textBox8。文本跨度>);
try
{
string s = "INSERT INTO [Students Records] (Name,Age,Class,Gender,Guardian's Name,Contact.No,Address,Email-Address) VALUES(@Name,@Age,@Class,@Gender,@Guardian's Name,@Contact.No,@Address,@Email-Address)";
SqlConnection con = new SqlConnection(cs);
SqlCommand cmd = new SqlCommand(s,con);
cmd.Connection = con;
cmd.Parameters.AddWithValue("@Name", textBox1.Text);
cmd.Parameters.AddWithValue("@Age", textBox2.Text);
cmd.Parameters.AddWithValue("@Class", textBox3.Text);
cmd.Parameters.AddWithValue("@Gender", textBox4.Text);
cmd.Parameters.AddWithValue("@Guardian's Name", textBox5.Text);
cmd.Parameters.AddWithValue("@Contact.No", textBox6.Text);
cmd.Parameters.AddWithValue("@Address", textBox7.Text);
cmd.Parameters.AddWithValue("@Email-Address", textBox8.Text);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
MessageBox.Show("Saved!");
}
My query fails to execute successfully. Please help.:confused:
解决方案
Okay, the issue looks as though it's probably to do with the names that you have allocated to things. Try changing it to this:string s = "INSERT INTO [Students Records] (Name,Age,Class,Gender,[Guardian's Name],[Contact.No],Address,Email-Address) VALUES(@Name,@Age,@Class,@Gender,@Guardian,@Contact,@Address,@Email-Address)"; SqlConnection con = new SqlConnection(cs); SqlCommand cmd = new SqlCommand(s,con); cmd.Connection = con; cmd.Parameters.AddWithValue("@Name", textBox1.Text); cmd.Parameters.AddWithValue("@Age", textBox2.Text); cmd.Parameters.AddWithValue("@Class", textBox3.Text); cmd.Parameters.AddWithValue("@Gender", textBox4.Text); cmd.Parameters.AddWithValue("@Guardian", textBox5.Text); cmd.Parameters.AddWithValue("@Contact", textBox6.Text); cmd.Parameters.AddWithValue("@Address", textBox7.Text); cmd.Parameters.AddWithValue("@Email-Address", textBox8.Text);
这篇关于在SQL Server中执行查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文