| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1087 人关注过本帖
标题:存储过程返回值问题
只看楼主 加入收藏
狂砍程序
Rank: 1
等 级:新手上路
帖 子:127
专家分:0
注 册:2007-9-27
结帖率:100%
收藏
 问题点数:0 回复次数:3 
存储过程返回值问题
请问一下,问题是这样的
  我现在执行了一个存储过程,是一个注册用户帐号的存储过程,当用户注册帐号时,帐号是程序返回的,而不是用户填的,用户只填一些个人资料就行了,就和声请QQ帐号一样,是程序返回的用户帐号
  那么我现在就是这个存储过程,用户表示这样的
UserNum int identity(1000,1)
从1000开始递增,我在存储里执行了添加用户资料的存储过程,要返回该用户的UserNum,请问我在程序里面怎么去返回,@@identity怎么去定义和返回这个递增的用户名变量,而在程序里面是怎样获取这个变量的呢,啊
搜索更多相关主题的帖子: 帐号 返回值 identity 变量 用户 
2008-05-07 07:21
pasttime
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2008-5-7
收藏
得分:0 
看一下吧
USE [M_shop]
GO
/****** 对象:  StoredProcedure [dbo].[SPADDBOOK]    脚本日期: 05/06/2008 16:58:06 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[SPADDBOOK]
@Title nvarchar(50)=null,
@Author nvarchar(50)=null,
@Pubs nvarchar(50)=null,
@Category nvarchar(50)=null,
@EntryDate nvarchar(50)=null,
@Price float=null,
@PurchasedFrom nvarchar(50)=null,
@BookFor nvarchar(50)=null,
@BookId int output,
@Status int output
AS
BEGIN
SET NOCOUNT ON;
set @Price= cast(@Price as float)
INSERT INTO BookMaster(Title,Author,Pubs,Category,EntryDate,Price,PurchasedFrom,BookFor) values(@Title,@Author,@Pubs,@Category,@EntryDate,@Price,@PurchasedFrom,@BookFor)
SET @BookId =@@identity
IF @@error=0
SET @Status=1
else
SET @Status=0
END
2008-05-07 17:28
pasttime
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2008-5-7
收藏
得分:0 
程序部分
public  class DataAccess
    {
        
            SqlConnection Conn;
            SqlDataAdapter DA = new SqlDataAdapter();
            SqlCommandBuilder CB;
            DataTable DT;
            public SqlConnection OpenConnection()
            {
                Conn = new SqlConnection("data source=172.27.26.61;database=M_shop;User ID=webplus;Password=webplus");
                return Conn;
            }
            public Array CallStoredProcedure(string[,] MyINPram, string[,] MyOUTPram, string MySPName)
            {

                string Result;
                Conn = OpenConnection();
                SqlCommand CmdSp = new SqlCommand();
                CmdSp.Connection = Conn;
                = MySPName.ToString();
                = CommandType.StoredProcedure;
                //input parameters
                for (int i = 0; i < MyINPram.Length / 2; i++)
                {
                    CmdSp.Parameters.AddWithValue(MyINPram[i, 0], MyINPram[i, 1]);
                }
                //outparameters
                for (int i = 0; i < MyOUTPram.Length / 2; i++)
                {
                    if (MyOUTPram[i, 1] == "SqlDbType.VarChar")
                    {
                        CmdSp.Parameters.Add(MyOUTPram[i, 0], SqlDbType.VarChar, 150);
                    }
                    else
                    {
                        CmdSp.Parameters.Add(MyOUTPram[i, 0], SqlDbType.Int, 4);
                        //CmdSp.Parameters.Add(MyOUTPram[i, 0], MyOUTPram[i,1]);
                    }
                    CmdSp.Parameters[MyOUTPram[i, 0]].Direction = ParameterDirection.Output;
                }
                Conn.Open();
                CmdSp.ExecuteNonQuery();
                Conn.Close();
                string[,] Temparray = new string[MyOUTPram.Length / 2, 2];
                for (int i = 0; i < MyOUTPram.Length / 2; i++)
                {
                    Result = Convert.ToString(CmdSp.Parameters[MyOUTPram[i, 0]].Value);
                    //Result = (string)CmdSp.Parameters["@Msg"].Value;
                    Temparray[i, 0] = MyOUTPram[i, 0];
                    Temparray[i, 1] = Result;
                }
                return Temparray;

            }
            public DataTable CallStoredProcedure(string MySPName)
            {
                Conn = OpenConnection();
                SqlCommand cm = new SqlCommand(MySPName, Conn);
                = CommandType.StoredProcedure;
                DA.SelectCommand = cm;
                DT = new DataTable();
                DA.Fill(DT);
                return DT;
            }

        }
2008-05-07 17:29
pasttime
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2008-5-7
收藏
得分:0 
调用部分,只说原理
try
                {
                    DataAccess DA = new DataAccess();
                    OutResult = (string[,])DA.CallStoredProcedure(PramIn, PramOut, "SPADDBOOK");
                    NewBookId = Convert.ToInt32(OutResult[0, 1]);
                    StatusId = Convert.ToInt32(OutResult[1, 1]);
                }
                catch (Exception ex)
                {
                    LblMsg.Text = "::ERROR:: " + ex.Message;

                }
2008-05-07 17:30
快速回复:存储过程返回值问题
数据加载中...
 
   



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

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