我已经研究过这方面的主题,但遗憾的是我找不到任何我能够轻易理解的内容。我有一个名为GMASS的MySQL数据库代表指导监控和调度系统。我想要做的是当我从学生表中输入学生编号时,学生的姓名将在我的标签中预览。我怎么能这样做?我基本上是这个的初学者。非常感谢你。
答案 0 :(得分:0)
Imports System
Imports System.Data
Imports MySql.Data.MySqlClient
Public Class bd
Public Shared Mystr As String = MyConnectStr.getConnectString("localhost", "root", "", "databasename")
Public Shared Sub setConnectionString(str As String)
Mystr = str
End Sub
Public Shared Function getConnectionString() As String
Return Mystr
End Function
Public Shared Function Executer_scalar(Query As String) As String
Dim MySqlConn As MySqlConnection = Nothing
Try
MySqlConn = New MySqlConnection(getConnectionString())
Dim MySqlCom As New MySqlCommand(Query, MySqlConn)
MySqlConn.Open()
setNamesUTF8(MySqlConn)
Return MySqlCom.ExecuteScalar().ToString()
Catch ex As Exception
Throw ex
Finally
Try
MySqlConn.Close()
Catch generatedExceptionName As Exception
End Try
End Try
End Function
Public Shared Function Executer_NonQuery(Query As String) As Integer
Dim MySqlConn As MySqlConnection = Nothing
Try
MySqlConn = New MySqlConnection(getConnectionString())
Dim MySqlCom As New MySqlCommand(Query, MySqlConn)
MySqlConn.Open()
setNamesUTF8(MySqlConn)
Return MySqlCom.ExecuteNonQuery()
Catch ex As Exception
Throw ex
Finally
Try
MySqlConn.Close()
Catch ex As Exception
End Try
End Try
End Function
Public Shared Function Query(q As String, Optional table_name As String = "Table") As DataTable
Dim MySqlConn As MySqlConnection = Nothing
Try
MySqlConn = New MySqlConnection(getConnectionString())
Dim MySqlCom As New MySqlCommand(q, MySqlConn)
MySqlConn.Open()
setNamesUTF8(MySqlConn)
Dim MySqlda As New MySqlDataAdapter(MySqlCom)
Dim dt As New DataTable(table_name)
MySqlda.Fill(dt)
Return dt
Catch ex As Exception
Throw ex
Finally
Try
MySqlConn.Close()
Catch ex As Exception
End Try
End Try
End Function
Public Shared Function load_table(table_name As String) As DataTable
Dim MySqlConn As MySqlConnection = Nothing
Try
MySqlConn = New MySqlConnection(getConnectionString())
Dim MySqlCom As New MySqlCommand("SELECT * FROM " & table_name, MySqlConn)
MySqlConn.Open()
setNamesUTF8(MySqlConn)
Dim MySqlda As New MySqlDataAdapter(MySqlCom)
Dim dt As New DataTable(table_name)
MySqlda.Fill(dt)
Return dt
Catch ex As Exception
Throw ex
Finally
Try
MySqlConn.Close()
Catch ex As Exception
End Try
End Try
End Function
Public Shared Function load_tables(ParamArray table_name As String()) As DataSet
Dim MySqlConn As MySqlConnection = Nothing
Try
Dim ds As New DataSet()
Dim MySqlCom As MySqlCommand = Nothing
Dim MySqlda As MySqlDataAdapter = Nothing
Dim dt As DataTable = Nothing
MySqlConn = New MySqlConnection(getConnectionString())
MySqlConn.Open()
setNamesUTF8(MySqlConn)
For i As Integer = 0 To table_name.Length - 1
MySqlCom = New MySqlCommand("SELECT * FROM " & table_name(i), MySqlConn)
MySqlda = New MySqlDataAdapter(MySqlCom)
dt = New DataTable(table_name(i))
MySqlda.Fill(dt)
ds.Tables.Add(dt)
Next
Return ds
Catch ex As Exception
Throw ex
Finally
Try
MySqlConn.Close()
Catch ex As Exception
End Try
End Try
End Function
Public Shared Function load_DataSet(Query As String, Optional table_name As String = "Table") As DataSet
Dim MySqlConn As MySqlConnection = Nothing
Try
MySqlConn = New MySqlConnection(getConnectionString())
Dim MySqlCom As New MySqlCommand(Query, MySqlConn)
MySqlConn.Open()
setNamesUTF8(MySqlConn)
Dim MySqlda As New MySqlDataAdapter(MySqlCom)
Dim ds As New DataSet()
MySqlda.Fill(ds, table_name)
Return ds
Catch ex As Exception
Throw ex
Finally
Try
MySqlConn.Close()
Catch ex As Exception
End Try
End Try
End Function
Public Shared Function load_DataSet_S(ParamArray Query As String()) As DataSet
Dim MySqlConn As MySqlConnection = Nothing
Try
Dim ds As New DataSet()
Dim MySqlCom As MySqlCommand = Nothing
Dim MySqlda As MySqlDataAdapter = Nothing
Dim dt As DataTable = Nothing
MySqlConn = New MySqlConnection(getConnectionString())
MySqlConn.Open()
setNamesUTF8(MySqlConn)
For i As Integer = 0 To Query.Length - 1
MySqlCom = New MySqlCommand(Query(i), MySqlConn)
MySqlda = New MySqlDataAdapter(MySqlCom)
dt = New DataTable("Table_" & i)
MySqlda.Fill(dt)
ds.Tables.Add(dt)
Next
Return ds
Catch ex As Exception
Throw ex
Finally
Try
MySqlConn.Close()
Catch
End Try
End Try
End Function
Public Shared Function load_ps(ps_name As String, Optional params_name As String() = Nothing, Optional params_value As Object() = Nothing) As DataTable
Dim MySqlConn As MySqlConnection = Nothing
Try
MySqlConn = New MySqlConnection(getConnectionString())
Dim MySqlCom As New MySqlCommand(ps_name, MySqlConn)
MySqlCom.CommandType = CommandType.StoredProcedure
If (params_name IsNot Nothing) And (params_value IsNot Nothing) Then
For i As Integer = 0 To params_name.Length - 1
MySqlCom.Parameters.AddWithValue(params_name(i).Replace("@", ""), params_value(i))
Next
End If
MySqlConn.Open()
setNamesUTF8(MySqlConn)
Dim MySqlda As New MySqlDataAdapter(MySqlCom)
Dim dt As New DataTable(ps_name)
MySqlda.Fill(dt)
Return dt
Catch ex As Exception
Throw ex
Finally
Try
MySqlConn.Close()
Catch
End Try
End Try
End Function
Public Shared Function load_ps_ds(ps_name As String, Optional params_name As String() = Nothing, Optional params_value As Object() = Nothing) As DataSet
Dim MySqlConn As MySqlConnection = Nothing
Try
MySqlConn = New MySqlConnection(getConnectionString())
Dim MySqlCom As New MySqlCommand(ps_name, MySqlConn)
MySqlCom.CommandType = CommandType.StoredProcedure
If (params_name IsNot Nothing) And (params_value IsNot Nothing) Then
For i As Integer = 0 To params_name.Length - 1
MySqlCom.Parameters.AddWithValue(params_name(i).Replace("@", ""), params_value(i))
Next
End If
MySqlConn.Open()
setNamesUTF8(MySqlConn)
Dim MySqlda As New MySqlDataAdapter(MySqlCom)
Dim ds As New DataSet()
MySqlda.Fill(ds)
Return ds
Catch ex As Exception
Throw ex
Finally
Try
MySqlConn.Close()
Catch ex As Exception
End Try
End Try
End Function
Public Shared Function load_Reader(Query As String, Optional table_name As String = "Table") As MySqlDataReader
Dim MySqlConn As MySqlConnection = Nothing
Try
MySqlConn = New MySqlConnection(getConnectionString())
Dim MySqlCom As New MySqlCommand(Query, MySqlConn)
MySqlConn.Open()
setNamesUTF8(MySqlConn)
Return MySqlCom.ExecuteReader()
Catch ex As Exception
Throw ex
End Try
End Function
Public Shared Function Execute_Transact(ParamArray Query As String()) As Object
Dim MySqlConn As MySqlConnection = Nothing
Dim tran As MySqlTransaction = Nothing
Try
MySqlConn = New MySqlConnection(getConnectionString())
MySqlConn.Open()
setNamesUTF8(MySqlConn)
tran = MySqlConn.BeginTransaction()
Dim MySqlCom As MySqlCommand() = New MySqlCommand(Query.Length) {}
For i As Integer = 0 To Query.Length - 1
MySqlCom(i) = New MySqlCommand(Query(i), MySqlConn)
MySqlCom(i).Transaction = tran
Next
For i As Integer = 0 To Query.Length - 1
MySqlCom(i).ExecuteNonQuery()
Next
tran.Commit()
Return 0
Catch ex As Exception
Try
tran.Rollback()
Catch exx As Exception
End Try
Throw ex
Finally
Try
MySqlConn.Close()
Catch ex As Exception
End Try
End Try
End Function
Private Shared Sub setNamesUTF8(conn As MySqlConnection)
Try
Dim MySqlCom As New MySqlCommand("SET NAMES 'utf8'", conn)
MySqlCom.ExecuteNonQuery()
Catch ex As Exception
Throw ex
End Try
End Sub
End Class