数据库通用模块
这个是vb6.0下的,一直操作挺好用的
- VB code
'//一下代码是在glmk模块中'-------------------------'功能:设置连接字符串'返回:取得连接到数据库的字符串'-------------------------Public Function strConnectString() As String 'strConnectString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + App.Path + _ "\data\xyk.mdb;Persist Security Info=False;Jet OLEDB:Database Password=" strConnectString = "Provider=SQLOLEDB.12;Persist Security Info=False;Initial Catalog=" + glmk.psjk + ";User ID=" + glmk.puser + ";Password=" & glmk.ppassword & ";Data Source=" & glmk.pserverEnd Function'-------------------------'功能:通用的连接数据库模块'参数:SQL SQL语言'返回:1.直接对数据库进行操作时无返回值(方法1)' 2.返回一个 Recordset 对象(方法2)'方法:1、ExecuteSQL(sql)' 2、Set RS=ExecuteSQL(sql) 'RS是已经定义好的一个纪录集 sql 是查询语句 ''-------------------------Public Function ExecuteSQL(ByVal sql As String) As ADODB.Recordset Dim cnn As ADODB.Connection Dim rst As ADODB.Recordset Dim sTokens() As String On Error GoTo ExecuteSQL_Error '//设置错误捕获 sTokens = Split(sql) '//分割字符串 Set cnn = New ADODB.Connection cnn.Open strConnectString If InStr("INSERT,DELETE,UPDATE", UCase$(sTokens(0))) Then cnn.Execute sql Else Set rst = New ADODB.Recordset rst.Open Trim$(sql), cnn, adOpenStatic, adLockOptimistic Set ExecuteSQL = rst End IfExecuteSQL_Exit: Set rst = Nothing Set cnn = Nothing Exit FunctionExecuteSQL_Error: Debug.Print err.Description Debug.Print sql Resume ExecuteSQL_ExitEnd Function'//使用方法1。。。。在需要操作的窗体内'修改用户"张三"的密码,改为“123”Dim rs As New ADODB.Recordsetdim sql as string sql="select * from czyb where czyxm='张三'"set rs= glmk.ExecuteSQL(sql) '这样就可以对这个czyb进行修改删除等操作了rs.Fields("czymm")="123"rs.Updateset rs=nothing'.....省略'// 使用方法二'删除用户"张三"dim sql as string sql="delete from czyb where czyxm='张三'"glmk.ExecuteSQL(sql) '这样操作就无返回’//现在转到.net平台以后,这个模块怎么修改。。。。如果我返回一个 dataset的话,修改的数据能同步到数据库吗?
谁能给一个.net平台下的通用数据库操作模块呀
[解决办法]
参考:
- VB.NET code
Public Shared ReadOnly connstr As String = "Server=" & _ sqlservername & ";packet size=4096;Database=" & sqldatabase & _ ";User ID=" & sqlusername & ";Password=" & sqlpassword & ";Trusted_Connection=False" Public Shared ReadOnly Conn As New SqlConnection(connstr)Public Shared Function sqlread(ByVal sql As String, ByVal tablename As String) As DataSet Try GC.Collect() Dim sqlcmd As New SqlDataAdapter(sql, Conn) sqlcmd.Fill(sqlread, tablename) Catch ex As Exception Return Nothing End Try End FunctionPublic Overloads Shared Function sqlrun(ByVal sql As String, ByVal okmsg As String, ByVal errmsg As String) Try Conn.Open() Dim sqlcmd As New SqlCommand(sql, Conn) sqlcmd.ExecuteNonQuery() Conn.Close() GC.Collect() If okmsg <> "" Then MsgBox(okmsg, MsgBoxStyle.Information, "数据操作成功!") End If Catch ex As Exception Conn.Close() If errmsg <> "" Then MsgBox(errmsg + " " + ex.Message, MsgBoxStyle.Exclamation, "数据操作失败!") Else MsgBox(ex.Message, MsgBoxStyle.Exclamation, "数据操作失败!") End If Return Nothing End Try Return Nothing End Function
[解决办法]
[解决办法]
[解决办法]
GC.Collect()不是占资源大,而是会启动垃圾回收,会移动对象,重新完成内存整理。这个过程频繁地调用,会让你的.NET程序暂时停顿。
[解决办法]
这是正统的。不知道可否
- VB.NET code
Dim CONN As String = GConnectionString() Dim SqlConn As New SqlConnection(CONN) Dim SqlCmd As SqlCommand = New SqlCommand(SQLString, SqlConn) SqlCmd.CommandType = CommandType.Text Dim FenGe() As String FenGe = SQLString.Split(" ") If Strings.InStr("INSERT,DELETE,UPDATE", FenGe(0).ToUpper) Then If SqlConn.State <> ConnectionState.Open Then SqlConn.Open() '打开数据库连接 End If SqlCmd.ExecuteNonQuery() '执行SQL语句 If SqlConn.State <> ConnectionState.Closed Then SqlConn.Close() '关闭数据库连接 End If 'If FenGe(0).ToUpper = "INSERT" Then ' MessageBox.Show("插入记录成功") 'End If 'If FenGe(0).ToUpper = "DELETE" Then ' MessageBox.Show("删除记录成功") 'End If 'If FenGe(0).ToUpper = "UPDATE" Then ' MessageBox.Show("更新记录成功") 'End If Return Nothing Else Dim ObjectdsDataSet As New DataSet() Dim Adapter As New SqlDataAdapter Adapter.TableMappings.Add("Table", "TEMP") Adapter.SelectCommand = SqlCmd If SqlConn.State <> ConnectionState.Open Then SqlConn.Open() '打开数据库连接 End If SqlCmd.ExecuteNonQuery() '执行SQL语句 If SqlConn.State <> ConnectionState.Closed Then SqlConn.Close() '关闭数据库连接 End If Adapter.Fill(ObjectdsDataSet) '填充数据集 Return ObjectdsDataSet.Tables("TEMP") End If Catch 'InSertLog("执行DBOperate:" & SQLString) MsgBox(Err.Description) End try
[解决办法]
再加一个数据回滚吧~出错后自动取消前面的数据执行
[解决办法]