简体   繁体   English

当组合框文本更改VB.NET时,从文本框中的数据库中读取值

[英]Read Value from Database in TextBox when Combobox text changes VB.NET

I have a list of Users Names in ComboBox and Some TextBoxes. 我在ComboBox和一些TextBoxes中有一个用户名列表。 When ComboBox text changes (ie I select some username from ComboBox), The TextBoxes are filled with user details from the database. 当ComboBox文本更改时(即,我从ComboBox中选择了一些用户名),TextBox中将填充数据库中的用户详细信息。

I have code to achieve this in SQL Database. 我有在SQL数据库中实现此目的的代码。 But these queries are not working with MsAccess database. 但是这些查询不适用于MsAccess数据库。

    MysqlConn = New MySqlConnection
    Mysql.ConnectionString = "server=localhost;user=root;password=root;database=database"
    Dim READER As MySqlDataReader
    Try
        MysqlConn.open()
        Dim Query As String
        Query("select * from database.usernames where name='" & ComboBox1.Text & "'")
        Command = New MySqlCommand(Query, MysqlConn)
        READER = Command.ExecuteReader
        While READER.Read
        TextBox1.Text = READER.GetString("name") 
        End While

    End Try

Here is my answer. 这是我的答案。 Please don't get overwhelmed by it. 请不要被它淹没。 ;) ;)

Broken code 密码破损

First of all, as I see it, the code you provided cannot work at all, because: 首先,正如我所见,您提供的代码根本无法工作,因为:

  1. your Query variable is initialized in an invalid (or at least a very exotic) way. 您的Query变量是以无效(或至少非常奇怪的)方式初始化的。 You probably want to use something like: 您可能想要使用类似以下的内容:

     Dim Query As String Query = "select * from database.usernames where name='" & ComboBox1.Text & "'" 

    or in a single line: 或一行:

     Dim Query As String = "select * from database.usernames where name='" & ComboBox1.Text & "'" 
  2. you try to assign the connection string to the ConnectionString property of a nonexistent Mysql variable. 您尝试将连接字符串分配给不存在的Mysql变量的ConnectionString属性。 Or the variable exists because it is declared somewhere else, which might be a bug in your code snippet here. 或者变量存在是因为它在其他地方声明,这可能是您的代码片段中的错误。 But I assume you want to assign the connection string to the MysqlConn.ConnectionString property instead. 但是我假设您想将连接字符串分配给MysqlConn.ConnectionString属性。

  3. you have not declared the MysqlConn and Command variables anywhere. 您尚未在任何地方声明MysqlConnCommand变量。 You only just assign to them. 您只需分配给他们。 (I will simply assume you have declared the variables correctly somewhere else in your code...) (我只是假设您已在代码的其他位置正确声明了变量...)

  4. the IDataRecord interface does not provide a GetString(name As String) method overload. IDataRecord接口不提供GetString(name As String)方法重载。 So unless you have defined a custom extension method for it, you probably need to use the IDataRecord.GetOrdinal(name As String) method as well, or use the column index instead of the column name. 因此,除非为其定义了自定义扩展方法,否则可能还需要使用IDataRecord.GetOrdinal(name As String)方法,或者使用列索引而不是列名。

Anyway, the code you provided uses MySQL. 无论如何,您提供的代码都使用MySQL。 So I assume that MySQL is the "SQL Database" you are using successfully. 因此,我假设MySQL是您成功使用的“ SQL数据库”。 And that seems to work, as you say? 正如您所说,这似乎行得通吗? Well... Hmmm... Then I will simply assume your code snippet is completely correct and works perfectly with MySQL... :/ 嗯...嗯...那么我只是假设您的代码段是完全正确的,并且可以与MySQL完美配合...:/

MS Access vs. MySQL MS Access与MySQL

Using MS Access requires other data access classes (probably the ones in namespace System.Data.OleDb ) and another connection string . 使用MS Access需要其他数据访问类(可能是名称空间System.Data.OleDb )和另一个连接字符串 You could take a look at this ADO.NET OleDb example for MS Access in the Microsoft documentation. 您可以在Microsoft文档中查看有关MS Access的ADO.NET OleDb示例

You probably even have to update your SQL query, because every database system uses its own SQL dialect. 您甚至可能必须更新SQL查询,因为每个数据库系统都使用自己的SQL方言。 You might want to consult the Office documentation for that. 您可能需要为此查阅Office文档 But your query is quite simple, so perhaps all you have to do to make it work with MS Access is: 但是您的查询非常简单,因此可能要使它与MS Access配合使用,您需要做的就是:

  • remove the database name and use only the table name, and 删除数据库名称并仅使用表名称,然后
  • delimit the name identifier (since it is a reserved keyword in MS Access). 分隔name标识符(因为它是MS Access中的保留关键字)。

I personally delimit all identifiers in my SQL queries, just to avoid unintended conflicts with reserved keywords. 我个人在SQL查询中定界了所有标识符,只是为了避免与保留关键字发生意外冲突。 So I would personally use something like this: 因此,我个人将使用以下内容:

select * from [usernames] where [name] = '...'

Additional tips 其他提示

Also, I would like to provide you some additional (unrelated) tips regarding improving your code: 另外,我想向您提供一些有关改进代码的其他(无关)技巧:

  • Use Using -statements with variables of an IDisposable type as much as possible. 尽可能Using -statementIDisposable类型的变量一起Using Those types/classes do not implement that interface if there isn't a good reason for it, so I consider it not unimportant to call Dispose when you are done with such disposable objects (or using a Using statement to call Dispose implicitly). 如果没有充分的理由,这些类型/类将不会实现该接口,因此,我认为在Dispose此类可抛弃对象(或使用Using语句隐式调用Dispose )后,调用Dispose并不重要。
  • Use SQL parameters (if possible) to avoid SQL injection vulnerabilities. 使用SQL参数(如果可能)避免SQL注入漏洞。 Look at this StackOverflow question and its answer for an example of how to use SQL parameters with MS Access. 查看此StackOverflow问题及其答案 ,以获取有关如何在MS Access中使用SQL参数的示例。

Example

You may take a look at the following code snippet. 您可以看一下以下代码片段。 It might not provide a working example out-of-the-box, but you might get some useful/practical ideas from it: 它可能无法提供开箱即用的示例,但是您可能会从中获得一些有用的/实用的想法:

Dim connectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\Data\\database.mdb;User Id=admin;Password="
Dim query As String = "select * from [usernames] where [name] = @Name"

Using conn As New OleDbConnection(connectionString)
    Using command As New OleDbCommand(query)
        command.Parameters.Add("@Name", OleDbType.VarChar, 50).Value = ComboBox1.Text
        conn.Open()

        Using reader As OleDbDataReader = command.ExecuteReader
            If reader.Read Then
                textbox1.Text = reader.GetString(reader.GetOrdinal("name"))
            End If
        End Using
    End Using
End Using

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM