namespace Hotel
{
public partial class Billing : Form
{
SqlConnection con = new SqlConnection();
SqlDataAdapter da;
SqlCommand cmd = new SqlCommand();
DataTable dt = new DataTable();
public Billing()
{
InitializeComponent();
}
private void Billing_Load(object sender, EventArgs e)
{
con.ConnectionString = "Data Source=.\\SQLEXPRESS;AttachDbFilename=D:\\Projects\\c# assignments\\Hotel Manager\\Hotel\\database\\master.mdf;Integrated Security=True;User Instance=True";
//loadData();
}
private void button1_Click(object sender, EventArgs e)
{
con.Open();
int rno = Int32.Parse(txtRoom.Text);
cmd.CommandText = "SELECT SUM(ItemRate) FROM logs WHERE RoomNo=" + rno +"";
int amt = (int)cmd.ExecuteScalar(); //arror is at this part
//ExecuteScalar: Connection property has not been initialized.
cmd.CommandText = "INSERT INTO bill VALUES('" + txtBillNo.Text.ToString() + "','" + txtRoom.Text.ToString() + "','" + amt.ToString() + "')";
con.Close();
txtBillNo.Text = "";
txtRoom.Text = "";
BillView bv = new BillView();
bv.ShowDialog();
}
}
}
请帮我解决这个错误我无法将SQL查询结果存储到变量???
答案 0 :(得分:10)
using-statement
进行连接(以及实施IDisposable
的所有其他内容)。 Dispose也将关闭连接,即使出错也会using
。SqlCommand
的连接,因为您没有指定连接。您可以使用property或相应的constructor。以下是一个例子:
int amt;
using (var con = new SqlConnection(ConnectionString)) {
var sql = "SELECT SUM(ItemRate) FROM logs WHERE RoomNo = @RoomNo";
using (var cmd = new SqlCommand(sql, con)) {
cmd.Parameters.AddWithValue("@RoomNo", Int32.Parse(txtRoom.Text));
con.Open();
amt = (int)cmd.ExecuteScalar();
}
}
答案 1 :(得分:1)
打开连接是不够的;
您需要将con
与cmd
相关联。
答案 2 :(得分:1)
与所描述的错误完全相同,您尚未设置SQLCommand
的{{3}}属性。
尝试添加:
cmd.Connection = con;
在致电ExecuteScalar()
之前。
答案 3 :(得分:1)
您已经打开了一个SqlConnection,但是您还没有告诉SqlCommand对象使用它。尝试添加以下行:
cmd.Connection = con;
在执行查询之前。
答案 4 :(得分:1)
您展示的代码存在一些问题 - 尤其是一些seroius安全问题,我强烈建议您阅读SQL injection并准备好语句/参数和using。
只是一些快速更正/评论:
namespace Hotel
{
public partial class Billing : Form
{
SqlConnection con = new SqlConnection();
SqlDataAdapter da;
SqlCommand cmd = new SqlCommand();
DataTable dt = new DataTable();
public Billing()
{
InitializeComponent();
}
private void Billing_Load(object sender, EventArgs e)
{
con.ConnectionString = "Data Source=.\\SQLEXPRESS;AttachDbFilename=D:\\Projects\\c# assignments\\Hotel Manager\\Hotel\\database\\master.mdf;Integrated Security=True;User Instance=True";
//loadData();
}
private void button1_Click(object sender, EventArgs e)
{
con.ConnectionString = "Data Source=.\\SQLEXPRESS;AttachDbFilename=D:\\Projects\\c# assignments\\Hotel Manager\\Hotel\\database\\master.mdf;Integrated Security=True;User Instance=True";
con.Open();
int rno = Int32.Parse(txtRoom.Text);
cmd.Connection = con; // This solves the problem you see
// HERE you SHOULD use a SQL paramter instead of appending strings to build your SQL !!!
cmd.CommandText = "SELECT SUM(ItemRate) FROM logs WHERE RoomNo=" + rno +"";
int amt = (int)cmd.ExecuteScalar(); //arror is at this part
// HERE you SHOULD use a SQL paramter instead of appending strings to build your SQL !!!
// Another point: you build an INSERT but never execute it ?!?
cmd.CommandText = "INSERT INTO bill VALUES('" + txtBillNo.Text.ToString() + "','" + txtRoom.Text.ToString() + "','" + amt.ToString() + "')";
con.Close();
txtBillNo.Text = "";
txtRoom.Text = "";
BillView bv = new BillView();
bv.ShowDialog();
}
}
}
答案 5 :(得分:1)
您没有在button1_click中提供连接字符串。
con.ConnectionString = "Data Source=.\\SQLEXPRESS;AttachDbFilename=D:\\Projects\\c# assignments\\Hotel Manager\\Hotel\\database\\master.mdf;Integrated Security=True;User Instance=True";
你的代码中还有很多问题。它以这种方式工作
{
// Create Connection Object
// Provide connection object with Connection string
// Create command object
// Open connection
// Execute command
// Close connection
// Dispose connection
}
答案 6 :(得分:0)
using (SqlConnection sqlcon = new SqlConnection("Connection String HERE"))
{
using (SqlCommand sqlcmd= new SqlCommand())
{
sqlcmd.Connection = sqlcon;
sqlcmd.CommandType = CommandType.Text;
sqlcmd.CommandText = "SELECT SUM(ItemRate) FROM logs WHERE RoomNo=@rno";
slqcmd.Parameters.AddWithValue("@rno", rno);
try
{
sqlcon.Open();
command.ExecuteNonQuery();
}
catch (SqlException)
{
MessageBox.Show("Your Error Here");
}
finally
{
connection.Close();
}
}
我认为这会有所帮助,而且更安全