如何使用 C# 在 Oracle 关系数据库中插入日期

发布于 2024-07-16 06:29:44 字数 167 浏览 6 评论 0原文

我在 Oracle 中有 Date Var,当我尝试从 C# 程序插入数据时

sql = "insert into Table(MyDate) values (" + convert.todatetime(txt) + ")";

出现错误,我该怎么办?

I have Date Var in Oracle, and I try to insert Data from my C# program

sql = "insert into Table(MyDate) values (" + convert.todatetime(txt) + ")";

I get an Error, what can i do ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(7

小猫一只 2024-07-23 06:29:44
cmd.CommandText = "INSERT INTO Table (myDate)VALUES(:dateParam)";

cmd.Parameters.Add(new OracleParameter("dateParam", OracleDbType.Date))
    .Value = DateTime.Now;

cmd.ExecuteNonQuery();
cmd.CommandText = "INSERT INTO Table (myDate)VALUES(:dateParam)";

cmd.Parameters.Add(new OracleParameter("dateParam", OracleDbType.Date))
    .Value = DateTime.Now;

cmd.ExecuteNonQuery();
仲春光 2024-07-23 06:29:44

使用参数。 它将解决您的问题并防止注射。

Use parameters. It's going to solve your problem and prevent injection.

念﹏祤嫣 2024-07-23 06:29:44

Oracle 希望它是一个实际的日期值,而不仅仅是一个看起来像日期的字符串。 您必须使用 TO_DATE() 函数来解释字符串的格式,如下所示:

INSERT INTO Table (myDate)
VALUES(TO_DATE('2009-03-30 12:30:00', 'YYYY-MM-DD HH:mi:ss'));

Oracle expects it to be an actual date value, not just a string that looks like a date. You have to use the TO_DATE() function to explain how your string is formatted, something like this:

INSERT INTO Table (myDate)
VALUES(TO_DATE('2009-03-30 12:30:00', 'YYYY-MM-DD HH:mi:ss'));
假扮的天使 2024-07-23 06:29:44

尝试使用 DateTime.TryParse(text) 或 DateTime.Parse(text)

Try using DateTime.TryParse(text) or DateTime.Parse(text)

也只是曾经 2024-07-23 06:29:44

我知道这是一个提出得很糟糕的问题,但是当我遇到同样的问题并遇到这个问题时,我看到了一些糟糕的答案。 这就是我解决这个问题的方法,我将使用 OP 的上下文来回答:

将日期解析为 DateTime 变量:

DateTime myDate = DateTime.Parse(txt);

然后参数化您的查询:

sql = "insert into Table(MyDate) values (:myDate)";

设置一个 OracleParameter

OracleParameter param = new OracleParameter();
param.ParameterName = "myDate";
param.OracleDbType = OracleDbType.Date;
param.Value = myDate;

假设您已经有一个 OracleConnection 作为 连接,请设置您的命令并添加您的参数:

OracleCommand cmd = new OracleCommand(sql, connection);
cmd.Parameters.Add(param);

执行:

cmd.ExecuteNonQuery();

不要在任何TO_DATE废话。 这适用于直接使用 SQL*Plus 或 Oracle SQL Developer 添加内容时,或者可能希望以 TO_DATE 期望的精确格式发送 STRING 变量的值(不是 DateTime 变量)的情况并且您在查询或存储过程的 TO_DATE 构造中进行分配(即 to_date('2013-05-13 12:13:14', 'YYYY-MM-DD HH24: MI:SS')。使用 DateTime 变量并将其分配给 OracleDbTypeOracleDbType 的 OracleParameter。 Date,假设您的表中有一个 DATE 字段,并且可以将 txt 解析为 DateTime 变量,但是,最好是最简单的。

I know this was a poorly asked question, but I saw some poor answers when I had the same question and ran into this. This is how I solved it, and I'll answer using the OP's context:

Parse the date in to a DateTime variable:

DateTime myDate = DateTime.Parse(txt);

Then parameterize your query:

sql = "insert into Table(MyDate) values (:myDate)";

Set up an OracleParameter:

OracleParameter param = new OracleParameter();
param.ParameterName = "myDate";
param.OracleDbType = OracleDbType.Date;
param.Value = myDate;

Assuming you already have an OracleConnection as connection, set up your command and add your parameter:

OracleCommand cmd = new OracleCommand(sql, connection);
cmd.Parameters.Add(param);

Execute:

cmd.ExecuteNonQuery();

Do NOT waste your time on any of the TO_DATE nonsense. This is for when you are adding something using SQL*Plus or Oracle SQL Developer directly, or MAYBE where you want to send in a STRING variable's value (not a DateTime variable) in the EXACT format that TO_DATE expects and that you assign within the TO_DATE construct within your query or a stored procedure (i.e. to_date('2013-05-13 12:13:14', 'YYYY-MM-DD HH24:MI:SS'). Using a DateTime variable and assigning that to an OracleParameter with an OracleDbType of OracleDbType.Date, assuming you have a DATE field in your table and can parse txt into a DateTime variable, however, is best and easiest.

囍孤女 2024-07-23 06:29:44

最简单的方法:

DateTime inputDate = Convert.ToDateTime("01/01/2019"); //<---Input Sample Date in format

string queryParameters = String.Format("SELECT * FROM TABLE WHERE DATE = '{0}')", inputDate.ToString("dd-MMM-yyyy")); //<-- Converts System.DateTime into Oracle DateTime

//Forget looking anywhere else for an answer, copy and paste and reform this very code 
//and see the results

Easiest way possible:

DateTime inputDate = Convert.ToDateTime("01/01/2019"); //<---Input Sample Date in format

string queryParameters = String.Format("SELECT * FROM TABLE WHERE DATE = '{0}')", inputDate.ToString("dd-MMM-yyyy")); //<-- Converts System.DateTime into Oracle DateTime

//Forget looking anywhere else for an answer, copy and paste and reform this very code 
//and see the results
贩梦商人 2024-07-23 06:29:44

请绑定你的变量(就像 ocdecio 告诉的那样)! 它不仅可以防止 sql 注入,而且速度也更快。 尤其是在多并发的情况下。 阅读此处的示例: http://download .oracle.com/docs/cd/B28359_01/appdev.111/b28844/building_odp.htm#CEGCGDAB

“绑定变量是SQL语句中的占位符。当数据库收到一条SQL语句时,它会判断该语句是否已经被执行并存储在内存中。如果该语句确实存在于内存中,Oracle数据库可以重用它并跳过该任务解析和优化语句使用绑定变量使语句可以重用不同的输入值。使用绑定变量还可以提高数据库中的查询性能,无需对输入中的文字引号进行特殊处理,并防止 SQL 注入攻击。”

Please bind your variables (like ocdecio tells) ! Not only does it prevent sql injection it is also much faster. Especially in a multi concurrency situation. Read for example here: http://download.oracle.com/docs/cd/B28359_01/appdev.111/b28844/building_odp.htm#CEGCGDAB .

"Bind variables are placeholders inside a SQL statement. When a database receives a SQL statement, it determines if the statement has already been executed and stored in memory. If the statement does exist in memory, Oracle Database can reuse it and skip the task of parsing and optimizing the statement. Using bind variables makes the statement reusable with different input values. Using bind variables also improves query performance in the database, eliminates the need for special handling of literal quotation marks in the input, and protects against SQL injection attacks."

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