简体   繁体   English

我的逻辑正确吗? 我正在尝试从组合框(下拉列表)获取数据,并使用该数据在清单框中搜索和显示信息

[英]Is my logic correct? I'm trying to get data from combobox(dropdownlist) and use that data to search and display information in checkedlistbox

I keep getting this error whenever I do the search: Invalid column name 'datafromcombobox' 每当我执行搜索时,我都会不断收到此错误:无效的列名'datafromcombobox'

I tried hardcode the data in my SQL query and it works. 我尝试对SQL查询中的数据进行硬编码,并且可以正常工作。 Any idea what's wrong with my code? 知道我的代码有什么问题吗?

Here's the code: 这是代码:

        string selectedMedication = cboMedicationType.SelectedItem.ToString();

        string strMedications = "SELECT medicationName FROM MEDICATION WHERE medicationType= (" + selectedMedication + ")";
        SqlCommand cmdMedications = new SqlCommand(strMedications, connection);

        connection.Open();
        SqlDataReader readMedications = cmdMedications.ExecuteReader();

        while (readMedications.Read())
        {
            string medicationVar = readMedications["medicationName"].ToString();
            clbMedication.Items.Add(medicationVar, true);

        }
        readMedications.Close();
        connection.Close();

I think you just forgot the quotes in your query. 我认为您只是忘记了查询中的引号。 This should work : 这应该工作:

string strMedications = "SELECT medicationName FROM MEDICATION WHERE medicationType=   ('" + selectedMedication + "')";

I would do it this way: 我会这样:

using (SqlCommand cmdMedications = new SqlCommand(strMedications, connection))
{
    string strMedications = "SELECT medicationName FROM MEDICATION WHERE medicationType = @selectedMedication;
    command.Parameters.AddWithValue("selectedMedication", cboMedicationType.SelectedItem.ToString());
    connection.Open();
    using (SqlDataReader readMedications = cmdMedications.ExecuteReader())
    {
        while (readMedications.Read())
        {
            string medicationVar = readMedications["medicationName"].ToString();
            clbMedication.Items.Add(medicationVar, true);
        }
    }
    connection.Close();
}

I changed several things: 我改变了几件事:

  • Always use parameterized queries. 始终使用参数化查询。 If you don't, you can be a victim of SQL Injection . 否则,您可能成为SQL Injection的受害者。 I changed that in your code. 我在您的代码中进行了更改。
  • It's usually better to use using in your code ( see this article ), because then you don't need to close or dispose objects--the using statements take care of that. 通常最好在代码中使用using请参阅本文 ),因为那样就不需要closedispose对象了using语句可以解决这一问题。
  • As for your error, it's probably an error in your query, but my code should take care of that. 至于您的错误,可能是您的查询中的错误,但是我的代码应该解决这个问题。

Let me know if I can clear up something else. 让我知道是否可以清除其他内容。 I hope this helps! 我希望这有帮助!

PS I mostly typed this without debugging so there might be some small things you might have to tweak, but I thing you should get the gist of it. PS我主要是在没有调试的情况下键入此内容,因此可能需要调整一些小东西,但我认为您应该了解它的要旨。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我的逻辑正确吗? 我正在尝试从数据库中进行简单的搜索 - Is my logic correct? I'm trying to do a simple search from a database 我正在尝试使用 switch case 来显示存储在类中的数据 - I'm trying to use switch case to display data stored in a class 我正在尝试从 Android 设备获取用于 Unity 应用程序的光传感器数据 - I'm trying to get light sensor data from an Android device for use in a Unity app 将Dropdownlist用作数据源,然后使用Foreach从dropdownlist获取数据 - Dropdownlist used as Datasource, then use Foreach to get data from dropdownlist 从comboBox获取数据并使用它来获取另一个组合框中的数据 - Getting data from comboBox and use it to get data in onother combobox 如何获取正确的信息以显示在列表框中? - How can i get the correct information to display in my listbox? 我的逻辑正确吗? 我正在尝试计算在foreach循环中读取的每个循环的总量? - Is my logic correct? I'm trying to calculate the total amount of each loop that's being read in a foreach loop? 尝试获取要显示在我的列表视图中的数据 - Trying to get data to display in my listview 试图从我的列表框中获取信息以显示在文本框中 - Trying to get information from my listbox to display in text boxes 我正在尝试从SQL Server Compact数据库中读取数据,但是我一直收到相同的错误 - I'm trying to read data from my SQL Server Compact database, but I keep getting the same error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM