vb.net 利用datagridview显示DB数据(亲自实践)

vb.net 利用datagridview显示DB数据(亲自实践),第1张

概述1 窗口form中追加控件datagridview    2 窗口form程序中设定:    Public Class FormDummyman       '链接对象设定       Dim conn As String = "data source=服务器名称;database=数据库名称; user id=登录用户名;password=密码;"       '连接       Dim con 1窗口form中追加控件datagrIDvIEw

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数据(亲自实践)所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1284848.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-09
下一篇 2022-06-09

发表评论

登录后才能评论

评论列表(0条)

保存