如何在存储过程中获取计算列值

时间:2012-08-27 07:23:32

标签: c# asp.net .net sql-server-2008

我使用的是ASP.NET 4.0,C#和SQL Server 2008 R2。我从网页中的用户那里得到UserName,存储在SQL表User_Info2中。使用SQL Server 2008功能“计算列”我使用存储过程自动为每个Vendor_ID生成Insert。在按钮单击中,在我插入记录后,我想用Vendor_ID显示消息,那么请有人告诉我如何从存储过程中获取Vendor_ID列吗?

    CREATE TABLE User_Info2
    (  SNo int Identity (2000,1) , 
       Vendor_ID AS 'VEN' + CAST(SNo as varchar(16)) PERSISTED PRIMARY KEY, 
       UserName VARCHAR(30) NOT NULL
    )

存储过程

    ALTER PROCEDURE [dbo].[usp_User_Info2] @UserName VARCHAR(30)
    AS  
    BEGIN 
       SET NOCOUNT ON;

       INSERT INTO User_Info2 (UserName) VALUES (@UserName)   
    END   

C#代码:

protected void BtnUserNext_Click(object sender, EventArgs e) 
{      
  SqlConnection SqlCon = new SqlConnection(GetConnectionString());  
  SqlCommand cmd = new SqlCommand();   
  cmd.CommandType = CommandType.StoredProcedure;  
  cmd.CommandText = "usp_User_Info2";   
  cmd.Parameters.Add("@UserName", SqlDbType.VarChar).Value = txtUserName.Text.Trim();      
  cmd.Connection = SqlCon;    
  try   
  {     
     SqlCon.Open();   
     cmd.ExecuteScalar();  
  }  
  finally   
  {
    string url = "../CompanyBasicInfo.aspx?Parameter=" + Server.UrlEncode ("+ Vendor_ID  +");
    ClientScript.RegisterStartupScript(this.GetType(),"callfunction",
    "alert('Login created  successfully for "+ Vendor_ID +"');
    window.location.href = '"  + url + "';", true); 
    SqlCon.Close();    
  }    
}

4 个答案:

答案 0 :(得分:6)

您可以使用OUTPUT子句输出插入的值,然后在执行存储过程时读取它:

ALTER PROCEDURE [dbo].[usp_User_Info2] @UserName VARCHAR(30)
AS  
BEGIN 
   SET NOCOUNT ON;

   INSERT INTO User_Info2 (UserName) 
   OUTPUT Inserted.Vendor_ID
   VALUES (@UserName)   
END   

并在您的C#调用代码中:

  object spResult;
  string vendorID;

  try   
  {     
     SqlCon.Open();   
     spResult = cmd.ExecuteScalar();  

     if(spResult != null)  // check to make sure you got something back!
     {
         vendorID = spResult.ToString();
     }        
  }  

答案 1 :(得分:1)

你可以试试这个

ALTER PROCEDURE [dbo].[usp_User_Info2] @UserName VARCHAR(30)
AS  
BEGIN 
   SET NOCOUNT ON;

   declare @Id int

   INSERT INTO User_Info2 (UserName) VALUES (@UserName)   

   SET @Id = Scope_Identity()

   SELECT Vendor_ID From User_Info2 WHERE SNo = @Id
END   

C#

try   
{     
  SqlCon.Open();   
  string VendorID = cmd.ExecuteScalar() as string;  
}  

答案 2 :(得分:0)

你也可以这样做

select top(1) Vendor_ID from User_Info2 order by SNo desc

答案 3 :(得分:0)

您可以在SCOPE_IDENTITY()

的帮助下获取当前插入行
SET @SNo = SCOPE_IDENTITY()

在插入查询下方,您可以在@SNo

上执行选择查询

所以它会是:

ALTER PROCEDURE [dbo].[usp_User_Info2] 
(
    @UserName VARCHAR(30),
    @SNo int,
    @Vendor_ID VARCHAR(50) OUTPUT
)
AS  
BEGIN 
   SET NOCOUNT ON;

   INSERT INTO User_Info2 (UserName) VALUES (@UserName)
   SET @SNo = Scope_Identity()


   select @Vendor_ID as Vendor_ID from User_Info2 where SNo = @SNo

END   

修改

SqlParameter[] param= new SqlParameter[1];

param[0] = new SqlParameter("@Vendor_ID", 0);
param[0].Direction = ParameterDirection.Output;

//  Here there will be a Stored Procedure Call

int VendorID = Convert.ToInt32(param[0].Value);

现在你将@Vendor_ID Output变量。