2012-02-08 2 views
0

OLEDB를 사용하여 기존 Excel 워크 시트에 삽입하고 있습니다. 하지만 특정 순서로 삽입하고 싶습니다.ORDER on INSERT TO

File.Copy(lTemplateFolder + lFilename, lDistributorFolder + lFilename, true); 
    string lConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + lDistributorFolder + "\\" + lFilename + ";Extended Properties=\"Excel 8.0;HDR=YES;\""; 
    DbProviderFactory lFactory = DbProviderFactories.GetFactory("System.Data.OleDb"); 
    int lSequence = 0; 

    using (DbConnection lConnection = lFactory.CreateConnection()) 
    { 
     lConnection.ConnectionString = lConnectionString; 
     lConnection.Open(); 

     foreach (DataRowView rowView in dv) 
     { 
      DataRow row = rowView.Row; 

      lSequence++; 

      using (DbCommand lCommand = lConnection.CreateCommand()) 
      { 
       lCommand.CommandText = "INSERT INTO [Sheet1$]"; 
       lCommand.CommandText += "([First Name],[Last Name],[Title],[Company],[Address],[Address 2],[City],[State],[Zip],[Country],[Work phone],[Email],[Website],[Stamp Time],[Campaign],[Source],[Business Unit],[Market Segment],[Notes]) "; 
       lCommand.CommandText += "VALUES("; 
       lCommand.CommandText += "\"" + row["name"].ToString().Replace(" ", " ") + "\","; 
       lCommand.CommandText += "\"" + row["lastname"].ToString().Replace(" ", " ") + "\","; 
       lCommand.CommandText += "\"" + row["title"].ToString().Replace(" ", " ") + "\","; 
       lCommand.CommandText += "\"" + row["company"].ToString().Replace("\"", "\"\"").Replace(" ", " ") + "\","; 
       lCommand.CommandText += "\"" + row["address"].ToString().Replace("\"", "\"\"").Replace(" ", " ") + "\","; 
       lCommand.CommandText += "\"" + row["address2"].ToString().Replace("\"", "\"\"").Replace(" ", " ") + "\","; 
       lCommand.CommandText += "\"" + row["city"].ToString().Replace("\"", "\"\"").Replace(" ", " ") + "\","; 
       lCommand.CommandText += "\"" + row["state"].ToString().Replace("\"", "\"\"").Replace(" ", " ") + "\","; 
       lCommand.CommandText += "\"" + row["zip"].ToString().Replace("\"", "\"\"").Replace(" ", " ") + "\","; 
       lCommand.CommandText += "\"" + row["country"].ToString().Replace("\"", "\"\"").Replace(" ", " ") + "\","; 
       lCommand.CommandText += "\"" + row["workphone"].ToString() + "\","; 
       lCommand.CommandText += "\"" + row["email"].ToString().Replace("\"", "\"\"").Replace(" ", " ") + "\","; 
       lCommand.CommandText += "\"" + row["website"].ToString().Replace("\"", "\"\"").Replace(" ", " ") + "\","; 
       lCommand.CommandText += "\"" + row["stamptime"].ToString() + "\","; 
       lCommand.CommandText += "\"" + row["campaign"].ToString().Replace("\"","\"\"").Replace(" ", " ") + "\","; 
       lCommand.CommandText += "\"" + row["source"].ToString().Replace("\"", "\"\"").Replace(" ", " ") + "\","; 
       lCommand.CommandText += "\"" + row["notes"].ToString().Replace("\"", "\"\"").Replace(" ", " ") + "\",";      
       lCommand.CommandText += "\"" + "High" + "\""; 

      lCommand.CommandText += ")"; 
      lCommand.ExecuteNonQuery(); 
      } 
     } 

     lConnection.Close(); 
    } 

는 그래서 내가하고 싶은 것은 row["notes"].ToString()을 그래서 처음 등등 가장 긴 메모 문자열을 삽입 row["notes"].ToString().Count()으로 삽입 순서입니다 : 그래서이있다. 이것이 가능한가? 만약 내가 이것을 어떻게 할 것인가?

감사합니다.

+5

주문한'INSERT'을해야 할 때가 거의 없습니다. 순서가 중요한 경우 대신'SELECT '중에 적용하십시오. 또는 귀하의 경우에는 통합 문서에서 정렬 작업을 사용하십시오. – Yuck

+0

나는 단지 INSERT만을하고 있지 않다. –

+1

그 점을 이해합니다. 내가 말하는 것은 Excel을 사용하여 데이터를 정렬하는 것입니다. 실제로 데이터베이스에 들어가는 경우'SELECT' 중에 정렬 할 수 있습니다. 일반적으로 - 데이터를 소비하고 사용자에게 제시하는 것은 주문을 수행해야합니다. ** 세트에서 ** 데이터를 주문하는 것은 프레젠테이션 개념입니다. – Yuck

답변

1

Excel에서 제공되는 모든 DataView를 목록에로드 한 다음이 목록을 행 [ "notes"] 값으로 정렬하여 원하는 순서로 삽입 할 수 있습니다.

DataView d = null;//Load from Excel 
    var rows = (from DataRowView rowView in d select rowView.Row).ToList(); 
    foreach (DataRow dataRow in rows.OrderByDescending(r=>(int)r["notes"])) 
    { 
     //Insert code here that you already have 
    }