我的数据库是:
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.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();
由于未识别月份,因此显示错误。请帮忙。
答案 0 :(得分:1)
您可以使用自定义格式化程序将其格式化为正确的格式。通常默认的C#短字符串不适用于Oracle。
dateTimePicker1.Value.ToString("MM/dd/yyyy")
或
dateTimePicker1.Value.ToString("M/d/yyyy")
答案 1 :(得分:0)
您绝不应在应用程序中使用连接的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();