为了防御性地准备我的问题,我在发布之前使用了Google,Bing和StackOv :-)。另外,我是MVC3的新手,仍然在努力解决框架的语法错综复杂问题。
我在下面的代码块中的SQL语句中出错,这让我很烦恼。语法显示正确。我使用Select * From ..简化了SQL语句,它返回数据就好了。
此外,如果有更好的方法(不使用EF对象),请务必接受建议。我非常喜欢看到SQL语句的灵活性和控制 - 要么就是习惯的形式,要么习惯于它: - )。
提前致谢!!
@using System.Data.SqlClient;
@using System.Configuration;
@{
Layout = null;
}
@{
SqlConnection cn = null;
cn = new SqlConnection(ConfigurationManager.ConnectionStrings["sqlConn"].ToString());
cn.Open();
SqlCommand cmd = new SqlCommand((@"SELECT DISTINCT" +
"tblSBT.sname," +
"tblSBDetails.sid," +
"tblSBDetails.assignedtrack," +
"tblSBDetails.maxtrack," +
"tblSBDetails.currentvals," +
"tblSBDetails.maxvals," +
"tblSBDetails.lastupdated" +
"FROM" +
"tblSBT (NOLOCK)" +
"LEFT OUTER JOIN" +
"tblSBDetails (NOLOCK)" +
"ON" +
"tblSBT.sid = tblSBDetails.sid" +
"WHERE" +
"tblSBDetails.lastupdated > DateADD(n, -5, GETDATE())"+
"ORDER BY" +
"tblSBT.sname" +), cn);
var myreader = cmd.ExecuteReader();
}
答案 0 :(得分:17)
如果您正在使用@符号,则无需像执行此操作那样连接字符串。当你加入这样的字符串时,它也不是编写这段代码的最有效方式。
SqlConnection cn = null;
cn = new SqlConnection(ConfigurationManager.ConnectionStrings["sqlConn"].ToString());
cn.Open();
SqlCommand cmd = new SqlCommand((@"SELECT DISTINCT
tblSBT.sname,
tblSBDetails.sid,
tblSBT.sname,
tblSBDetails.sid,
tblSBDetails.assignedtrack,
tblSBDetails.maxtrack,
tblSBDetails.currentvals,
tblSBDetails.maxvals,
tblSBDetails.lastupdated
FROM tblSBT (NOLOCK)
LEFT OUTER JOIN tblSBDetails (NOLOCK)
ON .sid = tblSBDetails.sid
WHERE tblSBDetails.lastupdated > DateADD(n, -5, GETDATE())
ORDER BY tblSBT.sname"), cn);
var myreader = cmd.ExecuteReader();
答案 1 :(得分:3)
您必须在字符串之间添加空格。
SqlCommand cmd = new SqlCommand((@"SELECT DISTINCT " +
"tblSBT.sname," +
"tblSBDetails.sid," +
"tblSBDetails.assignedtrack," +
"tblSBDetails.maxtrack," +
"tblSBDetails.currentvals," +
"tblSBDetails.maxvals," +
"tblSBDetails.lastupdated" +
" FROM" +
" tblSBT (NOLOCK)" +
" LEFT OUTER JOIN" +
" tblSBDetails (NOLOCK)" +
" ON" +
" tblSBT.sid = tblSBDetails.sid" +
" WHERE" +
" tblSBDetails.lastupdated > DateADD(n, -5, GETDATE())"+
" ORDER BY" +
" tblSBT.sname"), cn);
不要连接字符串,因为您使用的是multiline string literal @。
SqlCommand cmd = new SqlCommand(@"SELECT DISTINCT tblSBT.sname,tblSBDetails.sid,
tblSBDetails.assignedtrack,
tblSBDetails.maxtrack,
tblSBDetails.currentvals,
tblSBDetails.maxvals,
tblSBDetails.lastupdated
FROM tblSBT (NOLOCK)
LEFT OUTER JOIN
tblSBDetails (NOLOCK)
ON tblSBT.sid = tblSBDetails.sid
WHERE tblSBDetails.lastupdated > DateADD(n, -5, GETDATE())
ORDER BY tblSBT.sname",cn);
答案 2 :(得分:2)
更好的调试方法是将字符串变量=设置为整个SQL语句,然后将其复制并粘贴到SQL Server Mgmt Studio中。您可能已经看到代码一起运行并确定了您的问题。
另外,我认为你真的不了解@符号在这里的用法。只有你的第一行“SELECT DISTINCT”被视为文字,你连接的其余字符串只是字符串,而不是字符串文字。实际上,在这种情况下甚至不重要。
你可能意味着什么
SqlCommand cmd = new SqlCommand((@"SELECT DISTINCT
tblSBT.sname,
tblSBDetails.sid,
tblSBDetails.assignedtrack,
tblSBDetails.maxtrack,
tblSBDetails.currentvals,
tblSBDetails.maxvals,
tblSBDetails.lastupdated
FROM
tblSBT (NOLOCK)
LEFT OUTER JOIN
tblSBDetails (NOLOCK)
ON
tblSBT.sid = tblSBDetails.sid
WHERE
tblSBDetails.lastupdated > DateADD(n, -5, GETDATE())
ORDER BY
tblSBT.sname"), cn);