我对这个SQL语句有些麻烦。我搜索了周围,但无法找到awnser。所以这是我用来获取适合我的condidtions的用户数据的声明:
SqlConnection connection = null;
try
{
connection = new SqlConnection(Main.Settings.Connectionstring);
connection.Open();
}
catch (Exception)
{
MessageBox.Show("Failure at creating a connection to the database!", "Fehler", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
if (tabPage == TabPageEnum.Email)
{
SqlCommand importUser = new SqlCommand("select address.ADR_FIRSTNAME, address.ADR_LASTNAME, address.ADR_EMAIL1, address.ADR_ID" +
"from dbo.ADDRESS as address, dbo.EMPLOYEE as emp" +
"where emp.EMP_ADR_ID = address.ADR_ID" +
"and address.GCRecord IS NULL" +
"and address.ADR_EMAIL1 IS NOT NULL" +
"and (address.ADR_FIRSTNAME IS NOT NULL" +
"or address.ADR_LASTNAME IS NOT NULL)", connection);
}
我感谢任何帮助! 如果还有其他需要的信息,请告诉我。
答案 0 :(得分:4)
address.ADR_ID" + "from
结果为:address.ADR_IDfrom
您应该在查询的不同部分的末尾或开头添加空格。
喜欢这个::
address.ADR_ID " + "from
答案 1 :(得分:3)
你检查了空格吗?我认为命令的每一行都缺少一个空格
答案 2 :(得分:1)
像
这样的结构ADR_ID" + "from dbo.ADDRESS as address, dbo.EMPLOYEE as emp
会给你
ADR_IDfrom dbo.ADDRESS as address, dbo.EMPLOYEE as emp
其中 ADR_IDfrom 显然不是有效的SQL。您需要在每个字符串的末尾放置空格,以便形成有效的SQL 还要考虑将代码放入try块并检查异常。它可能对你有很大帮助。请参阅SqlError class。
答案 3 :(得分:1)
与其他提到的一样,缺少空格是你的问题。
编写查询的另一种方法是使用@
- 引用的字符串。使用它,您不必转义任何字符,并且可以轻松编写多行字符串:
SqlCommand importUser = new SqlCommand(
@"select address.ADR_FIRSTNAME, address.ADR_LASTNAME, address.ADR_EMAIL1, address.ADR_ID
from dbo.ADDRESS as address, dbo.EMPLOYEE as emp
where emp.EMP_ADR_ID = address.ADR_ID
and address.GCRecord IS NULL
and address.ADR_EMAIL1 IS NOT NULL
and (address.ADR_FIRSTNAME IS NOT NULL
or address.ADR_LASTNAME IS NOT NULL)", connection);
答案 4 :(得分:1)
你检查过这个空间。打开它有帮助