c# - 在c# Visual Studio中的一个连接中创建多个mysql命令&where子句中的未知列

标签 c# mysql visual-studio database-connection

我的mySql数据库包含ID,projectName,companyName,projectNum,..等 我需要创建显示项目名称的组合框(项目名称不唯一) 当我尝试执行此操作时,出现以下错误: “where 子句中的未知列‘proj2’” 即使当我尝试打印这个值时,它也会在我的代码中成功打印。 所以我改为在 Combobox 中显示 ID 并且效果很好 现在我需要如果我选择一个ID来填充一些字段(projectName,companyName,projectNum)然后在其他Combobox(例如Combobox2)中显示值,它的项目编号不唯一并且它 取决于项目名称字段。 我尝试建立一个连接和两个连接,但都不起作用。 Combobox2 中什么也没有出现 当我尝试从第一个组合框中选择 ID 时,出现相同的错误: “where 子句中的未知列‘proj2’” 我不知道是否应该改变数据库的设计。 再次我要提到的是,项目名称、公司名称、项目编号可能会在50多个记录中重复。 下面是代码 第一个函数填充第一个组合框:

private void Form2_Load(object sender, EventArgs e)
        {
            try
            {
               // String getQuery = "Select projectName From ubc.BOQ_Table Group By projectName";
                String getQuery = "Select ID From ubc.BOQ_Table";

                connection.Open();
                MySqlCommand command = new MySqlCommand(getQuery, connection);
                MySqlDataReader reader = command.ExecuteReader();
                while (reader.Read())
                {
                    for (int i = 0; i < reader.FieldCount; i++)
                    {
                        comboBox1.Items.Add(reader.GetString("ID"));

                    }
                }
               reader.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            connection.Close();
        }

填充字段的第二个函数取决于选择 ID:

 private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            try

        {

        //get vaalue of selected project
        selectedProject = comboBox1.SelectedItem.ToString();

            String selectQuery = "Select * From ubc.BOQ_Table where ID=" + selectedProject;
            connection.Open();
            MySqlCommand command = new MySqlCommand(selectQuery, connection);
            MySqlDataReader reader = command.ExecuteReader();
            if (reader.Read())
            {
                projectNameText.Text = reader.GetString("projectName");
                projectName = projectNameText.Text;
                companyNameText.Text = reader.GetString("companyName");
                projectNumber.Text = reader.GetInt32("projectNumber").ToString();

                reader.Close();
            }
                command.CommandText = "Select itemNum From ubc.BOQ_Table where projectName=" + projectName;
                command.ExecuteNonQuery();
                while (reader.Read())
                {
                    for (int i = 0; i < reader.FieldCount; i++)
                    {
                        comboBox2.Items.Add(reader.GetString("itemNum"));

                    }
                }
                reader.Close();
        }
        catch(Exception ex)
        {
            MessageBox.Show(ex.Message);
        }

        connection.Close();

    }

最佳答案

这行代码导致了问题:

command.CommandText = "Select itemNum From ubc.BOQ_Table where projectName=" + projectName;

正如评论者所提到的,连接字符串会导致语法错误,并使您的代码容易受到 SQL 注入(inject)攻击。解决方案是通过将变量放入 MySqlParameter 对象中来使用“参数化查询”。

command.CommandText = "Select itemNum From ubc.BOQ_Table where projectName=@projectName;";
command.Parameters.AddWithValue("@projectName", projectName);
using (var reader = command.ExecuteReader())
{
    // ...

(您可能会发现有些人说“不要使用 AddWithValue”,但这种反对意见仅适用于 SqlCommand;没有充分的理由避免使用它与MySqlCommand。)

关于c# - 在c# Visual Studio中的一个连接中创建多个mysql命令&where子句中的未知列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59476573/

相关文章:

c# - 如何从 Visual Studio 扩展和新的 csproj 格式获取 IntermediateOutputPath

mysql - php/mysql 编码不工作

android - 登录网络服务器时出现问题

c# - 如何将 .jpg 包含到 dll 中?

c# - WCF DataContract DataMember 命令?

MySQL 错误 1172 - 结果由多行组成

c++ - 如何为 Visual Studio Clang-Format 插件提供 clang 格式文件?

c# - 正则表达式查询生成器

c# - 如何在 C# 中停止 ffmpeg 视频录制?

C# - EF4 - 获取服务器的 DateTime.Now