简体   繁体   English

根据会员名称通过vb.net从数据库中获取值?

[英]Get a value from Database According to the Member Name with vb.net?

I have a SQL table which looks like: 我有一个SQL表,看起来像:

+-------------------+
| id   member   req |
+-------------------+
| 1    Jim      0   |
| 2    Mary     0   |
| 3    Hunter   0   |
+-------------------+

I need to get the id of a certain member through my vb.net application (WinForms) in Visual Basic 2010. 我需要通过Visual Basic 2010中的vb.net应用程序(WinForms)获取某个成员的id

Lets say I wanted to get Mary 's id . 可以说我想得到玛丽id What is the SQL query to return the id for Mary ? 返回Maryid的SQL查询是什么?

Whats the Query for that? 对此的查询是什么?

-------------------------------------------------------------------------------- -------------------------------------------------- ------------------------------

Possible Query Code: 可能的查询代码:

'My.Settings.username is the name I'm looking up in the database to get the member id . 'My.Settings.username是我正在数据库中查找以获取成员id

Dim SQLID As String = "SELECT id FROM members WHERE member= '" & My.Settings.username & "'" submitRequest(SQLID) vagueID.Text = 'ID Here'

First, download the MySQL connector . 首先,下载MySQL连接器

Second, add a reference to the MySQL connection to your project. 其次,添加对项目的MySQL连接的引用。

Third, import the Namespace of which you'll be using many of the classes from to reduce typing (at top of .vb file): 第三,导入您将使用许多类的命名空间以减少键入(在.vb文件顶部):

Imports MySql.Data.MySqlClient

Finally in your Form's Load event handler (or other event handler where you wish to display the ID in the label): 最后,在表单的Load事件处理程序(或希望在标签中显示ID的其他事件处理程序)中:

'Change to your specific connection string.
Dim strConnectionString As String = "Database=YourDatabaseName;Data Source=localhost;User Id=YourUser;Password=YourPassword"

Dim sqlConnection As New MySqlConnection(strConnectionString)

Dim strCommandText = "SELECT id FROM members WHERE member = @Member"

'Assumes that the ID field is an INT type.  Use a Long type if the type is a BIGINT.
Dim intID as Integer

Try

    'Open the connection.
    sqlConnection.Open()

    'Create a command to get the ID.
    Dim sqlCommand As MySqlCommand = New MySqlCommand(strCommandText, sqlConnection)

    'It is always a good idea to use parameters.
    sqlCommand.Parameters.AddWithValue("@Member", My.Settings.username)

    vagueID.Text = Convert.ToString(sqlCommand.ExecuteScalar())

Catch ex As Exception
    'Error handling goes here.
    MsgBox(ex.Message)
Finally
    'Always close the connection!
    sqlConnection.Close()
End Try

MySQLCon = New MySqlConnection MySQLCon.ConnectionString = "Database=localhost;Data Source=host;User Id=root;Password=root" Dim READER As MySqlDataReader Dim COMMAND As MySqlCommand Try MySQLCon.Open() Dim SQLID As String = "SELECT id FROM members WHERE member='" & My.Settings.username & "'" COMMAND = New MySqlCommand(SQLID, MySQLCon) READER = COMMAND.ExecuteReader() While READER.Read vagueID.Value2 = READER.GetInt32("id") End While MySQLCon.Close() MySQLCon.Dispose() Catch ex As Exception 'Do Something End Try

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

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