关于数据库连接-调用类--help
我一个连接数据库和对数据库操作的类。如何调用DataGrid和dataset的就不知道怎么用!!
'该类实现对数据库的查询、插入、删除、修改以及数据导出等功能
Imports System.Data.SqlClient
Public Class clsDBOperator
Private strConnection As String '保存数据库连接字符串
Private SQLConn As New SqlConnection() '定义连接
Private SQLCmd As New SQLCommand() '定义连接命令
Private SQLDA As New SqlDataAdapter() '新建对象数据适配器
Private SQLDS As New DataSet() '定义数据集
Sub New(ByVal str As String) 'str连接字符串
strConnection = str
SQLConn.ConnectionString = strConnection
SQLCmd.Connection = SQLConn
End Sub
'用于测试数据库连接
Public Function TestConnect() As Boolean
Try
SQLConn.Open()
If SQLConn.State = ConnectionState.Open Then
Return True
Else
Return False
End If
Catch ex As Exception
MessageBox.Show(ex.Message)
Finally
SQLConn.Close()
End Try
End Function
'用于对数据库执行插入删除更新操作
'*********************************************************
'用于对数据库的查询操作、值返回到DataGrid中
'Private strConnection As String '保存数据库连接字符串
'Private SQLConn As New SqlConnection() '定义连接
'Private SQLCmd As New SQLCommand() '定义连接命令
' Private SQLDA As New SqlDataAdapter() '新建对象数据适配器
'Private SQLDS As New DataSet() '定义数据集
'********************************************************
Public Function SQLCommand(ByVal strCmd As String) As Integer
Try
If TestConnect() = True Then
SQLConn.Open()
SQLCmd.CommandText = strCmd
Return SQLCmd.ExecuteNonQuery
Else
Return 0
End If
Catch ex As Exception
MessageBox.Show(ex.Message)
Finally
SQLConn.Close()
End Try
End Function
'*********************************************************
'用于对数据库的查询操作、值返回到DataGrid中
'Private strConnection As String '保存数据库连接字符串
'Private SQLConn As New SqlConnection() '定义连接
'Private SQLCmd As New SQLCommand() '定义连接命令
' Private SQLDA As New SqlDataAdapter() '新建对象数据适配器
'Private SQLDS As New DataSet() '定义数据集
'********************************************************
'用于对数据库的查询操作返回到数据集
Public Sub DataToDS(ByVal DS As DataSet, ByVal strCmd As String)
Try
If TestConnect() = True Then
SQLDA.SelectCommand.Connection = SQLConn
SQLConn.Open()
SQLDA.SelectCommand.CommandText = strCmd
DS.Clear()
SQLDA.Fill(DS)
End If
Catch ex As Exception
MessageBox.Show(ex.Message)
Finally
SQLConn.Close()
End Try
End Sub
'***********************************************************
'用于对数据库的查询操作、值返回到DataGrid中
'Private strConnection As String '保存数据库连接字符串
'Private SQLConn As New SqlConnection() '定义连接
'Private SQLCmd As New SQLCommand() '定义连接命令
' Private SQLDA As New SqlDataAdapter() '新建对象数据适配器
'Private SQLDS As New DataSet() '定义数据集
'***********************************************************
Public Sub DataToDG(ByVal Dg As DataGrid, ByVal strCmd As String)
Try
If TestConnect() = True Then
SQLDA.SelectCommand.Connection = SQLConn
SQLConn.Open()
SQLDA.SelectCommand.CommandText = strCmd
SQLDS.Clear()
SQLDA.Fill(SQLDS)
Dg.DataSource = SQLDS.Tables(0)
End If
Catch ex As Exception
MessageBox.Show(ex.Message)
Finally
SQLConn.Close()
End Try
End Sub
End Class
如下调用好像不可以:
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
'Dim bataset As New DataGrid
Dim cmd As New clsDBOperator(strcon)
Dim str As String
str = "select * from 进货单"
cmd.DataToDG(DataGrid, str) ‘DataGrid是我窗体上的一个DataGrid控件
End Sub
请问我要怎么调用呢
还有如何调用daset的呢!麻烦各位大虾帮忙~!~!!help!!!!!!!!!!!!!!!!!!!!11最好写个示例给偶看!!
问题点数:20、回复次数:5Top
1 楼boblaile(爱在13月32)回复于 2006-03-10 12:19:44 得分 0
'用于对数据库的查询操作返回到数据集的怎么调用!!!!!
help
不好意思上面daset写错了Top
2 楼godwu(灵魂舞者)回复于 2006-03-10 14:52:00 得分 20
直接用DataToDG(DataGrid, str) 就行了吧Top
3 楼boblaile(爱在13月32)回复于 2006-03-10 16:03:33 得分 0
我去试试Top
4 楼boblaile(爱在13月32)回复于 2006-03-10 16:08:49 得分 0
strcon是连接字符串 放在一个模块中
忘了说Top
5 楼boblaile(爱在13月32)回复于 2006-03-11 14:05:22 得分 0
还是不行啊!Top




