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 表添加新行,然后将存储过程的输出参数输出给用户。单击按钮时,它会运行并将行添加到数字中,但输出参数未打印到屏幕上。想法?

4

2 回答 2

5

也许:

select EmployeeId = SCOPE_IDENTITY() 

在存储过程中应该是:

select @EmployeeId = SCOPE_IDENTITY() 
于 2013-04-04T20:16:09.610 回答
2

存储过程中的语法不正确。
你应该使用

select @EmployeeId = SCOPE_IDENTITY()  
       ^

检索输出参数的常用方法是通过命令集合。我不确定在此过程中参数是否在某处被替换

string EmpId = cmd.Parameters["@EmplyeeId"].Value.ToString();
于 2013-04-04T20:17:04.813 回答