为什么在将 date.today 提交到日期时间列时,sql server 会给出转换错误?

发布于 2024-08-25 17:34:33 字数 1059 浏览 4 评论 0原文

每次尝试向 sql server 提交日期值时,我都会收到转换错误。 sql server 中的列是日期时间,在 vb 中我使用 Date.today 传递给我的参数化查询。 我不断收到 sql 异常

从字符串转换日期时间时,

转换失败。这是代码

Public Sub ResetOrder(ByVal connectionString As String)
        Dim strSQL As String
        Dim cn As New SqlConnection(connectionString)
        cn.Open()
        strSQL = "DELETE Tasks WHERE ProjID = @ProjectID"
        Dim cmd As New SqlCommand(strSQL, cn)
        cmd.Parameters.AddWithValue("ProjectID", 5)
        cmd.ExecuteNonQuery()

        strSQL = "INSERT INTO Tasks (ProjID, DueDate, TaskName) VALUES " & _
        " (@ProjID, @TaskName, @DueDate)"
        Dim cmd2 As New SqlCommand(strSQL, cn)
        cmd2.CommandText = strSQL

        cmd2.Parameters.AddWithValue("ProjID", 5)
        cmd2.Parameters.AddWithValue("DueDate", Date.Today)
        cmd2.Parameters.AddWithValue("TaskName", "bob")
        cmd2.ExecuteNonQuery()
        cn.Close()
        DataGridView1.DataSource = ds.Projects
        DataGridView2.DataSource = ds.Tasks
    End Sub

任何想法将不胜感激。

I am getting a conversion error every time I try to submit a date value to sql server. The column in sql server is a datetime and in vb I'm using Date.today to pass to my parameterized query. I keep getting a sql exception

Conversion failed when converting datetime from character string.

Here's the code

Public Sub ResetOrder(ByVal connectionString As String)
        Dim strSQL As String
        Dim cn As New SqlConnection(connectionString)
        cn.Open()
        strSQL = "DELETE Tasks WHERE ProjID = @ProjectID"
        Dim cmd As New SqlCommand(strSQL, cn)
        cmd.Parameters.AddWithValue("ProjectID", 5)
        cmd.ExecuteNonQuery()

        strSQL = "INSERT INTO Tasks (ProjID, DueDate, TaskName) VALUES " & _
        " (@ProjID, @TaskName, @DueDate)"
        Dim cmd2 As New SqlCommand(strSQL, cn)
        cmd2.CommandText = strSQL

        cmd2.Parameters.AddWithValue("ProjID", 5)
        cmd2.Parameters.AddWithValue("DueDate", Date.Today)
        cmd2.Parameters.AddWithValue("TaskName", "bob")
        cmd2.ExecuteNonQuery()
        cn.Close()
        DataGridView1.DataSource = ds.Projects
        DataGridView2.DataSource = ds.Tasks
    End Sub

Any thoughts would be greatly appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

何止钟意 2024-09-01 17:34:33

尝试

DateTime.Now()

DateTime.Today;

代替

Date.Today

Try

DateTime.Now()

Or

DateTime.Today;

Instead of

Date.Today
折戟 2024-09-01 17:34:33

可能是应用程序和 SQL Server 之间的区域设置不同造成的问题。

尽管它不能解决根本问题,但避免这种情况的直接方法是显式格式化传递给 SQL 的日期

cmd2.Parameters.AddWithValue("DueDate", DateTime.Today.ToString("yyyyMMdd"));

编辑:
我更仔细地阅读了您的代码:您的参数和值的顺序不同。尝试:

strSQL = "INSERT INTO Tasks (ProjID, DueDate, TaskName) VALUES " & _
" (@ProjID, @DueDate, @TaskName)"

Could be an issue with different regional settings between your application and SQL server.

Although it doesn't resolve the underlying issue, a straight-forward way to avoid this is by explicitly formatting the date you pass to SQL

cmd2.Parameters.AddWithValue("DueDate", DateTime.Today.ToString("yyyyMMdd"));

EDIT:
I read your code more carefully: you've got your parameters and values in different orders. Try:

strSQL = "INSERT INTO Tasks (ProjID, DueDate, TaskName) VALUES " & _
" (@ProjID, @DueDate, @TaskName)"
星軌x 2024-09-01 17:34:33

参数顺序错误。哎哟!

@TaskName 和 @DueDate 已交换。我相信以上所有建议都是正确的。

strSQL = "INSERT INTO Tasks (ProjID, DueDate, TaskName) VALUES " & _         " (@ProjID, @TaskName, @DueDate)"       

Parameters were in the wrong order. Doh!

@TaskName and @DueDate were swapped. I believe all of the above suggestion are correct.

strSQL = "INSERT INTO Tasks (ProjID, DueDate, TaskName) VALUES " & _         " (@ProjID, @TaskName, @DueDate)"       
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文