2013-04-04 97 views
1

我有一個存儲過程輸出參數

create proc spAddEmployees 
@Name varchar(50), 
@Gender varchar(10), 
@Salary int, 
@EmployeeId int out 
as 
begin 
insert into tblEmployees values (@Name, @Gender, @Salary) 
select EmployeeId = SCOPE_IDENTITY() 
end 

具有告訴當前SCOPE_IDENTITY 的標記看起來用戶的輸出參數一樣

<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent"> 
    <table style="border: 1px solid black; font-family:Arial"> 
    <tr> 
     <td> 
      Employee Name 
     </td> 
     <td> 
      <asp:TextBox ID="txtEmployeeName" runat="server"></asp:TextBox> 
     </td> 
    </tr>   
    <tr> 
     <td> 
      Gender 
     </td> 
     <td> 
      <asp:DropDownList ID="ddlGender" runat="server"> 
       <asp:ListItem>Male</asp:ListItem> 
       <asp:ListItem>Female</asp:ListItem> 
      </asp:DropDownList> 
     </td> 
    </tr> 
    <tr> 
     <td> 
      Salary 
     </td> 
     <td> 
      <asp:TextBox ID="txtSalary" runat="server"></asp:TextBox> 
     </td> 
    </tr>  
    <tr> 
     <td colspan="2"> 
      <asp:Button ID="btnSubmit" runat="server" Text="Submit" 
       onclick="btnSubmit_Click" /> 
     </td> 
    </tr>   
    <tr> 
     <td colspan="2"> 
      <asp:Label ID="lblMessage" runat="server"></asp:Label> 
     </td> 
    </tr> 
</table> 

而後面的代碼

public partial class _Default : System.Web.UI.Page 
    { 
     protected void Page_Load(object sender, EventArgs e) 
     { 

     } 

     protected void btnSubmit_Click(object sender, EventArgs e) 
     { 
      //read from the config file so you don't have to hardcode the connection string 
      string cs = ConfigurationManager.ConnectionStrings["dbcs"].ConnectionString; 
      //when you use the using statement the database connection is automatically closed for you 
      using(SqlConnection con = new SqlConnection(cs)) 
      { 
       SqlCommand cmd = new SqlCommand("spAddEmployees", con); 

       cmd.CommandType = CommandType.StoredProcedure; 


       cmd.Parameters.AddWithValue("@Name", txtEmployeeName.Text); 
       cmd.Parameters.AddWithValue("@Gender",ddlGender.SelectedValue); 
       cmd.Parameters.AddWithValue("@Salary", txtSalary.Text); 


       SqlParameter outputParmeter = new SqlParameter(); 

       outputParmeter.ParameterName = "@EmployeeId"; 

       outputParmeter.SqlDbType = SqlDbType.Int; 
       outputParmeter.Direction = ParameterDirection.Output; 

       cmd.Parameters.Add(outputParmeter); 

       con.Open(); 
       cmd.ExecuteNonQuery(); 

       string EmpId = outputParmeter.Value.ToString(); 
       lblMessage.Text = "Employee Id = " + EmpId;  


      } 

     } 
    } 

該程序應該向tblEmployees表中添加一個新行,然後向用戶輸出存儲過程的輸出參數。它在點擊按鈕時運行並將該行添加到數字中,但輸出參數未被打印到屏幕上。思考?

回答

5

也許:

select EmployeeId = SCOPE_IDENTITY() 

在存儲過程應該是:

select @EmployeeId = SCOPE_IDENTITY() 
+0

這就是它:)。 – wootscootinboogie 2013-04-04 20:17:23

2

在存儲過程中的語法是不正確的。
您應該使用

select @EmployeeId = SCOPE_IDENTITY() 
    ^

,也是通常的方式來檢索輸出參數是通過命令集合。我不確定在這個過程中參數被替換的地方

string EmpId = cmd.Parameters["@EmplyeeId"].Value.ToString(); 
+0

感謝行業標準的建議。總是很高興知道你什麼時候做其他人會發現奇怪的事情。 – wootscootinboogie 2013-04-04 20:18:41

+0

正如我所說,關於第二點我不確定。我一直這樣做,但通過調試器的簡單傳遞可以解決疑問。 – Steve 2013-04-04 20:19:51