收到错误“ExecuteNonQuery:Connection属性尚未初始化。”当我运行我的程序

时间:2013-05-28 21:32:53

标签: c# sql-server

我的代码出了什么问题?当我在sql server和c#之间建立连接时,它给出了这个错误“ExecuteNonQuery:Connection属性尚未初始化”。

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!!");
    }
}

}

2 个答案:

答案 0 :(得分:1)

在对SqlCommand构造函数的调用中,在SQL之后添加连接对象:

    using (SqlCommand cmd = new SqlCommand(
       "insert into CheckIn values(@TransactionId,@GuestName,@RoomType,@RoomNo,@ReservationDate,@CheckInDate,@CheckOutDate,@NoOfDays,@NoOfAdults,@NoOfChildren)",
       con))
    {
        //...
    }

答案 1 :(得分:0)

VALUES sql的INSERT INTO部分在哪里?

Side-Note:您还应该使用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();
}