简体   繁体   English

变量未声明或从未分配? 和数据读取器无法正常工作?

[英]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.

相关问题 变量未声明或从未分配 - The variable is either undeclared or was never assigned 变量未声明或从未分配警告 - The variable is either undeclared or was never assigned warning 得到错误变量 <variable name> 要么未申报,要么从未分配 - got error The variable <variable name> is either undeclared or was never assigned 变量'variable_name'要么未声明,要么从未分配过 - The variable 'variable_name' is either undeclared or was never assigned 变量未声明或从未分配,并且重建不起作用 - The variable is either undeclared or was never assigned and rebuilding does not work 变量“ control”是未声明的或从未分配过的-除非如此,否则应用程序将进行编译 - The variable 'control' is either undeclared or was never assigned - Except it is, and the application compiles 未声明或从未分配导致错误“变量”的Windows Forms Designer类继承 - Windows Forms Designer class inheritance causing error 'variable' is either undeclared or was never assigned Winform设计X64不起作用“变量usercontrol1未声明或从未分配” - Winform design X64 doesn't work “The variable usercontrol1 is either undeclared or was never assigned” 变量已分配但从未使用过 - The variable is assigned but never used 分配了变量,但从未使用过它的值 - Variable is assigned but its value is never used
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM