簡體   English   中英

出現錯誤“ ExecuteNonQuery:連接屬性尚未初始化。”我運行程序

[英]getting error “ ExecuteNonQuery: Connection property has not been initialized.” wen i run my program

我的代碼有什么問題? 當我在sql服務器和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!!");
    }
}

}

在對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 -statement以確保即使在發生異常的情況下也關閉連接:

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();
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM