Shared Function DBOperate(ByVal SQLString As String, ByRef Msg As String) As DataTable
Try
Dim CONN As String = "Provider = SQLOLEDB.1;data source=127.0.0.1\SQLEXPRESS;initial catalog=HYSYS;user id=sa;password=1234"
Dim oleconn As New OleDb.OleDbConnection(CONN)
Dim cmd As OleDb.OleDbCommand = New OleDb.OleDbCommand(SQLString, oleconn)
= CommandType.Text
Dim sTokens() As String
sTokens = SQLString.Split(" ") '.split去掉中间的空格
If Strings.InStr("INSERT,DELETE,UPDATE", sTokens(0).ToUpper) Then
If oleconn.State <> ConnectionState.Open Then
oleconn.Open()
'打开数据库连接
End If
cmd.ExecuteNonQuery()
'执行SQL语句
If oleconn.State <> ConnectionState.Closed Then
oleconn.Close() '关闭数据库连接
End If
If sTokens(0).ToUpper = "INSERT" Then
Msg = "插入记录成功"
End If
If sTokens(0).ToUpper = "DELETE" Then
Msg = "删除记录成功"
End If
If sTokens(0).ToUpper = "UPDATE" Then
Msg = "更新记录成功"
End If
Return Nothing
Else
Dim ObjectdsDataSet As New DataSet()
Dim adapter As New OleDb.OleDbDataAdapter()
adapter.TableMappings.Add("Table", "TEMP")
adapter.SelectCommand = cmd
If oleconn.State <> ConnectionState.Open Then
oleconn.Open()
'打开数据库连接
End If
cmd.ExecuteNonQuery()
'执行SQL语句
If oleconn.State <> ConnectionState.Closed Then
oleconn.Close() '关闭数据库连接
End If
adapter.Fill(ObjectdsDataSet) '填充数据集
Return ObjectdsDataSet.Tables("TEMP")
End If
Catch
MsgBox(Err.Description)
End Try
Return Nothing
End Function