微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

[VB.NET]帮我看看菜鸟代码??

帮我看看菜鸟代码?? 这是我新建的类DataBasesql Imports System Imports System.Data Imports System.Data.sqlClient Public Class DataBasesql 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 MessageBox.Show(ex.ToString) End Try End Function 关闭数据库连接 Public Overloads Function DBClose() As Boolean Try Conn.Close() Conn = nothing Catch ex As Exception MessageBox.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 MessageBox.Show(ex.ToString) End Try End Function End Class 我在页面调用为 Public Dbsql As New DataBasesql 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 Dbsql.DBopen() Dbsql.DBDataGridView(str,DataGridView1) Dbsql.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 MessageBox.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 MessageBox.Show(ex.ToString) End Try End Function __________________________________________________________________________ 楼上正确。Tables是一个众多DataTable对象的集合,Tables(0)才是一个DataTable对象。 __________________________________________________________________________

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐