【发布时间】:2016-10-03 04:35:39
【问题描述】:
在我的电脑 (Win7) 上,该语句运行没有错误。如果我将 c# .exe 复制到程序最终应该运行的服务器(Win2012 服务器),那么我会收到错误
ORA-01843: 月份无效
我读取了一个 csv 文件并将其插入到带有语句的 oracle-db 中
command.CommandText = "INSERT INTO table (DATUM, ...) VALUES('" + dr[0].ToString() + "',..."')";
dr[0].ToString() 的值为 "01.06.2016"
DATUM 列是 oracle-db 中的 DATE 类型。
我用消息框调试了代码并得到了这个结果:
我看不出这两条语句有什么区别,当我执行int rowsupdated = command.ExecuteNonQuery();时,来自服务器的左边一条正在调用错误
我已经比较了区域设置,它们在两个系统上都是相同的(德语)。还有什么可能导致问题?谢谢
填写Datatable的部分(dr的来源):
StreamReader oStreamReader = new StreamReader(Zielverzeichnis + Dateiname, System.Text.Encoding.UTF8); //nach, für Umlaute
DataTable dtCSV_Import = null;
int RowCount = 0;
string[] ColumnNames = null;
string[] oStreamDataValues = null;
//using while loop read the stream data till end
while (!oStreamReader.EndOfStream)
{
String oStreamRowData = oStreamReader.ReadLine().Trim();
if (oStreamRowData.Length > 0)
{
oStreamDataValues = oStreamRowData.Split(';');
//Bcoz the first row contains column names, we will poluate
//the column name by
//reading the first row and RowCount-0 will be true only once
if (RowCount == 0)
{
RowCount = 1;
ColumnNames = oStreamRowData.Split(';');
dtCSV_Import = new DataTable();
//using foreach looping through all the column names
foreach (string csvcolumn in ColumnNames)
{
DataColumn oDataColumn = new DataColumn(csvcolumn.ToUpper(), typeof(string));
//setting the default value of empty.string to newly created column
oDataColumn.DefaultValue = string.Empty;
//adding the newly created column to the table
dtCSV_Import.Columns.Add(oDataColumn);
}
}
else
{
//creates a new DataRow with the same schema as of the oDataTable
DataRow oDataRow = dtCSV_Import.NewRow();
//using foreach looping through all the column names
//Prüfen was kleiner ist, Spalten aus XML oder tatsächliche Spalten in der CSV -> sonst Fehler [i]
if (oStreamDataValues.Length < ColumnNames.Length)
{
for (int i = 0; i < oStreamDataValues.Length; i++)
{
oDataRow[ColumnNames[i]] = oStreamDataValues[i] == null ? string.Empty : oStreamDataValues[i].ToString();
}
}
else
{
for (int i = 0; i < ColumnNames.Length; i++)
{
oDataRow[ColumnNames[i]] = oStreamDataValues[i] == null ? string.Empty : oStreamDataValues[i].ToString();
}
}
//adding the newly created row with data to the oDataTable
dtCSV_Import.Rows.Add(oDataRow);
}
}
}
//close the oStreamReader object
oStreamReader.Close();
//release all the resources used by the oStreamReader object
oStreamReader.Dispose();
【问题讨论】:
-
永远不要依赖隐式转换。这取决于用于会话的 NLS 设置。而是始终明确提供格式:to_date(stringval,'dd-mm-yyyy')
-
dr到底是什么?它是数据阅读器吗?而不是字符串连接,为什么不将其转换为DateTime(我假设它是object)并使用参数化查询添加它?为什么你用mysql标记? -
您确实也比较了两个系统上的表定义,不是吗?您介意在此处添加吗?
-
@katz 很好用。请考虑接受 MT0 的回答,除了 to_date 他还涵盖其他选项
-
为什么要存储
DATUM(日期)和ZEIT(时间)列?合并两者并将其全部存储在一个DATE列中 - 日期总是有一个时间部分。