[英]How to return a List type variable containing values from LINQ query result in C#.Net?
[英]How to save the result of a SQL query in a variable in C#.net?
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查询结果存储到变量中???
using-statement
进行连接(以及实现IDisposable
所有其他内容)。 Dispose也将关闭连接,甚至在出错时using
。 SqlCommand
的连接,因为您没有指定连接。 您可以使用该属性或相应的构造函数 。 这是一个例子:
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();
}
}
仅打开连接是不够的;
您需要将con
与cmd
关联。
您已经打开了一个SqlConnection,但是您还没有告诉SqlCommand对象使用它。 尝试添加此行:
cmd.Connection = con;
在执行查询之前。
您显示的代码有几个问题 - 尤其是。 一些seroius安全问题,我强烈建议你阅读SQL注入和准备好的语句/参数并使用 。
只是一些快速更正/评论:
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();
}
}
}
您还没有在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
}
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();
}
}
这对我认为有用,而且更安全
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.