将日期插入SQL Server的问题。 Vb.net [英] Problem with inserting date into SQL server . Vb.net
问题描述
您好;
我正在尝试使用vb.net将日期值插入Sql server DB。
这里是sql命令
Hello ;
I'm trying to insert date value to Sql server DB by using vb.net .
here is the sql command
"INSERT INTO [Orders]([ProductID],[CustomerID],[EmployeeID],[OrderDate],[Quantitiy],BillNuber) VALUES (" & ProductID & "," & CustomerID & "," & EmployeeID & "," & System.DateTime.Now.ToString((" yyyy-MM-dd")) & "," & Quantity & "," & billnumber & ")"
我还使用了Now.tostring(yyyy / MM / DD),Today.tostring ( YYYY / MM / DD)
问题当我使用now()错误信息弹出错误'2'附近
即使没有错误,当我使用today()或system.datetime时,当我检查数据库时,日期看起来像1900-01-01 00:00:00.000
你能不能帮助我?
我尝试了什么:
now()
今天()
Now.tostring(yyyy / MM / DD)
Today.tostring(yyyy / MM / DD )
I also used Now.tostring("yyyy/MM/DD") , Today.tostring("yyyy/MM/DD")
the problem when i Use now() error message pops "error near '2'"
and even if there is no error and when i use today() or system.datetime , when i check the database the date look like "1900-01-01 00:00:00.000"
can you help me ?
What I have tried:
now()
today()
Now.tostring("yyyy/MM/DD")
Today.tostring("yyyy/MM/DD")
推荐答案
与SQL Server本地无关的通用日期格式是yyyy-MM-dd不要使用/作为分隔符。 />
同样以字符串形式添加日期周围的引号。
最好还是不要使用字符串连接而是使用SQLParameters:
将SQLParameters与VB.NET / C#一起使用 [ ^ ]
VB.Net中的sqlParameters数组 - 堆栈溢出 [ ^ ]
The generic date format which works regardless of the local for SQL Server is "yyyy-MM-dd" don't use / for a separator.
Also in string form add " quotes around the date.
Better still don't use string concatenation and use SQLParameters instead :
Using SQLParameters with VB.NET/C#[^]
sqlParameters Array in VB.Net - Stack Overflow[^]
这篇关于将日期插入SQL Server的问题。 Vb.net的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!