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

前端之家收集整理的这篇文章主要介绍了[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对象。 __________________________________________________________________________ 原文链接:https://www.f2er.com/vb/263543.html

猜你在找的VB相关文章