2

I am using ASP.NET 4.0, C# and SQL Server 2008 R2. I am getting UserName from the user in a webpage for stored in the SQL table User_Info2. Using SQL Server 2008 feature "computed column" I am generating the Vendor_ID automatically for every Insert using the stored procedure. In button click, after I insert the record I want to display the message with Vendor_ID, so please anyone tell me how to get the Vendor_ID column from the stored procedure ?

    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
    )

Stored procedure

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

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

C# Code :

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

4 回答 4

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();
     }        
  }  
于 2012-08-27T07:39:44.297 回答
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;  
}  
于 2012-08-27T07:42:54.190 回答
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变量。

于 2012-08-27T07:45:38.517 回答
0

你也可以这样做

select top(1) Vendor_ID from User_Info2 order by SNo desc
于 2012-08-27T07:42:19.983 回答