![](/img/trans.png)
[英]How to convert DateTime to string in C# using Oracle date format
[英]Casting a C# String to a date type that matches Oracle;s date format
我正在尝试编写通过C#/ ASP.NET程序执行的Oracle SQL查询,以将数据插入Oracle Db。 我在查找C#方法时遇到问题,该方法将转换字符串(来自表单的用户输入,格式为11/11/2012)以匹配Oracle的日期数据类型。 我试图通过Convert.ToDateTime(object)方法进行转换,但没有成功。
这是我的代码:
<%@ Page Language="C#" Debug="true" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.OleDb" %>
<script runat="server">
void Page_Load(Object sender, EventArgs e) {
//Get user's input form form fields
var saleID = Convert.ToInt32(Request.Form["saleID_aspx"]);
var custID = Convert.ToInt32(Request.Form["custID_aspx"]);
var agentID = Convert.ToInt32(Request.Form["agentID_aspx"]);
var saleDate = Convert.ToDateTime("11/11/2012"); //Originally Request.Form["saleDate_aspx"]
var homeID = Convert.ToInt32(Request.Form["homeID_aspx"]);
var actualAmount = Convert.ToInt32(Request.Form["actualAmount_aspx"]);
var contractID = Convert.ToInt32(Request.Form["contractID_aspx"]);
var valueCommand = "VALUES(" + saleID + "," + custID + "," + agentID + "," + saleDate + "," + contractID + "," + homeID + "," + actualAmount + ")";
// Declaration section
OleDbConnection objDBConn;
OleDbCommand objCmd;
OleDbCommand objCmdSelect;
OleDbDataReader objDR;
// Set up OLE DB Connection object
objDBConn = new OleDbConnection("Provider=*****1;" +
"User ID=********;" +
"Password=*******;" +
"Data Source=****");
// Open DB connection
objDBConn.Open();
// Create OleDbCommand object with SQL to execute
objCmd = new OleDbCommand("INSERT INTO Sale (saleID, cust_ID, agent_ID, saleDate, contractID, homeID, actualamount)" +
valueCommand, objDBConn);
// Create a DataReader and execute the command
objDR = objCmd.ExecuteReader();
// Copy results from DataReader to DataGrid object
gridCusts.DataSource = objDR;
gridCusts.DataBind();
// Close all objects
objDR.Close();
objCmd.Dispose();
////////////////////////////////////////////////////////////
// Create OleDbCommand object with SQL to execute
objCmdSelect = new OleDbCommand("SELECT * " +
" FROM Sale " +
" ORDER BY saleID", objDBConn);
// Create a DataReader and execute the command
objDR = objCmdSelect.ExecuteReader();
// Copy results from DataReader to DataGrid object
gridCusts.DataSource = objDR;
gridCusts.DataBind();
// Close all objects
objDR.Close();
objCmdSelect.Dispose();
///////////////////////////////////////////////////////////
objDBConn.Close();
}
</script>
<html>
<head>
<title>CUSTOMERS table</title>
<link href="bootstrap/css/bootstrap.css" type="text/css" rel="stylesheet">
</head>
<body>
<div id="container">
<h2>Oracle SALES table contents via C#.NET and OLE DB</h2>
<div style="margin:0 auto text-align:center;">
<asp:DataGrid id="gridCusts" class='table' runat="server" />
</div>
<a href="index.html" target="_self" class="btn">Go Back</a>
</div>
</body>
</html>
您可以简单地使用oracle函数to_date
。
var valueCommand = "VALUES(" + saleID + "," + custID + "," + agentID + ",
to_date('" + saleDate.ToString("MM/dd/yyyy") + "', 'mm/dd/yyyy')," + contractID + "," + homeID + "," + actualAmount + ")";
我已经在您的代码中看到许多导致程序失败的问题。
ExecuteNonQuery()
方法以执行SELECT
以外的SQL语句。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.