MVC 3中的Sql错误
本文关键字:错误 Sql 中的 MVC | 更新日期: 2023-09-27 18:11:49
我一直得到一个错误说:
c附近语法错误"(sql error
)
但是我看不出为什么。我在Studio Management中运行了这个程序,没有出现任何问题。下面是我的方法:
public string GetSecurityDetails(int contactId)
{
ContactId = contactId;
using (DataAccess da = new DataAccess())
{
da.Execute("SELECT c.ContactId,c.SecurityQuestionAnswer,q.Name" +
"FROM profile.SecurityQuestion q" +
"INNER JOIN profile.Contact c ON q.SecurityQuestionID = c.SecurityQuestionFK" +
"WHERE c.ContactID = @Contact",new SqlParameter("@Contact",contactId));
while(da.Read())
{
SecurityAnswer = da.GetValue<string>("securityQuestionAnswer");
SecurityQuestion = da.GetValue<string>("Name");
break;
}
}
}
我正在使用ASP。. NET MVC 3 in Visual Studio 2010
您的查询中缺少空格:
da.Execute(
"SELECT c.ContactId,c.SecurityQuestionAnswer,q.Name FROM profile.SecurityQuestion q " +
"INNER JOIN profile.Contact c ON q.SecurityQuestionID = c.SecurityQuestionFK " +
"WHERE c.ContactID = @Contact",new SqlParameter("@Contact",contactId)
);
但是我会在字符串之前使用@
,所以它可以是多行:
da.Execute(
@"SELECT c.ContactId,c.SecurityQuestionAnswer,q.Name FROM profile.SecurityQuestion q
INNER JOIN profile.Contact c ON q.SecurityQuestionID = c.SecurityQuestionFK
WHERE c.ContactID = @Contact",new SqlParameter("@Contact",contactId)
);
看起来你在个人资料中缺少一个空格。安全问题q " +另一个在q.SecurityQuestionID = SecurityQuestionFK " +