如何使用VB.NET 2015在SQL数据库中保存日期 [英] How do I save date in SQL database using VB.NET 2015

查看:83
本文介绍了如何使用VB.NET 2015在SQL数据库中保存日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在使用vb.net 2015和SQL server 2012创建应用程序,操作系统赢了10,

所以我的电脑日期格式为dd / MM / yyyy,当我在日期测试时小于13 / MM / yyyy - 保存良好但如果日期超过13我收到错误消息(

类型的未处理异常< span class =code-string>'  System.Data.SqlClient.SqlException'  in  System.Data.dll 

附加信息:转换失败 转换日期和/或时间来自字符字符串





任何人都可以帮助我。

提前致谢



我尝试过:



这是我试过的代码

Dim DY As Integer =(dat).ToString(dd)

If Not DY> 12然后

dat =格式(dat,yyyy-dd-MM)

Else

dat = CDate(Me.TxtJVDate.Text).ToString(dd / MM / yyyy )'格式(dat,yyyy-MM-dd)

结束如果

如果Isadding = True那么

将NewVoucherNumber调暗为字符串= GiveNewID(JV,BRID)

SQLString =INSERT INTO JV_Hed(JV_No,JV_Date,J_Month,JV_Year,JV_Source,Dr,Cr,JV_Status,JV_Br,Comments)&

values('&修剪(NewVoucherNumber)& ','& CDate(dat)& ','& CurMonth& ','& CurYear& ','JV',& Val(Me.TxtDeb.Text)&

,& Val(Me.TxtCr.Text)& ,'错','& BRID& ','& Me.TxtComm.Text& ')



SaveInto.CommandText = SQLString

conn.Open()

SaveInto.ExecuteNonQuery( )

SaveInto.CommandText =UPDATE AutoNumbers SET counter ='& CurNo& '其中P ='JV'和Branch ='& BRID& '

SaveInto.ExecuteNonQuery()

conn.Close()

解决方案

不要这样做!不要连接字符串来构建一个SQL命令。它会让您对意外或故意的SQL注入攻击持开放态度,这可能会破坏整个数据库。使用参数化查询代替,并以本机格式传递值,而不是所以整数值作为整数参数,日期作为DateTime,等等。

这也将同时修复你的问题。

 使用 con 作为  SqlConnection (strConnect)
con.Open()
使用 com 作为 SqlCommand( INSERT INTO myTable(myIntegerColumn,myStrin) gColumn,myDateColumn)VALUES(@ MI,@ MSC,@ MDC),con)
com.Parameters.AddWithValue( @ MIC 666
com.Parameters.AddWithValue( @ MSC 我对Column2的价值
com.Parameters.AddWithValue( @ MDC,DateTime.Now)
com.ExecuteNonQuery()
结束 使用
结束 使用


最好的方法是在整个应用程序中使用通用的不变格式。

用户yyyy-MM-dd并查看问题是否仍然存在存在。



注意:我怀疑,你会b以 dd-MM-yyyy 格式发送值时,获取超过12(而非13)的错误。



如果我遗失这里的东西,请告诉我。



谢谢:)



更新:

请参考OriginalGriff的解决方案1,这是在应用程序中处理日期时间值的完美方式。


OriginalGriff非常感谢它工作正常( :

Hi,
I am creating an application using vb.net 2015 and SQL server 2012, the OS is win 10,
So my pc date format is dd/MM/yyyy, when I am testing it in date less than 13/MM/yyyy - saving good but if the date is more than 13 I am getting an error message (

An unhandled exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll

Additional information: Conversion failed when converting date and/or time from character string.

)

can anyone help me.
Thanks in advance

What I have tried:

This is the code I tried
"Dim DY As Integer = (dat).ToString("dd")
If Not DY > 12 Then
dat = Format(dat, "yyyy-dd-MM")
Else
dat = CDate(Me.TxtJVDate.Text).ToString("dd/MM/yyyy") 'Format(dat, "yyyy-MM-dd")
End If
If Isadding = True Then
Dim NewVoucherNumber As String = GiveNewID("JV", BRID)
SQLString = "INSERT INTO JV_Hed(JV_No,JV_Date,J_Month,JV_Year,JV_Source,Dr,Cr,JV_Status,JV_Br,Comments)" &
" values('" & Trim(NewVoucherNumber) & "','" & CDate(dat) & "','" & CurMonth & "','" & CurYear & "','JV'," & Val(Me.TxtDeb.Text) &
"," & Val(Me.TxtCr.Text) & ",'False','" & BRID & "','" & Me.TxtComm.Text & "')"

SaveInto.CommandText = SQLString
conn.Open()
SaveInto.ExecuteNonQuery()
SaveInto.CommandText = "UPDATE AutoNumbers SET counter = '" & CurNo & "' where P = 'JV' and Branch = '" & BRID & "'"
SaveInto.ExecuteNonQuery()
conn.Close()"

解决方案

Don't do it like 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, and pass values in "native" format, not as strings. So integer values go as integer parameters, dates as DateTime, and so forth.
That will also fix your problem at the same time.

Using con As New SqlConnection(strConnect)
    con.Open()
    Using com As New SqlCommand("INSERT INTO myTable (myIntegerColumn, myStringColumn, myDateColumn) VALUES (@MIC, @MSC, @MDC)", con)
        com.Parameters.AddWithValue("@MIC", 666)
        com.Parameters.AddWithValue("@MSC", "my Value For Column2")
        com.Parameters.AddWithValue("@MDC", DateTime.Now)
        com.ExecuteNonQuery()
    End Using
End Using


The best way is to use the a common invariant format throughout the application.
User "yyyy-MM-dd" and see if the problem still exists.

Note: I suspect, you will be getting error for value more than 12 (not 13) while sending values in dd-MM-yyyy format.

If I am missing something here, please let me know.

Thanks :)

UPDATE:
Please refer to the solution 1 by OriginalGriff which is the perfect way to handle datetime values in application.


OriginalGriff Thanks a lot it is working fine (:


这篇关于如何使用VB.NET 2015在SQL数据库中保存日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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