标题:用SqlCommand能返回一定义变量re1的值吗?
取消只看楼主
梦幻倩影
Rank: 5Rank: 5
等 级:职业侠客
威 望:8
帖 子:162
专家分:352
注 册:2016-8-19
结帖率:100%
已结贴  问题点数:20 回复次数:2 
用SqlCommand能返回一定义变量re1的值吗?
用SqlCommand能返回一定义变量re1的值吗?代码如下,提示不能用return:

新增和更新都没问题,但就是想返回一个值,到底是新增的,还是更新的

        objConnection = New SqlConnection(sqlstr)
        ' 打开连接
        If objConnection.State <> ConnectionState.Open Then
            objConnection.Open()
        End If
        Dim sql As String
        sql = "declare @re1 varchar(20)" + vbCrLf
        sql += "if not exists (select * from dbo.idpass where 姓名='" + TextBox1.Text + "')" + vbCrLf
        sql += "begin" + vbCrLf
        sql += "if not exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[idpass]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)" + vbCrLf
        sql += "CREATE TABLE [dbo].[idpass] (" + vbCrLf
        sql += "[姓名] [char] (10) COLLATE Chinese_PRC_CI_AS NOT NULL ," + vbCrLf
        sql += "[密码] [char] (10) COLLATE Chinese_PRC_CI_AS NULL ," + vbCrLf
        sql += "[权限] [char] (40) COLLATE Chinese_PRC_CI_AS NULL ) ON [PRIMARY]" + vbCrLf + vbCrLf
        sql += "Insert into idpass(姓名,密码,权限) values('" + TextBox1.Text + "','" + TextBox2.Text + "',' ') " + vbCrLf
        sql += "set @re1='新增操作人员成功' " + vbCrLf
        sql += "end" + vbCrLf
        sql += "else" + vbCrLf
        sql += "begin" + vbCrLf
        sql += "update idpass set 密码 ='" + Trim(TextBox2.Text) + "' where 姓名= '" + Trim(TextBox1.Text) + "'"
        sql += "set @re1='操作人员密码修改成功' " + vbCrLf
        sql += "end" + vbCrLf
        sql += "return @re1"
        MsgBox(sql)
        Dim cmd As SqlCommand = New SqlCommand(sql, objConnection)
2016-09-23 16:50
梦幻倩影
Rank: 5Rank: 5
等 级:职业侠客
威 望:8
帖 子:162
专家分:352
注 册:2016-8-19
得分:0 
还是说只能用函数,或过程
2016-09-23 17:50
梦幻倩影
Rank: 5Rank: 5
等 级:职业侠客
威 望:8
帖 子:162
专家分:352
注 册:2016-8-19
得分:0 
存储过程不熟, 中如何通过代码实现 新建、修改 SQL存储过程?而不用在SQL服务器中去 写和执行 代码
2016-09-23 17:54



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-469113-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.094056 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved