我不想发布这个,但我找不到解决这个问题的答案。
我正在为朋友制作一个应用程序,但是当我运行它时,它会给我一个警告,并且最近开始崩溃。
我尝试调试问题,它给了我一个完全不同的问题。
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;
}
}
第一个问题“变量DB_App未声明或从未分配。”
来自这个脚本。
partial class Form1 {
...
private DBApplication DB_App = new DBApplication();
...
private void InitializeComponent()
{
this.OrderID.Text = DB_App.lastname;
}
我也试过DB_App.OrderID();同样的问题。
答案 0 :(得分:3)
您的查询无效,因为您错过了WHERE
子句
应用快速修正更改
string query = "SELECT * FROM wdb_customer WHERE=" + customer_id;
到
string query = "SELECT * FROM wdb_customer WHERE customer_id = " + customer_id;
^^^ use real column name here
旁注:不要直接从用户输入构建SQL查询字符串;改用参数。它会阻止您的代码进行SQL注入。阅读更多here
据说你的代码看起来像这样
...
myComm.CommandText = "SELECT * FROM wdb_customer WHERE customer_id = @customer_id";
myComm.Parameters.AddWithValue("@customer_id", customer_id);
Reader = myComm.ExecuteReader();
...