0

我真的很感激我能得到的任何帮助。

我的问题是我将 Combobox1 绑定到 BindingSource 并且 DataMember 和 ValueMember 属性链接并正常工作。对于我的一生,我不知道如何使用 Combobox1 的值(选定的 valuemember)来过滤我在 Combobox2 上显示的结果。我迫切需要一种简单的方法来做到这一点。

我的失败代码如下

Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
    Dim conn As New SqlConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\Database1.mdb") 'This line fails

    Dim strSQL As String = "SELECT * FROM Questions WHERE Section='" & ComboBox1.ValueMember & "'"
    Dim da As New SqlDataAdapter(strSQL, conn)
    Dim ds As New DataSet
    da.Fill(ds, "Disk")

    With ComboBox2 'Here i try to populate the combobox2
        .DataSource = ds.Tables("Questions")
        .DisplayMember = "Question_String"
        .ValueMember = "Question_Code"
        .SelectedIndex = 0
    End With
End Sub

我不断收到如下系统级错误 {"Keyword not supported: 'provider'."}

我尝试了其他一些选项,但我得到的错误似乎更神秘,有人可以帮我解决这个问题。我会很感激的。

4

3 回答 3

1

Dim conn As New OleDBConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source='|DataDirectory|\Database1.mdb'")

此外,您的查询必须使用字符串而不是对象,因此请尝试...

Dim strSQL As String = "SELECT * FROM Questions WHERE Section='" & ComboBox1.ValueMember.toString & "'"

于 2012-05-21T18:02:23.687 回答
0

Here are a couple of observations about your code that I hope you find helpful:

First, you might want to look at this MSDN help on how to move your database connection string out of the code and into a configuration file. This is especially important for your code to work to work more seemlessly across different environment (dev box, staging, production, etc) - Connection Strings and Configuration Files (ADO.NET)

I also noticed that you never explicitly open or close the connection. Per this entry on stack overflow, you should be ok, but keep in mind that if you happen to change the code to explicitly open the connection you will also need to close it.

I also noticed that you aren't using a parameterized query. This makes your code vulnerable to a SQL Injection attack. Here is a link to a blog posting by Scott Guthrie 'Tip/Trick: Guard Against SQL Injection Attacks'. You never know who may copy and paste your block of code with this bad practice.

Finally, you do the following query (with appropriate mods from other answers:

Dim strSQL As String = "SELECT * FROM Questions WHERE Section='" & ComboBox1.ValueMember.toString & "'"

And subsequently only use Question_String, and Question_Code in your code. You might want to consider changing your query to only pull the columns you need. This is especially helpful when you have tables with many columns. Otherwise you will needlessly pull data your code never actually needs. So your query would become:

   Dim strSQL As String = "SELECT Question_String, Question_Code FROM Questions WHERE Section='" & ComboBox1.ValueMember.toString & "'"
于 2012-05-22T15:18:52.440 回答
0

因为您将提供程序用于不需要它的连接,因为提供程序应该是 SQL 驱动程序。

我不知道您使用的是什么数据库(好吧,该文件是访问文件),但您需要检查您是否有正确的连接字符串

http://www.connectionstrings.com/sql-server-2008#p2

于 2012-05-10T15:38:15.037 回答