帮我看看菜鸟
代码?? 这是我新建的类DataBase
sql Imports System Imports System.Data Imports System.Data.
sqlClient Public Class DataBase
sql Public Conn As
sqlConnection Public ds As DataSet Public da As
sqlDataAdapter Public Comm As
sqlCommand Public Str_Con As String 打开
数据库连接 Public Overloads Function DBOpen() As System.Data.
sqlClient.
sqlConnection Str_Con = server = ;uid=sa;pwd= ;database =gl Str_Con = Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=gl Try Conn = New
sqlConnection(Str_Con) Conn.Open() DBOpen = Conn Return Conn Catch ex As Exception Conn = Nothing Message
Box.Show(ex.ToString) End Try End Function
关闭数据库连接 Public Overloads Function DBClose() As Boolean Try Conn.Close() Conn = Nothing Catch ex As Exception Message
Box.Show(ex.ToString) End Try End Function 填充数据 Public Function DBDataGridView(ByVal str_cmd As String,ByVal datagv As DataGridView) As String Try da = New
sqlDataAdapter(str_cmd,Conn) ds = New DataSet da.Fill(ds) datagv.DataSource = ds.Tables Catch ex As Exception Message
Box.Show(ex.ToString) End Try End Function End Class 我在
页面中
调用为 Public Db
sql As New DataBase
sql Private Sub Form1_Load(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles MyBase.Load Dim str As String str = select * from tb_vi Db
sql.DBOpen() Db
sql.DBDataGridView(str,DataGridView1) Db
sql.DBClose() End Sub 为什么在DataGridView1不能
显示数据了 。。。 __________________________________________________________________________ 填充数据 Public Function DBDataGridView(ByVal str_cmd As String,Conn) ds = New DataSet da.Fill(ds) datagv.DataSource = ds.Tables datagv.DataBind() //LZ上了一个哦 , C#是datagv.databind(),不知道VB是否也是这样用的! Catch ex As Exception Message
Box.Show(ex.ToString) End Try End Function End Class __________________________________________________________________________ vb.net好像不要绑定哦 __________________________________________________________________________ vb.net好像不要绑定哦 __________________________________________________________________________ Public Function DBDataGridView(ByVal str_cmd As String,ByVal datagv As DataGridView) As String//---返回值? Try da = New
sqlDataAdapter(str_cmd,Conn) ds = New DataSet da.Fill(ds) datagv.DataSource = ds.Tables//這句是得不到數據源的 改成: datagv.DataSource=ds.Tables(0) 或者: datagv.DataSource=ds.Tables datagv.DataMember=ds.Tables(0).TableName Catch ex As Exception Message
Box.Show(ex.ToString) End Try End Function __________________________________________________________________________ 楼上正确。Tables是一个众多DataTable对象的集合,Tables(0)才是一个DataTable对象。 __________________________________________________________________________
原文链接:https://www.f2er.com/vb/263543.html