[英]variable was undeclared or was never assigned? and data reader not working?
I didn't want to post this but I couldn't find an answer to solve this problem. 我不想发布此消息,但找不到解决此问题的答案。
Im making an application for a friend, but when I run it, it gives me a warning, and recently started crashing. 我正在为朋友创建应用程序,但是当我运行它时,它给了我警告,并且最近开始崩溃。
I tried debugging the problem, and it gave me a completely different problem. 我尝试调试问题,这给了我一个完全不同的问题。
class DBApplication : DBInfo
{
private MySqlConnection connection = new MySqlConnection();
private int customer_id;
public string lastname;
//constructor
public DBApplication()
{
OrderID();
}
public string OrderID()
{
customer_id = 8;
//add the DataBase query to the string.
string query = "SELECT * FROM wdb_customer WHERE=" + customer_id;
if (OpenConnection() == true)
{
MySqlCommand myComm = connection.CreateCommand();
MySqlDataReader Reader;
myComm.CommandText = query;
/*when I debugged, it said this was the problem?
but i dont understand why.*/*
Reader = myComm.ExecuteReader();
while (Reader.Read())
{
lastname = Reader["customer_name"].ToString();
}
Reader.Close();
}
return lastname;
}
}
the first problem "the variable DB_App is either undeclared or was never assigned." 第一个问题是“未声明变量DB_App或从未分配变量。”
thats from this script. 多数民众赞成从这个脚本。
partial class Form1 {
...
private DBApplication DB_App = new DBApplication();
...
private void InitializeComponent()
{
this.OrderID.Text = DB_App.lastname;
}
I have also tried DB_App.OrderID(); 我也尝试过DB_App.OrderID(); same problem.
同样的问题。
Your query is invalid since you miss a column name in WHERE
clause 您的查询无效,因为您错过了
WHERE
子句中的列名
To apply a quick fix change 应用快速修复更改
string query = "SELECT * FROM wdb_customer WHERE=" + customer_id;
to 至
string query = "SELECT * FROM wdb_customer WHERE customer_id = " + customer_id;
^^^ use real column name here
On a side note: do not build SQL query strings directly from user input; 附带说明:不要直接从用户输入构建SQL查询字符串; use parameters instead.
使用参数代替。 It prevents your code from SQL injections .
它可以防止您的代码被SQL注入 。 Read more here
在这里阅读更多
This being said your code might look like this 这就是说您的代码可能看起来像这样
...
myComm.CommandText = "SELECT * FROM wdb_customer WHERE customer_id = @customer_id";
myComm.Parameters.AddWithValue("@customer_id", customer_id);
Reader = myComm.ExecuteReader();
...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.