我的代码有什么问题?当我在 sql server management studio 和 c# 之间建立连接时,它给了我这个错误“ExecuteNonQuery: Connection property has not been initialized”。
private void button1_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection();
con.ConnectionString= "Database= hotel; server= Sherissa\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!!");
}