Visual Studio 2008 datetimepicker转oracle日期和时间戳
我试图将数据插入我的 Oracle 数据库,但出现“无效月份”错误: 看来我无法将表单的 datetimepicker 值转换为 oracle 日期或时间戳 (7) 请帮忙!
我的代码
dateTimePicker1.Format = DateTimePickerFormat.Custom;
dateTimePicker1.CustomFormat = " dd MM yyyy ";
string m = "insert into member(memberid,name,street,roadno,houseno,phoneno,joindate,sex) values(member_deptno.nextval,'" + a + "','" + b+ "','" + c + "','" + d + "','" + h + "','" + dateTimePicker1.Value.Date+ "','"+de+"')";
im trying to insert data into my oracle database but i am getting "invalid month" error as
it seems that i cant convert the datetimepicker value of my form into oracle date or timestamp(7) please help!
my code
dateTimePicker1.Format = DateTimePickerFormat.Custom;
dateTimePicker1.CustomFormat = " dd MM yyyy ";
string m = "insert into member(memberid,name,street,roadno,houseno,phoneno,joindate,sex) values(member_deptno.nextval,'" + a + "','" + b+ "','" + c + "','" + d + "','" + h + "','" + dateTimePicker1.Value.Date+ "','"+de+"')";
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
用户参数化的sql插入。
例如参数化选择查询
user parameterized sql insert.
e.g. parameterized select query
正如其他人提到的,参数是可行的方法,但我认为它们不能解决您的问题。
我假设您的代码片段中显示的 CustomFormat 是 Oracle 想要的格式。问题是,当您调用 dateTimePicker1.Value.Date 时,它会为您提供一个 DateTime 对象,并且由于您将其与字符串组合,所以它会执行 .ToString() 方法,从而产生不同的格式。您应该将格式字符串放入 .ToString() 中以控制输出。例子:
As others have mentioned parameters are the way to go but I don't think they'll solve your issue.
I assume the CustomFormat shown in your snippet is the format Oracle wants. The problem is that when you call
dateTimePicker1.Value.Date
it gives you a DateTime object and since you're combining it with a string it executes the .ToString() method which results in a different format. You should put your format string in the .ToString() to control the output. Example: