2011-04-29 4 views
0

저는 MS Access 데이터베이스에서 가장 최근 데이터를 가져오고 Oracle의 기존 테이블에 데이터를 가져와야하는 프로젝트를 진행하고 있습니다.오라클에 보낼 때 데이터 대신 null 값 가져 오기

나는이 프로젝트를 거의 완료했다. 그러나 하나의 작은 문제가 있습니다 : 컴파일러 콘솔 응용 프로그램을 실행 완료되면 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 액세스 접속을 확립하고 테이블

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() 명령은 내가 찾고있는 모든 데이터를 gragb로 확인하는 일종의 온 전성 검사입니다.

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 액세스에서 정보의 전부를 얻을, 그리고 내가 모든 데이터를 받고 있습니다 만, null로 가득 행이 나타납니다. 누군가 내가 여기서 무슨 일이 일어나고 있는지 이해할 수있게 도와 줄 수 있습니까?

답변

0

1) 왜 ExecuteNonQuery를 호출 한 후 리더를 실행합니까? 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(); 
        } 
     } 
    } 
} 
+0

같은 일이 여전히 발생합니다. 심지어 for 루프가 같은 문제를 주석 처리 했음에도 불구하고 거기에 있습니다. Oracle에서 여전히 Null 행을 얻고 있습니다. ** for 루프는 데이터가 올바르게 정렬되어 있는지 확인할 수 있도록 필드 이름을 가져 오기위한 것입니다. ** –

+0

프로그램이 잘못되어있는 것을 이해하고 있다고 생각합니다. 나는 그것이 부주의하게 필드 이름을 갖는 행을 잡아서 데이터에 널 (null) 행을 던지는 이유라고 생각한다. 그러나 이것이 내가 원하는 모든 데이터를 테이블에 가지고 있지만 null 값으로 채워진 추가 행을 가질 것이라는 것을 의미하는 경우가 아닐 수 있습니다. –

0

좋아, 내가 문제를 해결 :

위 변경 한 후, 코드처럼 보일 것입니다. 난 그 코드가 오라클에 보내기 전에 값을 설정하지 않았다. 그래서 int로 값을 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(); 
     }   
    } 
    } 
} 
관련 문제