How to insert date in an Oracle relational database using C#

你说的曾经没有我的故事 提交于 2019-12-13 09:39:34

问题


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 ?


回答1:


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

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

cmd.ExecuteNonQuery();



回答2:


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




回答3:


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'));



回答4:


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




回答5:


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."




回答6:


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.




回答7:


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


来源:https://stackoverflow.com/questions/698339/how-to-insert-date-in-an-oracle-relational-database-using-c-sharp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!