在新建的类代码中写下面代码
Public Function ExecuteNoSQL(ByVal Sql As String) As String
Dim con As SqlConnection
Dim com As SqlCommand
con = New SqlConnection(GetConn)
con.Open()
com = New SqlCommand(Sql, con)
Return com.ExecuteNonQuery
con.Close()
com.Dispose()
End Function
Public Function ExecuteNoSQL(ByVal sql As String, ByVal parameter() As SqlParameter) As String
Dim con As SqlConnection
Dim com As SqlCommand
Dim i, j As Integer
j = parameter.Length - 1
con = New SqlConnection(GetConn)
con.Open()
com = New SqlCommand(sql, con)
For i = 1 To j
com.Parameters.Add(parameter(i))
Next
Return com.ExecuteNonQuery
con.Close()
com.Dispose()
End Function
对ExecuteNosql进行重载,第一个是不带参数的执行,根据SQL语句返回受影响的行数,第二个是带参数的,patameter数据下标应从1开始使用,返回受影响的行数.
在普通的用户界面代码中写下面代码
Dim addQx(3) As SqlParameter
Dim sql As String
Dim jc As String = ""
addQx(1) = New SqlParameter("@yhid", SqlDbType.VarChar)
addQx(1).Value = yhid
addQx(2) = New SqlParameter("@yhmc", SqlDbType.VarChar)
addQx(2).Value = yhmc
addQx(3) = New SqlParameter("@yhmm", SqlDbType.VarChar)
addQx(3).Value = pass
sql = " insert into 用户信息 (用户id,名称,密码,状态) values (@yhid,@yhmc,@yhmm,'未审核') "
jc = Trim(ks.ExecuteNoSQL(sql, addQx))
好了,到此就可以使用了
如对本文有疑问,请提交到交流论坛,广大热心网友会为你解答!! 点击进入论坛