我有一个将数据从SQL数据库绑定到gridview的功能。到目前为止,它运用良好:
Dim dad As SqlDataAdapter
Dim bs As BindingSource
Dim dtb As DataTable
Private Sub getDataSQL(ByVal selectCommand As String, ByVal connectionString As String)
dtb = New DataTable
Dim cnn As New SqlConnection(connectionString)
cnn.Open()
dad = New SqlDataAdapter(selectCommand, cnn)
Dim basiccommands = New SqlCommandBuilder(dad)
dad.InsertCommand = basiccommands.GetInsertCommand
dad.UpdateCommand = basiccommands.GetUpdateCommand
dad.DeleteCommand = basiccommands.GetDeleteCommand
dad.Fill(dtb)
bs = New BindingSource
bs.DataSource = dtb
navigator_main.BindingSource = bs 'this is a binding navigator
dgv_main.DataSource = bs 'this is the main datagridview
cnn.Close()
End Sub
当用户添加/编辑/删除我调用的行时:
bs.EndEdit()
dad.Update(dtb)
现在我必须重用getData函数,通过连接到不同类型的数据库(主要是MySQL,ODB,SQL)来填充gridview。为此,我创建了一个接口,允许通过指示连接类型重用大量函数:
Public Interface IDataBaseHelper
Function openConnection(ByVal conn_string As String) As Boolean
Function executeQuery(ByVal query As String) As IDataReader
Function executeQuery(ByVal command As IDbCommand) As IDataReader
Function executeNonQuery(ByVal query As String) As Integer
Function createSQLCommand(ByVal query As String) As IDbCommand
Function getDataAdapter(ByVal query As String) As IDataAdapter
Sub closeConnection()
End Interface
新功能如下所示:
Private mBindingSource As New BindingSource()
Private mDataAdapter As IDataAdapter
Private mDataSet As DataSet
Private Sub getData(ByVal selectCommand As String, ByVal connectionString As String, ByVal connectionType As String)
'Connection types are defined in FDataBaseHelper: FDataBaseHelper.SQLSERVER_ .MYSQL_ .ODBC_
Dim cnn = FDataBaseHelper.createInstance(connectionType)
cnn.openConnection(connectionString)
mDataAdapter = cnn.getDataAdapter(selectCommand)
mDataSet = New DataSet()
Dim dtb As New DataTable
mDataAdapter.Fill(mDataSet)
dtb = mDataSet.Tables(0)
mBindingSource.DataSource = dtb
navigator_main.BindingSource = mBindingSource
dgv_main.DataSource = mBindingSource
End Sub
clase FDataBaseHelper看起来像:
Public Class FDataBaseHelper
'Class factory depending on type of connection
Public Const SQLSERVER_ = "SQLServer"
Public Const MYSQL_ = "MySQL"
Public Const ODBC_ = "ODBC"
Public Shared Function createInstance(conn_type As String) As IDataBaseHelper
If conn_type = SQLSERVER_ Then
Return New DataBaseHelperSQL
ElseIf conn_type = MYSQL_ Then
Return New DataBaseHelperMySQL
ElseIf conn_type = ODBC_ Then
Return New DataBaseHelperODBC
End If
Return Nothing
End Function
End Class
getDataAdapter函数(例如DataBaseHelperSQL)是:
Public Function getDataAdapter(query As String) As IDataAdapter Implements IDataBaseHelper.getDataAdapter
Dim command = createSQLCommand(query)
Dim adapter = New SqlDataAdapter(command)
Return adapter
End Function
datagridview很好地填充,但用户所做的任何更改都不会存储在数据库中。问题是IDataAdapter没有InsertCommand,UpdateCommand和DeleteCommand作为成员,我收到错误:
mAdapter.Update(mDataSet)
错误说:“当使用新行传递DataRow集合时,更新需要有效的InsertCommand” 我已经尝试了几个建议的解决方案,但无济于事,因为我没有发现任何人使用接口。 任何帮助将不胜感激
编辑:最后,解决方案就像在辅助类中移动基本命令的定义一样简单。例如。对于DataBaseHelperSQL,getDataAdapter方法代表:Public Function getDataAdapter(query As String) As IDataAdapter Implements IDataBaseHelper.getDataAdapter
Dim command = createSQLCommand(query)
Dim adapter = New SqlDataAdapter(command)
Dim basiccommands = New SqlCommandBuilder(adapter)
adapter.InsertCommand = basiccommands.GetInsertCommand
adapter.UpdateCommand = basiccommands.GetUpdateCommand
adapter.DeleteCommand = basiccommands.GetDeleteCommand
Return adapter
End Function
答案 0 :(得分:0)
最后,解决方案就像在辅助类中移动基本命令的定义一样简单。例如。对于DataBaseHelperSQL,getDataAdapter方法代表:
Public Function getDataAdapter(query As String) As IDataAdapter Implements IDataBaseHelper.getDataAdapter
Dim command = createSQLCommand(query)
Dim adapter = New SqlDataAdapter(command)
Dim basiccommands = New SqlCommandBuilder(adapter)
adapter.InsertCommand = basiccommands.GetInsertCommand
adapter.UpdateCommand = basiccommands.GetUpdateCommand
adapter.DeleteCommand = basiccommands.GetDeleteCommand
Return adapter
End Function