我正在从事一个项目,该项目需要我从 MS Access 数据库中获取最新数据,然后将数据存储到 Oracle 中的现有表中。
我几乎完成了这个项目;但是我有一个小问题:当编译器完成运行控制台应用程序时,oracle 表有一行,其中每个值现在都为空。
我已经盯着这个程序看了好几个小时了,但我一无所获。我想知道第一双眼睛是否可以帮助我解决这个问题。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using System.Data.OracleClient;
using System.Text;
using System.Data.SqlClient;
using System.IO;
using System.Data.Odbc;
namespace ConsoleApplication4
{
class Program2
{
static void Main(string[] args)
{
string connectionString = "Dsn=Gas_meter";
string col0 = "";
string col1 = "";
string col2 = "";
string col3 = "";
string col4 = "";
string col5 = "";
string col6 = "";
string col7 = "";
string col8 = "";
这将建立与 MS Access 的连接并从表中获取最新数据
OdbcConnection DbConnection = new OdbcConnection(connectionString);
OdbcCommand DbCommand = DbConnection.CreateCommand();
DbConnection.Open();
DbCommand.CommandText = "SELECT DateTime, S1Flow, S2Flow, S3Flow, S4Flow, S1FlowTotal, S2FlowTotal, S3FlowTotal, S4FlowTotal FROM CommonStation WHERE Format(DateTime, 'mm/dd/yyyy') >=(select Format(max(DateTime),'mm/dd/yyyy') from CommonStation)";
DbCommand.ExecuteNonQuery();
OdbcDataReader DbReader = DbCommand.ExecuteReader();
这部分将字段名输出到控制台窗口。这个和下面的 Console.WriteLine () 命令对我来说是一种健全性检查,以确保它抓取了我正在寻找的所有数据。
int fCount = DbReader.FieldCount;
Console.Write("");
for (int i = 0; i < fCount; i++)
{
String fName = DbReader.GetName(i);
Console.Write(fName + "\t");
}
Console.WriteLine();
这部分将数据发送到 Oracle 表中。这里再次有一个 Console.WriteLine() 命令,用于检查来自 MS 的信息是否访问了我想要的内容。
try
{
while (DbReader.Read())
{
string connString = "DSN=Gas_meter_proj;Uid=cm;Pwd=cmdev123";
OdbcConnection conn = new OdbcConnection(connString);
string sqlins = @"insert into Commonstation(CommStatDate_Time, S1_Flow, S2_Flow, S3_Flow, S4_Flow, S1_Flow_Total, S2_Flow_Total, S3_Flow_Total, S4_Flow_Total ) values (to_date('" +col0+"', 'MM/DD/YYYY HH:MI:SS AM' ),to_number('" + col1 + "'), to_number('" + col2 + "'), to_number('" + col3 + "'), to_number('" + col4 + "'),to_number('" + col5 + "'),to_number('" + col6 + "'),to_number('" + col7 + "'),to_number('" + col8 + "'))";
OdbcCommand cmdnon = new OdbcCommand(sqlins, conn);
cmdnon.Parameters.Add(col0, OdbcType.DateTime);
cmdnon.Parameters.Add(col1, OdbcType.Int);
cmdnon.Parameters.Add(col2, OdbcType.Int);
cmdnon.Parameters.Add(col3, OdbcType.Int);
cmdnon.Parameters.Add(col4, OdbcType.Int);
cmdnon.Parameters.Add(col5, OdbcType.Int);
cmdnon.Parameters.Add(col6, OdbcType.Int);
cmdnon.Parameters.Add(col7, OdbcType.Int);
cmdnon.Parameters.Add(col8, OdbcType.Int);
conn.Open();
col0 = DbReader["DateTime"].ToString();
col1 = DbReader["S1Flow"].ToString();
col2 = DbReader["S2Flow"].ToString();
col3 = DbReader["S3Flow"].ToString();
col4 = DbReader["S4Flow"].ToString();
col5 = DbReader["S1FlowTotal"].ToString();
col6 = DbReader["S2FlowTotal"].ToString();
col7 = DbReader["S3FlowTotal"].ToString();
col8 = DbReader["S4FlowTotal"].ToString();
Console.Write(col0 + "\t");
Console.Write(col1 + "\t");
Console.Write(col2 + "\t");
Console.Write(col3 + "\t");
Console.Write(col4 + "\t");
Console.Write(col5 + "\t");
Console.Write(col6 + "\t");
Console.Write(col7 + "\t");
Console.Write(col8 + "\t");
int rowsAffected = cmdnon.ExecuteNonQuery();
Console.WriteLine();
conn.Close();
Console.WriteLine(rowsAffected);
}
如果在运行程序时出现一般性错误,我会给出一个关于它是什么以及它来自哪里的一般解释。
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
finally
{
DbReader.Close();
DbCommand.Dispose();
DbConnection.Close();
}
}
}
}
同样,我从 MS Access 获取了所有信息,看起来我正在获取所有数据,但行中填充了空值。有人可以帮助我了解这里发生了什么吗?