oracle 如何将 DateTimepicker 值转换为日期数据类型?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33796845/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 03:05:32  来源:igfitidea点击:

How to convert DateTimepicker value to date datatype?

c#oraclevisual-studiodatetimepicker

提问by Saketan Patil

My database is:

我的数据库是:

create table bus
( bus_no varchar(10),
  jdate date,
  source varchar(20),
  destination varchar(20),
  departtime varchar(10),
  primary key(bus_no,jdate));

C#

C#

c.connect();
comm = new OracleCommand(); /*Class OracleCommand  represents an SQL statement or stored procedure to execute against a database. OracleCommand() initializes a new instance of the OracleCommand. */
comm.Connection = c.conn;
//Console.Write(dateTimePicker1.Value.ToShortDateString());
comm.CommandText = "INSERT INTO bus VALUES ('" + busno.Text + "','" + dateTimePicker1.Value.ToShortDateString() + "','" + source.Text + "','" + destination.Text + "','" + departtime.Text + "')";
comm.CommandType = CommandType.Text;
MessageBox.Show("Bus Added");
comm.ExecuteNonQuery();
c.conn.Close();

It's showing error as month is not recognized. Please help.

由于无法识别月份,因此显示错误。请帮忙。

采纳答案by Neha Jain

you can use custom formatter to format it to correct format. Generally default short string of C# not work with Oracle.

您可以使用自定义格式化程序将其格式化为正确的格式。通常默认的 C# 短字符串不适用于 Oracle。

dateTimePicker1.Value.ToString("MM/dd/yyyy")

or

或者

dateTimePicker1.Value.ToString("M/d/yyyy")

回答by Stawros

You should never use concatenated SQL strings in your applications. Parameterized queries will be faster and help you to avoid SQL Injectionand troubles with type conversion.

永远不要在应用程序中使用串联的 SQL 字符串。参数化查询会更快,并帮助您避免SQL Injection类型转换的麻烦。

c.connect();
comm = new OracleCommand(); /*Class OracleCommand  represents an SQL statement or stored procedure to execute against a database. OracleCommand() initializes a new instance of the OracleCommand. */
comm.Connection = c.conn;
comm.CommandText = "INSERT INTO bus VALUES (:bus_no, :jdate, :source, :destination, :departtime)";
comm.Parameters.Add("bus_no",  busno.Text);
comm.Parameters.Add("jdate", dateTimePicker1.Value);
comm.Parameters.Add("source", source.Text);
comm.Parameters.Add("destination", destination.Text);
comm.Parameters.Add("departtime", departtime.Text);
comm.ExecuteNonQuery();
MessageBox.Show("Bus Added");
c.conn.Close();