执行标量查询代码问题

发布于 2024-12-19 10:47:24 字数 640 浏览 4 评论 0原文

我试图在下面的查询中返回成员 ID。如果我像查询一样运行查询,我会得到 20,但是当我执行代码时,它会返回零。我在这里做错了什么?

public int GetMemberID(string guid)
   {
       string strConectionString = ConfigurationManager.AppSettings["DataBaseConnection"];
       string StrSql = "SELECT MemberID FROM MEMBERS WHERE (Guid = @GuidID)";

       int memberId;
       using (var connection = new SqlConnection(strConectionString))
       using (var command = new SqlCommand(StrSql, connection))
       {
           command.Parameters.Add("@GuidID", SqlDbType.Int).Value = guid;
           memberId = (int)command.ExecuteScalar();
       }

       return memberId; 

   }

Im trying to return the member id in this query below. If i run the query as just as a query i get 20 but when i exectute the code it returns a zero. What am i doing wrong here?

public int GetMemberID(string guid)
   {
       string strConectionString = ConfigurationManager.AppSettings["DataBaseConnection"];
       string StrSql = "SELECT MemberID FROM MEMBERS WHERE (Guid = @GuidID)";

       int memberId;
       using (var connection = new SqlConnection(strConectionString))
       using (var command = new SqlCommand(StrSql, connection))
       {
           command.Parameters.Add("@GuidID", SqlDbType.Int).Value = guid;
           memberId = (int)command.ExecuteScalar();
       }

       return memberId; 

   }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

埋葬我深情 2024-12-26 10:47:24

guid 变量不是 int

command.Parameters.Add("@GuidID", SqlDbType.VarChar).Value = guid;

The guid variable is not an int.

command.Parameters.Add("@GuidID", SqlDbType.VarChar).Value = guid;
末骤雨初歇 2024-12-26 10:47:24

你的参数@GuidID是Int类型?
确保它是正确的。

Your paramter @GuidID is an Int type?
Make sure it is right.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文