发送到Oracle时获取空值代替数据

时间:2011-04-29 20:47:57

标签: null oracle8i

我正在开展一个项目,要求我从MS Access数据库中获取最新数据,然后将数据从Oracle中的现有表中获取。

我几乎完成了这个项目;但是我有一个小问题:当编译器运行完控制台应用程序时,oracle表有一行,其中每个值现在为null。

我一直盯着这个节目好几个小时,我无处可去。我想知道第一组眼睛是否可以帮助我解决这个问题。

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 Access的信息我想要的内容。

 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获得了所有信息,看起来我得到了所有数据,但是有行填充为null。有人可以帮我理解这里发生了什么吗?

2 个答案:

答案 0 :(得分:0)

1)为什么要调用ExecuteNonQuery然后执行reader?删除ExecuteNonQuery语句。

2)您的完整性检查消耗了所有行,并且当代码到达语句while (DbReader.Read())时,没有更多行要遍历。删除完整性检查。

完成上述更改后,您的代码应如下所示:

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 = "";
                        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(); //####THIS STATEMENT IS NOT REQUIRED. REMOVE IT
                        OdbcDataReader DbReader = DbCommand.ExecuteReader();
                        int fCount = DbReader.FieldCount;
                        Console.Write("");

                        //####THIS FOR LOOP WILL READ THRU ALL RECORDS. REMOVE IT
                        /*
                        for (int i = 0; i < fCount; i++)
                        {
                                String fName = DbReader.GetName(i);
                                Console.Write(fName + "\t");
                        }
                        */
                    Console.WriteLine();
                    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();
                    }
        }
    }
}

答案 1 :(得分:0)

好的,我解决了这个问题。在将代码发送到oracle之前,我没有让代码设置一个值,因此它最初将值设置为null,因为它是在代码开头设置的值。最终代码的这一部分现在可以正常工作。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.OracleClient;
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 = "";
      string sqlins = "";
      string connString = "DSN=Gas_meter_proj;Uid=cm;Pwd=cmdev123";
      OdbcConnection conn = new OdbcConnection(connString);
      OdbcCommand cmdnon = new OdbcCommand(sqlins, conn);
      conn.Open();
      cmdnon.Parameters.Add(col0, OdbcType.DateTime);
      cmdnon.Parameters.Add(col1, OdbcType.Numeric);
      cmdnon.Parameters.Add(col2, OdbcType.Numeric);
      cmdnon.Parameters.Add(col3, OdbcType.Numeric);
      cmdnon.Parameters.Add(col4, OdbcType.Numeric);
      cmdnon.Parameters.Add(col5, OdbcType.Numeric);
      cmdnon.Parameters.Add(col6, OdbcType.Numeric);
      cmdnon.Parameters.Add(col7, OdbcType.Numeric);
      cmdnon.Parameters.Add(col8, OdbcType.Numeric);

      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)";
      OdbcDataReader DbReader = DbCommand.ExecuteReader();
      int fCount = DbReader.FieldCount;
      Console.Write("");
      /*
        for (int i = 0; i < fCount; i++)
        {
          String fName = DbReader.GetName(i);
          Console.Write(fName + "\t");
        }
      */
      Console.WriteLine();
      try
      {
        while (DbReader.Read())
        {
          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();
          cmdnon.CommandText = "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 + "'))";
          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();
          Console.WriteLine(rowsAffected);
        }
      }
      catch (Exception ex)
      {
         Console.WriteLine(ex.ToString());
      }
      finally
      {
         conn2.Close();      
         DbReader.Close();
         DbCommand.Dispose();
         DbConnection.Close();
      }          
    }
  }
}