使用VB.net

时间:2015-07-13 14:44:02

标签: vb.net forms

我有2个表格。第一个表单有一个电子邮件文本框和一个按钮。第一个表单连接到访问数据库。第二种形式也连接到访问并包含多个文本框。

当用户键入他们的电子邮件并点击第一个表单中的按钮时。应用程序应根据其电子邮件查询信息。

示例:我输入" Jeff@gmail.com"在第一个表单中单击按钮。

第二种形式:基于" Jeff@gmail.com"应用程序以形式2查询texbox中的电子邮件信息,如年龄,身高和体重。

我该怎么做?

2 个答案:

答案 0 :(得分:0)

查找SQL SELECT语句并尽可能多地尝试。一旦你发现了障碍,我就会很乐意提供帮助

答案 1 :(得分:0)

您需要使用数据库创建MySqL服务器,并将第一个表单连接到该服务器。例如,我创建了一个名为 user_data.db 的数据库并在此数据库中创建了用户数据表,你说你有两个表单,第一个是一个文本框,更多是关于第二,您需要创建一个新模块,例如 mysql_client ,您需要添加 MySqL.Data 作为参考。然后,我们走了:

Imports System.Net
Imports System.Net.Sockets
Imports MySql.Data.MySqlClient

    Module mysql_client

        Private Function ServerIsRunning(ByVal Ip As String, Port As Int32) As Boolean 'You need to determinate if your MySqL server is running
            Dim TcpClient As New TcpClient
            Dim ipaddr As IPAddress = Nothing
            IPAddress.TryParse(Ip, ipaddr)
            Try
                TcpClient.Connect(ipaddr, Port)
                Return True
            Catch Excep As Exception
                Return False
            End Try
        End Function

        Sub GetUserInfo(ByVal user_email As String)

            'Declaring the connection informations
            Dim MySqL_Server As String = "<your mysql server ip / host address>"
            Dim MySqL_Port As String = "your mysql server port (the default mysql port is 3306)>"
            Dim MySqL_Username As String = "<your username to login into the mysql server>"
            Dim MySqL_Password As String = "<your password to login into the mysql server>"
            Dim MySqL_DataBase As String = "<your mysql database name (now its users_data)>"
            Dim MySqL_DataTable As String = "<your mysql datatable name (now its users)>"

            If ServerIsRunning(MySqL_Server, MySqL_Port) Then 'Determinating if server is running

                Dim conn As New MySqlConnection
                Dim myCommand As New MySqlCommand
                Dim myAdapter As New MySqlDataAdapter
                Dim dt2 As New DataTable

                conn.ConnectionString = "server=" & MySqL_Server & "; port=" & MySqL_Port & "; user id=" & MySqL_Username & "; password=" & MySqL_Password & "; database=" & MySqL_DataBase & "" 'Setting up the connection string


                myCommand.Connection = conn
                myCommand.CommandText = "SELECT id, username, password FROM " & MySqL_DataTable & " where email = '" & user_email & "'" 'Getting id,username,password, from the datatable by the given email address (you can get more values by adding ", " and your text.)



                Try
                    conn.Open()

                    myAdapter.SelectCommand = myCommand
                    myAdapter.Fill(dt2) 'Filling the local datatable

                    Dim datalist As New List(Of String)
                    If dt2.Rows.Count > 0 Then
                        For i As Integer = 0 To dt2.Rows.Count - 1
                            datalist.Add(dt2.Rows(i)(1))
                        Next
                    End If

                    conn.Close() 'Closing the connection

                    'Then you can get values and display it (I using msgbox)

                    For i = 0 To datalist.Count - 1
                        MsgBox(datalist.Item(i))
                    Next

                Catch myerror As MySqlException
                    MessageBox.Show("Error Connecting to Database: " & myerror.Message) 'Showing the exception if some error in the code
                Finally
                    conn.Dispose()
                End Try

            End If

        End Sub

    End Module

我不确定那是不是你想要的,如果没有,请忽略这个答案。