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
总结以上是内存溢出为你收集整理的vb.net 利用datagridview显示DB数据(亲自实践)全部内容,希望文章能够帮你解决vb.net 利用datagridview显示DB数据(亲自实践)所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)