我希望能够读取一个值(在这种情况下为组ID)。我已经看过/读过的所有主题和教程都将数据收集到一个文本框中。
在这种情况下,我不想将其放在文本框中;我想获取组ID,然后说:
这是数据库的图像
基本上..但我没有看过任何教程或多个论坛。他们都没有取值,而是说value = 4则登录或执行其他操作。
任何帮助将不胜感激
If text = "1" Then
MysqlConn = New MySqlConnection
MysqlConn.ConnectionString =
"server='ip of server'.; username=; password=; database="
Dim READER As MySqlDataReader
Dim member_group_id As String
Try
MysqlConn.Open()
Dim Query As String
Query = "SELECT * FROM `core_members` where name='" & TextBox2.Text & "'"
Query = "SELECT * FROM `nexus_licensekeys` where lkey_key='" & TextBox1.Text & "'"
COMMAND = New MySqlCommand(Query, MysqlConn)
READER = COMMAND.ExecuteReader
Dim count As Integer
count = 0
While READER.Read
count = count + 1
End While
这是我到目前为止所拥有的。我是用Visual Basic实现MySQL数据的一种新方法,只是最近才开始使用它。我不确定接下来会发生什么,甚至不确定如何从读取组ID等开始。
正如我所说,从此以后的任何帮助都将非常感谢您如何读取组ID并说出此组ID =此数字,然后执行此操作。我确定你知道这个主意。
答案 0 :(得分:1)
我将代码分为UI Sub和可以将数据返回到UI的数据访问功能。您的事件过程代码应该相当简短,并且这些功能应具有单一目的。
将数据库对象保留在该方法的本地。这样,您可以更好地控制。 Using...End Using
块可确保即使发生错误也可以关闭和处置数据库对象。
我留给您添加验证码。检查是否有空的TextBox或没有记录返回。
我希望这可以作为使用ADO.net的快速入门。收获是:
使用参数
确保连接已关闭。 (使用块)
Private ConnString As String = "server=ip of server; username=; password=; database="
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim GroupID As String = GetGroupID(TextBox1.Text)
If GroupID = "4" Then
'your code here
End If
Dim LocalTable As DataTable = GetLicenseKeysData(TextBox1.Text)
'Get the count
Dim RowCount As Integer = LocalTable.Rows.Count
'Display the data
DataGridView1.DataSource = LocalTable
End Sub
Private Function GetGroupID(InputName As String) As String
'Are you sure member_group_id is a String? Sure looks like it should be an Integer
Dim member_group_id As String = ""
'You can pass the connection string directly to the constructor of the connection
Using MysqlConn As New MySqlConnection(ConnString)
'If you only need the value of one field Select just the field not *
'ALWAYS use parameters. See comment by @djv concerning drop table
Using cmd As New MySqlCommand("SELECT g_id FROM core_members where name= @Name")
'The parameters are interperted by the server as a value and not executable code
'so even if a malicious user entered "drop table" it would not be executed.
cmd.Parameters.Add("@Name", MySqlDbType.VarChar).Value = InputName
MysqlConn.Open()
'ExecuteScalar returns the first column of the first row of the result set
member_group_id = cmd.ExecuteScalar.ToString
End Using
End Using
Return member_group_id
End Function
Private Function GetLicenseKeysData(InputName As String) As DataTable
Dim dt As New DataTable
Using cn As New MySqlConnection(ConnString)
Using cmd As New MySqlCommand("SELECT * FROM `nexus_licensekeys` where lkey_key= @Name;", cn)
cmd.Parameters.Add("@Name", MySqlDbType.VarChar).Value = InputName
cn.Open()
dt.Load(cmd.ExecuteReader())
End Using
End Using
Return dt
End Function