请问下面代码第一句该怎么理解?谢谢
If Strings.InStr("INSERT,DELETE,UPDATE", sTokens(0).ToUpper) ThenIf 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
MsgBox("编辑记录成功")
End If
If sTokens(0).ToUpper = "DELETE" Then
MsgBox("删除记录成功")
End If
If sTokens(0).ToUpper = "UPDATE" Then
MsgBox("更新记录成功")
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 --------------------编程问答--------------------
Public Class DBOperation--------------------编程问答-------------------- If Strings.InStr("INSERT,DELETE,UPDATE", sTokens(0).ToUpper) Then
'定义一个数据库操作类函数,其传入的参数是查询字符串,返回值的类型是内存中的数据表DataTable
Shared Function DBOperate(ByVal SQLString As String) As DataTable
Dim CONN As String
CONN = "Provider=Microsoft.Jet.OLEDB.4.0;Password="""";User ID=Admin;Data Source=" + Application.StartupPath + "\database\学籍信息.mdb"
Dim oleconn As New OleDb.OleDbConnection(CONN)
Dim cmd As OleDb.OleDbCommand = New OleDb.OleDbCommand(SQLString, oleconn)
cmd.CommandType = CommandType.Text
Dim sTokens() As String
sTokens = SQLString.Split(" ")
'把SQLString,以空格为分割符,折分成一个数组,放在sTokens() 中。。。。比如说:SQLString="I LOVE YOU"DIM sTokens() as string '定义一个,字符串数组,sTokens = sqlstring.split(" ")那麼sTokens就有值了sTokens(0)="I"sTokens(1)="LOVE"sTokens(2)="YOU"
Try
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
MsgBox("编辑记录成功")
End If
If sTokens(0).ToUpper = "DELETE" Then
MsgBox("删除记录成功")
End If
If sTokens(0).ToUpper = "UPDATE" Then
MsgBox("更新记录成功")
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 ex As Exception
MsgBox(ex)
End Try
End Function
End Class
上面这句吗?
如果文字内包括insert,delete等文字转换为大写形式 --------------------编程问答-------------------- 很呆的写法,判断传入的是不是sql语句。 --------------------编程问答-------------------- 比如:SQLString="select * from table"
sTokens = SQLString.Split(" ")
sTokens(0)="select"
sTokens(0).ToUpper 将"select" 转为大写
If Strings.InStr("INSERT,DELETE,UPDATE", sTokens(0).ToUpper) Then
如果sTokens(0).ToUpper 的字符串在"INSERT,DELETE,UPDATE"中,则....
补充:.NET技术 , VB.NET