如何在VB.Net中转换日期?我将字符串中的日期转换为日期时出现错误。 [英] How to Convert Date in VB.Net? I got The Error When I Convert The date in string to date.

查看:83
本文介绍了如何在VB.Net中转换日期?我将字符串中的日期转换为日期时出现错误。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Imports System.Data.SqlClient
Imports System.Data
Imports System.Web



我收到此错误


I Got This Error

"Conversion failed when converting date and/or time from character string."





这是我的VB代码页面

和数据类型的日期是日期。





This Is My Code of VB page
and Data-type of Date is "Date".

Partial Class _Default
    Inherits System.Web.UI.Page
    Public cmd As New SqlCommand
    'Dim time As DateTime = Convert.ToDateTime(txtDate.Text)


    Public con As New SqlConnection("Data Source=.\SQLExpress;Initial Catalog=sample;Integrated Security=True")

    Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
        con.Open()
        Dim cmd As New SqlCommand("insert into tbl_emp1 values('" + txtName.Text + "','" + Convert.ToDateTime(txtDate.Text).ToShortDateString() + "')", con)
        cmd.ExecuteNonQuery()
        Response.Redirect("Default.aspx")

    End Sub
End Class

推荐答案

请不要这样做。

不要连接字符串以构建SQL命令。它让您对意外或故意的SQL注入攻击持开放态度,这可能会破坏您的整个数据库。请改用参数化查询。它将有助于保护您的数据库,同时解决您的问题...

并且,请列出您要插入数据的字段 - 这样可以更容易阅读和修改以后的事情,可以解决你的数据库设计的一些问题。



Please don't do that.
Do not concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead. It will help preserve your database, and cure your problem at the same time...
And, please, list teh fields you wan tto insert teh data into - it makes it easier to read, and modify things later, and can get rid of some more problems with your DB design.

Dim cmd As New SqlCommand("INSERT INTO tbl_emp1 (userName, dateEnter) VALUES(@NM, @DT)", con)
cmd.Parameters.AddWithValue("@NM", txtName.Text)
cmd.Parameters.AddWithValue("@DT", Convert.ToDateTime(txtDate.Text))
cmd.ExecuteNonQuery()



但是我建议您检查并转换方法顶部的日期并向用户inste报告任何问题尝试插入任何内容的广告...看看 DateTime.TryParse [ ^ ] - 这意味着如果用户输入4月31日,你的应用程序不会崩溃...


But I would suggest that you should check and convert the date at the top of the method and report any problems to the user instead of trying to insert anything...Have a look at DateTime.TryParse[^] - it means your app doesn't crash if the user enters 31st April for example...


首先,不要使用内联查询,它们会导致Sql注射攻击。而是使用参数化查询,您可以更好地控制数据及其类型。



读取 - 使用参数化的查询来防止SQL SERVER中的SQL注入攻击 [ ^ ]



来你的问题...

First of all, don't use inline queries, they lead to Sql Injection attack. Rather use Parameterized query, where you will have more control over Data and their Types.

Read- USING PARAMETERIZED QUERIES TO PREVENT SQL INJECTION ATTACKS IN SQL SERVER[^]

Coming to your problem...
Dim cmd As New SqlCommand("insert into tbl_emp1 values('" + txtName.Text + "','" + Convert.ToDateTime(txtDate.Text).ToShortDateString() + "')", con)



这里传递的是 String 而不是日期。由于该列的 DataType 在数据库中是 Date ,因此它会抛出异常。


Here you are passing String instead of Date. As the DataType of the column is Date in database, so it throwing exception.


这篇关于如何在VB.Net中转换日期?我将字符串中的日期转换为日期时出现错误。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆