我编写了以下代码,通过邮寄或电子邮件过滤想要通讯的人:
Sub loadoverviewnewsletter()
Dim sql As String
Dim sql1 As String
If newslettermail = True Then
sql = "SELECT * FROM Donateurs WHERE 'Newsletter per e-mail' =-1"
sql1 = "SELECT * FROM Donateurs WHERE 'Newsletter per e-mail' =-1"
ElseIf newsletterpost = True Then
sql = "SELECT * FROM Donateurs WHERE 'Newsletter per post' =-1"
sql1 = "SELECT * FROM Donateurs WHERE 'Newsletter per post' =-1"
ElseIf newsletternew = True Then
sql = "SELECT * FROM Donateurs WHERE 'Newsletter per post' =0 AND 'Nieuwsbrief per e-mail' =0"
sql1 = "SELECT * FROM Donateurs WHERE 'Newsletter per post' =0 AND 'Nieuwsbrief per e-mail' =0"
Else
sql = "SELECT * FROM Donateurs"
sql1 = "SELECT * FROM Donateurs"
End If
Dim adapter As New MySqlClient.MySqlDataAdapter(sql, modConnectorHandler.ServerString)
Dim dt As New DataTable("newsletteroverview")
adapter.Fill(dt)
ShowDonateurs.datagrid.DataSource = dt
ShowDonateurs.ShowDialog()
Dim adapter1 As New MySqlClient.MySqlDataAdapter(sql1, modConnectorHandler.ServerString)
Dim cmd1 As New MySqlClient.MySqlCommand(sql1, modConnectorHandler.mysqlconnection)
modConnectorHandler.mysqlconnection.Open()
Dim myreader As MySqlClient.MySqlDataReader = cmd1.ExecuteReader
myreader.Read()
modConnectorHandler.mysqlconnection.Close()
OverviewNewsLetter.Close()
不幸的是,它返回一个空数据网格,而当我选择newsletternew选项时,它会显示完整的表格。 “每个电子邮件的新闻稿”和“每个帖子的新闻稿”字段是tinyint字段,其值可以是-1(表示在应用程序中检查)和0(meand unchecked)。
我真的不明白为什么它不起作用..我希望任何人都可以提供帮助。
答案 0 :(得分:1)
您需要使用反引号而不是列名的单引号。考虑这个WHERE
条件:
WHERE 'Newsletter per e-mail' =-1
它将字符串常量Newsletter per e-mail
与-1
进行比较。比较结果始终为false
。试试这个:
WHERE `Newsletter per e-mail` = -1
现在它指的是列名而不是字符串常量。
最后,尽可能避免需要反引号的列名(几乎所有时间)。如你所见,它们往往会导致麻烦。