0

我有我想要返回的 SQL:

  CREATE PROCEDURE [Zinc].[GetUserByEmail]
    @Email nvarchar (256)
  AS
    SELECT zu.UserId FROM zinc.users zu WHERE zu.Email = @Email

然后在我的存储库中我到目前为止:

public int GetUserByEmail(string email)
{
    using (SqlConnection conn = new SqlConnection(ZincModelContainer.CONNECTIONSTRING))
    {
        using (SqlCommand cmd = conn.CreateCommand())
        {
            conn.Open();

            cmd.CommandType = System.Data.CommandType.StoredProcedure;
            cmd.CommandText = "[Zinc].[GetUserByEmail]";

            //not sure how to go from here?
        }
        return userId;
    }
}
4

3 回答 3

3

因为它是一个标量值,所以使用:

cmd.Parameters.Add("@Email", SqlDbType.NChar).Value = email;
var UserId = cmd.ExecuteScalar();
于 2013-02-22T07:52:54.013 回答
1
cmd.Parameters.Add("@Email", SqlDbType.NChar).Value = email;

int UserId = cmd.ExecuteScalar();
于 2013-02-22T07:53:32.997 回答
0

Microsoft 文档通常是最好的起点。有关从 SqlConnection 读取的示例,请参阅:http: //msdn.microsoft.com/en-us/library/877h0y3a.aspx 。

于 2013-02-22T07:54:59.883 回答