Visual Studio 2008 datetimepicker转oracle日期和时间戳

发布于 2024-08-14 21:40:30 字数 460 浏览 13 评论 0原文

我试图将数据插入我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

剪不断理还乱 2024-08-21 21:40:30

用户参数化的sql插入。

例如参数化选择查询

SqlConnection objConnection = new SqlConnection(connectionString);
objConnection.Open();
SqlCommand objCommand = new SqlCommand(
   "SELECT * FROM User WHERE Name = @Name AND Password = @Password",
   objConnection);
objCommand.Parameters.Add("@Name", tbName.Text);
objCommand.Parameters.Add("@Password", tbPassword.Text);
SqlDataReader objReader = objCommand.ExecuteReader();

user parameterized sql insert.

e.g. parameterized select query

SqlConnection objConnection = new SqlConnection(connectionString);
objConnection.Open();
SqlCommand objCommand = new SqlCommand(
   "SELECT * FROM User WHERE Name = @Name AND Password = @Password",
   objConnection);
objCommand.Parameters.Add("@Name", tbName.Text);
objCommand.Parameters.Add("@Password", tbPassword.Text);
SqlDataReader objReader = objCommand.ExecuteReader();
A君 2024-08-21 21:40:30

正如其他人提到的,参数是可行的方法,但我认为它们不能解决您的问题。

我假设您的代码片段中显示的 CustomFormat 是 Oracle 想要的格式。问题是,当您调用 dateTimePicker1.Value.Date 时,它​​会为您提供一个 DateTime 对象,并且由于您将其与字符串组合,所以它会执行 .ToString() 方法,从而产生不同的格式。您应该将格式字符串放入 .ToString() 中以控制输出。例子:

dateTimePicker1.Value.Date.ToString("dd MM yyyy");

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:

dateTimePicker1.Value.Date.ToString("dd MM yyyy");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文