【发布时间】:2015-09-06 07:59:00
【问题描述】:
我已将我的数据库从Access 转换为Sql,因为Sql 不接受format(),因此显示错误。
这是我的代码:
DefaultStr = "" &
"SELECT StudentAccount.Dated, StudentAccountS.StAdmNo, StudentAccountS.StClass, " &
"StudentAccountS.StName, StudentAccount.Perticular, StudentAccount.Amount,StudentAccount.Remark,StudentAccount.PayMode,TransactionID " &
"FROM (StudentAccount LEFT OUTER JOIN StudentAccountS ON StudentAccount.SSID = StudentAccountS.SSID) " &
"WHERE (StudentAccount.Debit) and (StudentAccount.Dated Between " &
"#" & Format(DateFrom, "MM/dd/yyyy") & "# AND #" & Format(DateTo, "MM/dd/yyyy") & "#)"
Select Case SIndex
Case 0
SelStr = " AND (StudentAccount.PayMode = '" & OptionStr & "') Order By StudentAccount.Dated"
Case 1
SelStr = " AND (StudentAccount.Perticular = '" & OptionStr & "') Order By StudentAccount.Dated"
Case 2, 3
SelStr = " AND (StudentAccount.TransType = '" & filterStr & "') Order By StudentAccount.Dated"
Case Else
SelStr = Nothing
End Select
Da = New SqlDataAdapter(DefaultStr & SelStr, Conn)
Ds = New DataSet
Da.Fill(Ds)
【问题讨论】:
-
在
mssql中,当您存储date(或datetime)值时,不要使用#(如Access),而是使用单引号。什么是DateTo和DateFrom,变量?如果他们使用这样的东西:'" & String.Format("{0:MM/dd/yyyy}", DateTo) & "' -
我还要补充一点,您不应该使用字符串连接来构建 sql 命令。使用参数化的 sql。看到这个stackoverflow.com/questions/306668/…
-
这也会抛出同样的错误
-
错误出现在这部分代码中:
WHERE (StudentAccount.Debit) and (StudentAccount.DatedStudentAccount.Debit 似乎不是布尔值,因此不能与and一起使用。