设置SQL Server和C#之间的连接时出错 [英] Error in setting up the connection between SQL Server and C#
本文介绍了设置SQL Server和C#之间的连接时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的代码出了什么问题?当我在sql server management studio和c#之间建立连接时,它给出了我的错误ExecuteNonQuery:Connection属性尚未初始化。
What is wrong with my code? When Im setting up the connection between sql server management studio and c#, it gives me this error ExecuteNonQuery: Connection property has not been initialized.
private void button1_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection();
con.ConnectionString= "Database= hotel; server= Roger\SQLEXPRESS";
con.Open();
SqlCommand cmd = new SqlCommand("insert into CheckIn (@TransactionId,@GuestName,@RoomType,@RoomNo,@ReservationDate,@CheckInDate,@CheckOutDate," + "@NoOfDays,@NoOfAdults,@NoOfChildren)");
cmd.Parameters.AddWithValue("@TransactionId",textBox1.Text);
cmd.Parameters.AddWithValue("@GuestName", textBox2.Text);
cmd.Parameters.AddWithValue("@RoomType", textBox3.Text);
cmd.Parameters.AddWithValue("@RoomNo", textBox4.Text);
cmd.Parameters.AddWithValue("@ReservationDate", textBox5.Text);
cmd.Parameters.AddWithValue("@CheckInDate", textBox6.Text);
cmd.Parameters.AddWithValue("@CheckOutDate", textBox7.Text);
cmd.Parameters.AddWithValue("@NoOfDays", textBox8.Text);
cmd.Parameters.AddWithValue("@NoOfAdults", textBox9.Text);
cmd.Parameters.AddWithValue("@NoOfChildren", textBox10.Text);
cmd.ExecuteNonQuery();
con.Close();
MessageBox.Show("DATA ADDED SUCCESSFULLY!!");
推荐答案
尝试使用此格式进行sql连接,填写在您的数据库信息的空白处:
Try this format for your sql connection, fill in the blanks with your DB information:
SqlConnection con = new SqlConnection(@"Data Source=''your pc name\SQLEXPRESS''; UserID=sa;Password=****;Initial Catalog=''MyDatabase''; IntegratedSecurity=True;");
这篇关于设置SQL Server和C#之间的连接时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文