将字符串解析为sql的datetime [英] Parsing string to datetime for sql

查看:64
本文介绍了将字符串解析为sql的datetime的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用各种代码将文本框中的字符串转换为SQL数据库的日期时间。这是我最近使用的:



< br /> 
Dim dtDate As DateTime< br />
< br />
dtDate = DateTime.ParseExact(txtDateFix.Text,yyyyMMdd,System.Globalization.CultureInfo.InvariantCulture)< br />
dtDate = DateTime.ParseExact(txtDateSite.Text, yyyyMMdd,System.Globalization.CultureInfo.InvariantCulture)





我之前使用过这个并且没有成功:





 txtDateFix.Text = dtDate.ToString(yyyyMMdd)< br /> 
txtDateSite.Text = dtDate .ToString(yyyyMMdd)





我在ASP.net项目中使用visual basic。



有些东西我不清楚 - 从我收集的东西,使用ParseExact将字符串转换为代码的一部分格式?如果有人在文本框中输入03/25/2014,如何修改此代码以便将其作为20140325插入数据库?我一直遇到一个错误,说日期与公历不兼容,这告诉我我的转换没有被处理。我不知道该怎么办。





谢谢!

解决方案

< blockquote>不要太担心格式太多 - 使用DateTime.TryParse转换用户设置文化版本,这是用户进入时可能会使用的版本!或者更好的是,使用Calendar控件或类似设置,以便用户不输入它,并直接返回DateTime值。



然后将其传递给SQL as参数化查询,直接使用DateTime值 - 不要将任何东西转换为字符串以发送到SQL,它理解日期时间和数字直接作为参数传递。

 使用 con 作为  SqlConnection(strConnect)
con.Open()
使用 com 作为 SqlCommand( INSERT INTO myTable(insertDate)VALUES(@DT) ,con)
com.Parameters.AddWithValue( @ DT,myDateTimeValue)
com.ExecuteNonQuery()
结束 使用
结束 使用

使用字符串意味着字符串连接来构建SQL命令,这非常非常危险 - 它允许SQL注入,这可能会损坏或破坏您的数据库。


你需要将输入日期时间文本转换为DateTime类型(使用DateTime.ParseExact);说它是 dt



那么你可以使用sql参数插入日期时间值,如下所示。无需再将其转换为字符串。

 cmd =  SqlCommand(  INSERT INTO TableName(datetimeColumn)VALUES(@inputDateTime),conn)
cmd.Parameters.AddWithValue(< span class =code-string> @ inputDateTime,dt)' < span class =code-comment> dt是您从输入文本转换的DateTime值
cmd.ExecuteNonQuery()


I have been using various code to convert a string in a textbox to datetime for an SQL database. This is what I recently used:

<br />
    Dim dtDate As DateTime<br />
<br />
dtDate = DateTime.ParseExact(txtDateFix.Text, "yyyyMMdd", System.Globalization.CultureInfo.InvariantCulture)<br />
       dtDate = DateTime.ParseExact(txtDateSite.Text, "yyyyMMdd", System.Globalization.CultureInfo.InvariantCulture)



I previously used this and it was unsuccessful:


txtDateFix.Text = dtDate.ToString("yyyyMMdd")<br />
   txtDateSite.Text = dtDate.ToString("yyyyMMdd")



I am using visual basic for an ASP.net project.

There is something I am unclear on-- from what I gather, using a ParseExact will convert the string to the format that is part of the code? If someone enters "03/25/2014" in the textbox, how do I modify this code so it inserts into the database as "20140325"? I keep running into an error that says the date is not compatible with the Gregorian calendar, which tells me my conversion isn't being handled. I'm not sure how to proceed.


Thanks!

解决方案

Don't worry about formats too much - use DateTime.TryParse to convert the user set culture version, which is what the user is likely to use when he enters it! Or better, use a Calendar control or similar so that the user doesn't type it, and which returns you a DateTime value directly.

Then pass it to SQL as a Parameterised query, using the DateTime value directly - don't convert anything to strings to send to SQL, it understands datetimes and numbers directly passes as parameters.

Using con As New SqlConnection(strConnect)
    con.Open()
    Using com As New SqlCommand("INSERT INTO myTable (insertDate) VALUES (@DT)", con)
        com.Parameters.AddWithValue("@DT", myDateTimeValue)
        com.ExecuteNonQuery()
    End Using
End Using

Using strings implies string concatenation to build an SQL command, and that is very, very dangerous - it allows SQL Injection which can damage or destroy your database.


you need to convert the input date time text to DateTime type ( using DateTime.ParseExact); say it as dt

then you can use sql parameter to insert date time value like below. no need to convert it again to a string.

cmd = New SqlCommand("INSERT INTO TableName(datetimeColumn) VALUES(@inputDateTime)", conn)
cmd.Parameters.AddWithValue("@inputDateTime", dt)' dt is DateTime value you converted from input text
cmd.ExecuteNonQuery()


这篇关于将字符串解析为sql的datetime的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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