2011-06-27 58 views
0

我在看書的dBase數據庫,然後發送該表到SQL Server 2008現在閱讀dBASE表設計

我的問題是我怎麼能得到正確列式,現在我獲取所有字符類型的varchar(255)。

我知道我很久很久以前做過這件事,但不記得我是如何做到的。

出口從dBase的數據庫中的數據集:

public DataSet getDataSetFromDBF(string path, string fileName) 
{ 
     OdbcConnection conn = new OdbcConnection(@"Driver={Microsoft dBASE Driver (*.dbf)};DriverID=277"); 
     String query = @"SELECT * FROM " + @"C:\spcs\" + fileName.ToString() + ".dbf"; 
     try 
     { 
      OdbcCommand MyCommand = new OdbcCommand(query, conn); 
      OdbcDataAdapter adapter = new OdbcDataAdapter(MyCommand); 
      DataSet ds = new DataSet();     
      adapter.FillSchema(ds, SchemaType.Mapped); 
      conn.Open(); 
      adapter.Fill(ds); 
      return ds; 
     } 
     catch (Exception ex) 
     { 
      exceptionLog(ex); 
      return null 
     } 
} 

然後到MSSQL服務器:

private bool createSqlQueryFromDbfFile(string fileName) 
{ 
    StringBuilder sb = new StringBuilder(); 
    string query = ""; 
    string pathToDB = @"c:\spcs\"; 
    string tableName = fileName.Replace(".dbf", ""); 
    sb.Append("IF OBJECT_ID('" + tableName + "', 'U') IS NOT NULL " + Environment.NewLine); 
    sb.Append("DROP TABLE " + tableName + ";" + Environment.NewLine); 
    sb.Append("CREATE TABLE " + tableName + Environment.NewLine); 
    sb.Append("(" + Environment.NewLine); 
    webHallDB.DAL db = new webHallDB.DAL();   
    try 
    { 
     DataSet ds = db.getDataSetFromDBF(pathToDB, tableName); 

     int kolumner = ds.Tables[0].Columns.Count; 

     string[] kolumnNamn = new string[kolumner]; 
     bool primaryKeySet = false; 
     for (int i = 0; i < kolumner; i++) 
     { 
      if (primaryKeySet == false) 
      { 
       sb.Append("id int PRIMARY KEY IDENTITY," + Environment.NewLine); 
       primaryKeySet = true; 
      } 
      string kolumnTyp = getColumnType(ds.Tables[0].Columns[i]); 
      // Sista kolumnen 
      if (i == kolumner - 1) 
      { 

       kolumnNamn[i] = ds.Tables[0].Columns[i].ColumnName; 
       sb.Append(ds.Tables[0].Columns[i].ColumnName + " " + kolumnTyp + ");" + Environment.NewLine); 
      } 
      else 
      { 
       kolumnNamn[i] = ds.Tables[0].Columns[i].ColumnName; 
       sb.Append(ds.Tables[0].Columns[i].ColumnName + " " + kolumnTyp + "," + Environment.NewLine); 
      } 
     }    

     int rader = ds.Tables[0].Rows.Count; 
     DateTime startTid = DateTime.Now; 

     StringBuilder sbInsert = new StringBuilder(); 

     for (int row = 0; row < rader; row++) 
     { 
      sbInsert.Append(Environment.NewLine + "Insert Into " + tableName); 

      sbInsert.Append(Environment.NewLine + " Values ("); 
      for (int col = 0; col < kolumner; col++) 
      { 
       if (col == (kolumner - 1)) 
       { 
        sbInsert.Append("'" + changeSpecialCharacters(ds.Tables[0].Rows[row][col].ToString()) + "');"); 
       } 
       else 
       { 
        sbInsert.Append("'" + changeSpecialCharacters(ds.Tables[0].Rows[row][col].ToString()) + "', "); 
       } 
      } 
     } 

     query = sb.ToString(); 
     string insertQuery = sbInsert.ToString(); 

     DateTime slutTid = DateTime.Now; 

     db.executeQuery(query); 
     if (db.executeQuery(insertQuery)) 
     { 
      slutTid = DateTime.Now; 
      logToGui("Tid för att köra query: " + (slutTid - startTid).TotalSeconds); 
      return true; 
     } 
     else 
     { 
      slutTid = DateTime.Now; 
      logToGui("Tid för att köra query: (failed)" + (slutTid - startTid).TotalSeconds); 
      return false; 
     } 
    } 
    catch (Exception ex) 
    { 
     logToGui("createSqlQueryFromDbfFile misslyckades!"); 
     logToGui(ex.Message); 
     return false; 
    } 
} 

更新:我設法弄明白:

adapter.FillSchema(ds, SchemaType.Mapped); 

然後我能夠使用DataColumn.MaxLength

+1

您應該提供您的解決方案作爲答案和自我接受。 – spender

+0

請做什麼花費者說。這是SO的規則 – abatishchev

回答

2

我設法弄明白:

adapter.FillSchema(ds, SchemaType.Mapped); 

然後我能夠使用DataColumn.MaxLength。