注册 登录
编程论坛 VB.NET论坛

Sqlhelper类 如何调用

yuk_yu 发布于 2016-11-03 15:42, 4542 次点击
Public Function ExecuteNonQueryCommand(ByVal strSQL As String, ByVal params() As SqlParameter) As SqlCommand
        Dim oconn As SqlConnection = GetConnection()
        Dim command As New SqlCommand(strSQL, oconn)
         = CommandType.Text
        For i As Integer = 0 To params.Length - 1
            command.Parameters.Add(params(i))
        Next
        oconn.Open()
        Dim retval As Integer = command.ExecuteNonQuery()
        oconn.Close()
        oconn.Dispose()
        Return command
    End Function



我想调用这个类来执行以下SQL
Dim StrSql As String = "Delete  from SysUser where NTIDName='Yu_yuk' and ValidityDate='07/25/2017'"

可以指点一下吗?SqlParameter 我不懂得怎么写
5 回复
#2
不说也罢2016-11-04 09:12
不知你的这个Sqlhelper类是如何定义的,有些奇怪
ExecuteNonQueryCommand函数的两个参数需要传递的第一个是存储过程名称,另一个是SqlParameter,这是存储过程需要的参数集,而该函数里又定义了 = CommandType.Text,我就不明白了。应该是 = CommandType.StoredProcedure才对。

如果你想调用一个类来执行以下SQL
Dim StrSql As String = "Delete  from SysUser where NTIDName='Yu_yuk' and ValidityDate='07/25/2017'"
可以这么写:
Public Function ExecuteNonQueryCommand(ByVal strSQL As String) As Integer
        Dim oconn As SqlConnection = GetConnection()'这是连接数据库的,不解释了
        Dim command As New SqlCommand(strSQL, oconn)
         = CommandType.Text
        oconn.Open()
        Dim retval As Integer = command.ExecuteNonQuery()'返回受影响的行数
        oconn.Close()
        oconn.Dispose()
        Return retval
    End Function

在调用时
Dim StrSql As String = "Delete  from SysUser where NTIDName='Yu_yuk' and ValidityDate='07/25/2017'"
Dim Mycmd As Integer=ExecuteNonQueryCommand(StrSql)
#3
梦幻倩影2016-11-04 09:22
http://
#4
不说也罢2016-11-04 09:24
至于SqlParameter的定义,可以这么写:当然这是在调用服务器存储过程时才用得到。
dim pters(3) as sqlparameter
pters(0)=new sqlparameter("",sqlbtype.nvarchar)
pters(0).value="这里赋值"
.....
command.parameters.addrange(pters)

建议楼主复习一下SqlCommand类的用法
#5
yuk_yu2016-11-04 09:32
回复 4楼 不说也罢
谢谢版主,我再好好学习下,先搞透SQLCommand,再次感谢!
#6
yuk_yu2016-11-04 12:14
回复 5楼 yuk_yu
我研究出来了,跟大家分享!如果谁有兴趣的我可以把这个类完整分享给大家。

程序代码:
Dim SqlCmd As SQLHelper = New SQLHelper

        Dim paras As SqlParameter() = {New SqlParameter("@IDNumer", TextBox1.Text),
                                          New SqlParameter("@NTIDName", TextBox2.Text)}

        Dim StrSql As String = "Delete from shortuser where NTIDName=@NTIDName and IDNumer=@IDNumer"
        SqlCmd.ExecuteNonQueryCommand(StrSql, paras)
1