“不是有效月份”将字符串插入Oracle日期字段时出错 [英] "not a valid month" error when inserting a string to Oracle date field

查看:130
本文介绍了“不是有效月份”将字符串插入Oracle日期字段时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下代码尝试将数据插入数据库。它们的价值来自3个针对3个文本框的日历控件。现在,当我尝试将textbox的值插入到我的oracle表中时,它的抛出不是有效的月份错误。



表的列类型是DB内的日期。

I am using the below code trying to insert data into database. the value for them comes from 3 calendar controls targeted to 3 textboxes. Now when i try to insert the value of textbox into my oracle table, its throwing not a valid month error.

The columns type of table is date inside the DB.

Dim Req_prod_date, Appr_prod_date, Cab_appr_date As DateTime

Req_prod_date = txtbx_req_prod_date.Text
Appr_prod_date = txtbx_pland_prod_date.Text
Cab_appr_date = txtbx_cab_apprd_date.Text

Qry = "Insert into shcemaname.tablename" & _
              "(RQU_PROD_DATE,APPR_PROD_DATE,CAB_APPR_DATE)" & _
              "values ('"Req_prod_date & "','" & Appr_prod_date & "', '" & Cab_appr_date & "')"
Dim com As OracleCommand = New OracleCommand(Qry, con)

Dim iInsert As Integer = 0
iInsert = com.ExecuteNonQuery()
con.Close()



请指教。在此先感谢


Please advise. Thanks in advance

推荐答案

最佳做法是尽可能使用参数(参见这个如何编程 [ ^ ])。

使用参数可以保护你免受 SQL注入 [ ^ ]。



现在关于数据库不接受字符串,你需要做的就是将字符串转换为VB代码中的日期时间,然后数据库肯定会使用它,这样你也可以在GUI中提醒用户格式是否错误等。



祝你好运,

Edo
Best practice is that you use parameters if you can (see this how to thread[^]).
Using parameters will protect you from SQL Injection[^].

Now regarding the DB not accepting the string, all you need to do is convert the string to Datetime in your VB code and then the DB is sure to work with it, this way you can also alert the user in the GUI if the format is wrong etc.

Good luck,
Edo


尝试使用日期格式插入dd-MMM-yyyy..

w hile target to textbox以dd-MMM-yyyy格式带来价值

就像这样2013年8月4日..



如果这些控件是DateTimePicker,则可以使用此插入查询

try using the date format for inserting as "dd-MMM-yyyy"..
while targetting to textbox bring the values in the format "dd-MMM-yyyy"
like this 04-Aug-2013..

if these control were DateTimePicker the you can use this insert query
Qry = "Insert into shcemaname.tablename" & _
              "(RQU_PROD_DATE,APPR_PROD_DATE,CAB_APPR_DATE)" & _
              "values ('"Req_prod_date.Value.ToString("dd-MMM-yyyy") & "','" & Appr_prod_date.Value.ToString("dd-MMM-yyyy") & "', '" & Cab_appr_date.Value.ToString("dd-MMM-yyyy") & "')"


这篇关于“不是有效月份”将字符串插入Oracle日期字段时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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