获取错误"ExecuteNonQuery:连接属性尚未初始化."当我运行程序时

本文关键字:quot 初始化 运行 程序 属性 ExecuteNonQuery 取错误 连接 获取 | 更新日期: 2023-09-27 18:15:41

我的代码有什么问题?当我设置sql server和c#之间的连接时,它给我这个错误"ExecuteNonQuery:连接属性尚未初始化。"

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Essencia
{
    public partial class NewReservation : Form
    {
        public NewReservation()
    {
        InitializeComponent();
    }
    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 values(@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.P`enter code here`arameters.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!!");
    }
}

}

获取错误"ExecuteNonQuery:连接属性尚未初始化."当我运行程序时

在调用SqlCommand构造函数时,在SQL:

后面添加连接对象
    using (SqlCommand cmd = new SqlCommand(
       "insert into CheckIn values(@TransactionId,@GuestName,@RoomType,@RoomNo,@ReservationDate,@CheckInDate,@CheckOutDate,@NoOfDays,@NoOfAdults,@NoOfChildren)",
       con))
    {
        //...
    }

哪里是INSERT INTO sql的VALUES部分?

旁注:您还应该使用using -语句来确保即使在出现异常的情况下也关闭连接:

string sql = @"INSERT INTO checkin 
                       (transactionid, 
                        guestname, 
                        roomtype, 
                        roomno, 
                        reservationdate, 
                        checkindate, 
                        checkoutdate, 
                        noofdays, 
                        noofadults, 
                        noofchildren) 
                VALUES(@TransactionId, 
                       @GuestName, 
                       @RoomType, 
                       @RoomNo, 
                       @ReservationDate, 
                       @CheckInDate, 
                       @CheckOutDate, 
                       @NoOfDays, 
                       @NoOfAdults, 
                       @NoOfChildren)";
using(var con = new SqlConnection(@"Database= hotel; server= Roger'SQLEXPRESS"))
using(var cmd = new SqlCommand(sql , con ))
{
    cmd.Parameters.AddWithValue("@TransactionId",textBox1.Text);
    // other parameters as well
    con.Open();
    cmd.ExecuteNonQuery();
}