0

我的代码有什么问题?当我在 sql server 和 c# 之间建立连接时,它给了我这个错误“ExecuteNonQuery: Connection property has not been initialized”。

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

}

4

2 回答 2

1

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

    using (SqlCommand cmd = new SqlCommand(
       "insert into CheckIn values(@TransactionId,@GuestName,@RoomType,@RoomNo,@ReservationDate,@CheckInDate,@CheckOutDate,@NoOfDays,@NoOfAdults,@NoOfChildren)",
       con))
    {
        //...
    }
于 2013-05-28T21:50:48.240 回答
0

sql的VALUES部分在哪里?INSERT INTO

旁注:您还应该使用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();
}
于 2013-05-28T21:40:01.660 回答