语法错误SQL C#Selectedindexchange问​​题

时间:2018-10-07 07:02:07

标签: c# mysql

希望您可以帮助我们检查此错误。 我们正在尝试使用此代码填充一堆标签,但要不断重复获取相同的错误。 我添加了下面的代码以及错误。

希望您能为我们提供答案,谢谢! 在检查如何使用列表在项目的标签上添加值时,我需要帮助。

对于将要答复的所有人,在此先感谢。

错误:

列表中没有输出或值,无法使用列表更改标签上的值。

private void cboSelectedProdut_SelectedIndexChanged(object sender, 
EventArgs e)
    {
        List<string> conts = new List<string>();

        if (cboSelectedProdut.Text != "")
        {
            PointofSALES sales = new PointofSALES();
            conts = sales.CallProduct(cboCategory.Text, 
cboSelectedProdut.Text);
            lblID.Text = conts[1];
            lblPrice.Text = conts[3];
            lblStock.Text = conts[4];
        }
    }

public List<string> CallProduct(string category, string param)
    {
        string table, product, param1 , param2 , param3;

        List<string> stacks = new List<string>();
        if (category == "Accessories")
        {
            product = "Accessories_Name";
            table = "tbl_accessories";
            param1 = "Accessories_ID";
            param2 = "Accessories_Quantity";
            param3 = "Accessories_Price";
            stacks.AddRange(Catt(product, table, param).ToArray());

        }
        else if (category == "Batteries")
        {
            product = "Batteries_Name";
            table = "tbl_batteries";
            param1 = "Accessories_ID";
            param2 = "Accessories_Quantity";
            param3 = "Accessories_Price";
            //stacks.AddRange(Catt(product, table, param, param1, 
param2, param3).ToArray());

        }
        else if (category == "Cotton")
        {
            product = "Cotton_Name";
            table = "tbl_cotton";
            param1 = "Accessories_ID";
            param2 = "Accessories_Quantity";
            param3 = "Accessories_Price";
            //stacks.AddRange(Catt(product, table, param, param1, 
param2, param3).ToArray());

        }
        else if (category == "Juices")
        {
            product = "Juices_Name";
            table = "tbl_juices";
            param1 = "Accessories_ID";
            param2 = "Accessories_Quantity";
            param3 = "Accessories_Price";
            //stacks.AddRange(Catt(product, table, param, param1, 
param2, param3).ToArray());

        }
        else if (category == "Kits")
        {
            product = "Kits_Name";
            table = "tbl_kits";
            param1 = "Accessories_ID";
            param2 = "Accessories_Quantity";
            param3 = "Accessories_Price";
            //stacks.AddRange(Catt(product, table, param, param1, 
param2, param3).ToArray());

        }
        else if (category == "Mods")
        {
            product = "Mods_Name";
            table = "tbl_mods";
            param1 = "Accessories_ID";
            param2 = "Accessories_Quantity";
            param3 = "Accessories_Price";
            //stacks.AddRange(Catt(product, table, param, param1, 
param2, param3).ToArray());

        }
        else
        {
            product = "VapeSet_Name";
            table = "tbl_vapeset";
            param1 = "Accessories_ID";
            param2 = "Accessories_Quantity";
            param3 = "Accessories_Price";
            //stacks.AddRange(Catt(product, table, param, param1, 
param2, param3).ToArray());

        }

        return stacks;
    }

    public List<string> Catt(string product, string table, string 
param)
     {
         string id, quantity, price;
         List<string> Products = new List<string>();
         Global global = new Global();
         string sql = "SELECT * FROM " + table + " WHERE @pram LIKE 
@param ";
         MySqlConnection connection = new 
MySqlConnection(global.ConnectionString);
         MySqlCommand command = new MySqlCommand(sql, connection);
         command.Parameters.AddWithValue("@param", param);
         command.Parameters.AddWithValue("@pram", product);
         connection.Open();
         MySqlDataReader reader = command.ExecuteReader();
         {
             while (reader.Read())
             {

                 for (int x = 0; x < reader.FieldCount; x++)
                 {

Products.Add(Convert.ToString(reader.GetValue(x)));
                 }

             }
         }
         return Products;
     }

2 个答案:

答案 0 :(得分:1)

这似乎是一个错字,您需要在WHERE

前添加一个空格

更改

string sql = "SELECT * FROM " + table + "WHERE @pram LIKE @param ";

string sql = "SELECT * FROM " + table + " WHERE @pram LIKE @param ";

答案 1 :(得分:0)

您不能在SQL语句中使用参数作为列名,因为它们将被编码为字符串(这会导致SQL语法错误)。像table一样,使用字符串串联来插入这些值。 (此外,表名和WHERE之间需要有一个空格。)

string sql = "SELECT * FROM `" + table + "` WHERE `" + product + "` LIKE @param ";
MySqlConnection connection = new MySqlConnection(global.ConnectionString);
MySqlCommand command = new MySqlCommand(sql, connection);
command.Parameters.AddWithValue("@param",param);