2窗口form程序中设定:
Public Class FormDummyman
'链接对象设定
Dim conn As String = "data source=服务器名称;database=数据库名称; user id=登录用户名;password=密码;"
'连接
Dim connection As New sqlClient.sqlConnection
'DB中指定表的内容
Dim dataSet As New DataSet
'DataTable
Dim table As New DataTable
'共通
Dim common As New Common
'画面启动
Public Sub FormDummyman_Load(ByVal sender As Object,ByVal e As System.EventArgs) Handles Me.Load
'判断连接状态
If connection.State = ConnectionState.Closed Then
'连接数据库
connection.ConnectionString = conn
'数据库打开
connection.Open()
End If
'取得指定表内容
dataSet = getDBTable(connection,"dummyman")
table = dataSet.Tables(0)
Me.DataGridView1.DataSource = table
Me.Show()
End Sub
'画面关闭
Private Sub FormDummyman_Closing(ByVal sender As Object,ByVal e As System.EventArgs) Handles Me.Closed
If connection.State = ConnectionState.Open Then
connection.Close()
End If
dataSet = Nothing
table = Nothing
'返回formMenu窗口
FormMenu.FormMenu_Load(sender,e)
End Sub
'获取sql Server中取得指定表的内容
Private Function getDBTable(ByVal connection As sqlClient.sqlConnection,ByVal tableName As String)
Try
Dim adapter As New sqlClient.sqlDataAdapter
Dim cmd As String = ""
'取得excel中sheet1的全部内容
cmd = "select * from [" & tableName & "]"
adapter = New sqlClient.sqlDataAdapter(cmd,connection)
Dim topics As New DataSet
adapter.Fill(topics)
Return topics
Catch Ex As Exception
MsgBox("获取sql Server中取得指定表的内容处理出错!! getDBTable() 错误信息: " & Ex.Message)
Return New DataSet
End Try
End Function
End Class
http://hi.baidu.com/bigheadsheep/blog/item/a489a217471b67e91bd57616.html
原文链接:https://www.f2er.com/vb/259821.html