执行标量查询代码问题
我试图在下面的查询中返回成员 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
guid
变量不是int
。The
guid
variable is not anint
.你的参数@GuidID是Int类型?
确保它是正确的。
Your paramter @GuidID is an Int type?
Make sure it is right.