0

我希望将两个变量转换为日期。

我可以让 SQL 用于转换一个(第一个代码),但是当我想转换两个变量时,我不断收到错误(请参阅下面的两次尝试)。

我搜索了 cast double 和 cast two variables,但是 double 提出了 double 类型,而 variable 也有同样的问题。我应该寻找什么工作或者我需要什么“加入”来转换两个变量。

  • 一个演员(工作)

    cmd.CommandText = "SELECT datestart, DateEnd, runco​​mpleted FROM [tblweeklyreports] where Cast(datestart as date) between '" & vDateStart & "' and '" & vDateStart & "'"

- 两个演员(不工作)

 sSQL = "SELECT datestart, DateEnd, runcompleted FROM [tblweeklyreports] where Cast(moduledatestart as date, moduledatecomplete as date) between '" & vDateStart & "' and '" & vDateEnd & "'"

 sSQL = "SELECT datestart, DateEnd, runcompleted FROM [tblweeklyreports] where Cast(moduledatestart as date),(moduledatecomplete as date) between '" & vDateStart & "' and '" & vDateEnd & "'"

它必须先出现(我只需要一个链接,但我似乎无法搜索正确的措辞!)

------------------- 根据讨论修改版本 --------------

Dim cn As SqlConnection = New SqlConnection()
        Dim cmd As SqlCommand = New SqlCommand()
        Dim dr As SqlDataReader
        Dim vRunCompleted As String = ""
        Dim vDateStartExist As String = ""
        cn.ConnectionString = ConfigurationManager.ConnectionStrings("mySQLConnectionString").ConnectionString
        cmd.Connection = cn
        'Open the connection to the database
        cn.Open()

将 vDateStartDate 调暗为日期 = vDateStart 将 vDateEndDate 调暗为日期 = vDateEnd

        cmd.CommandText = "SELECT * FROM [tblresults] where moduledatestart between @from and @to"
        cmd.Parameters.AddWithValue("@from", vDateStart)
        cmd.Parameters.AddWithValue("@to", vDateEnd)

        Dim str As New StringBuilder
        Dim vdrOkay As String
        str.Append(Chr(34) & " Employee Id" & Chr(34) & "," & "Forename" & "," & Chr(34) & "Surname" & Chr(34) & "," & "Control 1" & "," & Chr(34) & "Control 2" & Chr(34) & "," & "Full code" & "," & "Class Name" & "," & "Year" & ",")
        str.Append(Chr(34) & " Number" & Chr(34) & "," & "Class Start Date" & "," & Chr(34) & "Class End Date" & Chr(34) & "," & "Course Type" & "," & Chr(34) & "Provider Type" & Chr(34) & "," & "Provider" & "," & "Venue" & "," & "Evaluation" & "," & "Duration Hrs" & ",")
        str.Replace(",", vbNewLine, str.Length - 1, 1) ' go to next line

        dr = cmd.ExecuteReader()

        If (dr.HasRows = True) Then

            Label1.Text = "running"

            While (dr.Read())

                vdrOkay = Trim(StrConv(dr("EmployeeID"), VbStrConv.Uppercase))
                str.Append("=" & Chr(34) & vdrOkay & Chr(34) & ",")

 End While

            Dim vFileName As String = Replace(vDateStart, "/", "_") & "_" & Replace(vDateEnd, "/", "_")
            My.Computer.FileSystem.WriteAllText(("C:\sites\Examples\WeeklyReport\" & "ELearning_Report_" & vFileName & ".csv"), str.ToString, False)

        Else
 Label1.Text = "did not run"
        End If
4

1 回答 1

4

如果您采用推荐的使用参数化查询的方式,则无需进行任何转换,这还具有避免 SQL 注入的良好附加效果。

在您的情况下,这可能如下所示:

cmd.CommandText = "SELECT datestart, DateEnd, runcompleted FROM [tblweeklyreports] where datestart between @from and @to"
cmd.Parameters.AddWithValue("@from", vDateStart)
cmd.Parameters.AddWithValue("@to", vDateEnd)

问题在于您构建WHERE子句的方式。您无法在尝试时一次性比较两个字段。你需要写这样的东西:

SELECT 
    datestart, 
    DateEnd, 
    runcompleted 
FROM [tblweeklyreports] 
where 
    moduledatestart between @from and @to and
    moduledatecomplete between @from and @to

然后如上使用 SQL 参数。

于 2013-10-16T13:27:57.143 回答