这次更新主要加入一个执行大sql的函数。因为发现同时执行两个大sql(2000行以上)的时候,经常性的超时,所以加入了一个切分大sql的函数。另外,对所有使用connection对象的地方进行了dispose。
Public Class DataDriver
Private Shared _connString As String
Private Shared Function GetConnString()Function GetConnString() As String
If (_connString Is Nothing) Then
_connString = System.Configuration.ConfigurationSettings.AppSettings("ConnectionString")
End If
Return _connString
End Function
'过滤单引号',防止sql注入攻击
Public Shared Function DealSqlStr()Function DealSqlStr(ByVal strInput As String) As String
Dim strTemp As String
strTemp = strInput
If (strTemp <> Nothing AndAlso strTemp.Length > 0) Then
strTemp = strTemp.Replace("'", "''")
Else
strTemp = ""
End If
Return strTemp
End Function
'分页获取数据
Public Shared Function GetPage(ByVal col As String, ByRef table As String, ByRef dt As System.Data.DataTable, Optional ByVal page As Integer = 1, Optional ByVal condition As String = "", Optional ByVal order As String = "", Optional ByVal pageSize As Integer = 20) As Integer
Dim sql As String = "Select " + col + " from " + table
If (condition <> "") Then
sql += " where " + condition
End If
If (order <> "") Then
sql += " order by " + order
End If
Return GetPage(sql, dt, page, pageSize)
End Function
'分页获取数据
Public Shared Function GetPage(ByVal sql As String, ByRef dt As System.Data.DataTable, ByVal page As Integer, ByVal pageSize As Integer) As Integer
Dim objConn As New SqlConnection(UtilTools.GetConnString())
Dim objCmd As New SqlCommand("xp_GetPage", objConn)
Dim ds As New DataSet
Dim recordCount As Integer
objCmd.CommandType = CommandType.StoredProcedure
objCmd.Parameters.Add("@sql", sql)
objCmd.Parameters.Add("@page", page)
objCmd.Parameters.Add("@pageSize", pageSize)
objCmd.Parameters.Add("@needCount", 1)
Dim objDA As New SqlDataAdapter("", objConn)
objDA.SelectCommand = objCmd
objDA.Fill(ds)
dt = ds.Tables(1)
recordCount = ds.Tables(2).Rows(0).Item(0)
objCmd.Dispose()
objConn.Dispose()
Return recordCount
End Function
'获取单行数据
Public Shared Sub GetOneRow(ByVal col As String, ByVal table As String, ByRef dr As System.Data.DataRow, Optional ByVal condition As String = "")
Dim sql As String = "Select top 1 " + col + " from " + table
If (condition <> "") Then
sql += " where " + condition
End If
GetOneRow(sql, dr)
End Sub
'获取单行数据,如统计值等
Public Shared Sub GetOneRow(ByVal sql As String, ByRef dr As System.Data.DataRow)
Dim dt As New DataTable
GetRows(sql, dt)
If (dt.Rows.Count > 0) Then
dr = dt.Rows(0)
Else
dr = Nothing
End If
End Sub
'获取多行数据
Public Shared Sub GetRows(ByVal sql As String, ByRef dt As System.Data.DataTable)
Dim objConn As New SqlConnection(UtilTools.GetConnString())
Dim objDA As New SqlDataAdapter(sql, objConn)
objDA.Fill(dt)
objConn.Dispose()
objDA.Dispose()
End Sub
'执行sql语句
Public Shared Sub ExecuteSql(ByVal sql As String)
Dim objConn As New SqlConnection(UtilTools.GetConnString())
Dim objCmd As New SqlCommand(sql, objConn)
Try
objConn.Open()
objCmd.ExecuteNonQuery()
Catch ex As System.data.sqlclient.SqlException
Throw ex
Finally
objConn.Close()
objConn.Dispose()
objCmd.Dispose()
End Try
End Sub
'执行大sql语句,传入为StringBuilder以提高效率。
Public Shared Sub ExecuteBigSql(ByRef sbSql As System.Text.StringBuilder, Optional ByVal startWord As String = "insert into", Optional ByVal groupNumber As Integer = 500)
Dim str As String
Dim bGoOn As Boolean = True
Dim iStart As Integer = 0
Dim iNext As Integer = 0
str = sbSql.ToString()
While (bGoOn)
iStart = iNext
For i As Integer = 0 To groupNumber
iNext = str.IndexOf(startWord, iNext)
If (iNext < 0) Then
bGoOn = False
Exit For
Else
iNext += startWord.Length
End If
Next
If (iNext >= 0) Then
iNext -= startWord.Length
End If
If (iNext > 0) Then
ExecuteSql(str.Substring(iStart, iNext - iStart))
Else
ExecuteSql(str.Substring(iStart))
End If
End While
End Sub
End Class
该部分代码为完全代码,以前版本请参考:
http://www.cnblogs.com/squirrel_sc/archive/2004/09/27/47049.html