2011-03-03 26 views
9

मुझे पता है कि इस तरह के प्रश्न समय-समय पर पूछे जाते हैं लेकिन मुझे कोई संतोषजनक समाधान नहीं मिल रहा है।माइक्रोसॉफ्ट.एसीई.ओएलडीबी.12.0 सीएसवी कनेक्शनस्ट्रिंग

मैं एमएस एसीई ओएलडीडीबी 12 का उपयोग कर सीएसवी-फाइल कैसे खोल सकता हूं? मैं इसे निम्नलिखित कोड से आज़माता हूं।

DbConnection connection = new OleDbConnection(); 
connection.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\Documents;Extended Properties=\"Text;HDR=Yes\""; 
connection.Open(); 
DbCommand cmd; 

cmd = connection.CreateCommand(); 
cmd.CommandText = "SELECT * FROM [Mappe1#csv]"; 
DbDataReader reader = cmd.ExecuteReader(); 

while (reader.Read()) 
{ 
    for (int i = 0; i < reader.FieldCount; i++) 
     Console.Write("(" + reader.GetValue(i).ToString() + ")"); 

    Console.WriteLine(); 
} 

cmd.Dispose(); 
connection.Dispose(); 
Console.WriteLine("Done"); 
Console.ReadKey(); 

समस्या यह है कि केवल एक कॉलम पाया जाता है। पाठ ';' द्वारा सीमित है। यहां तक ​​कि जब मैं "delimited (|)" f.e. के साथ delimiter निर्दिष्ट करते हैं। यह काम नहीं करेगा।

मैं इस प्रदाता के लिए किसी भी प्रलेखन नहीं मिल सकता है ...

+0

हम एसीई से अधिक से अधिक दूर जा रहे हैं। इसमें बहुत समस्या है (लगभग कोई दस्तावेज़ीकरण, कोई समर्थन नहीं, डेटा या वर्कशीट नामों में विशिष्ट वर्णों के साथ समस्याएं ...)। हमने पाया कि यदि आपके पास एसीई की तुलना में एक साफ डिज़ाइन है, तो इसके बजाय इंटरऑप का उपयोग करने के लिए यह तेज़, आसान और अधिक विश्वसनीय है। सीएसवी के लिए अतिरिक्त हम एक और एपीआई का उपयोग कर रहे हैं (लुमेनवर्क्स सीएसवी रीडर: http://www.codeproject.com/Articles/9258/A-Fast-CSV-Reader) – SACO

उत्तर

0

प्रयास करें:

connection.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\Documents;Extended Properties=\"Text;HDR=Yes;FORMAT=Delimited\""; 

(कनेक्शन स्ट्रिंग की विस्तारित गुण में डालने "प्रारूप = सीमांकित" ...)

http://sqlserverpedia.com/blog/sql-server-bloggers/use-ace-drivers-and-powershell-to-talk-to-text-files/:

Cre

+0

नहीं; कोई परिवर्तन नहीं होता है। मैंने एफएमटी = टैबडिलेमिटेड (टैब टैब के लिए) भी कोशिश की। – SACO

+0

तब कहना मुश्किल है ... यह एक सटीक कनेक्शन स्ट्रिंग है जिसका उपयोग मैं अपने प्रोजेक्ट में एक सीएसवी पढ़ने के लिए करता हूं और यह एक आकर्षण की तरह काम करता है। एकमात्र अन्य अंतर चुनिंदा वक्तव्य में होगा। मेरे पास फ़ाइल नाम निर्दिष्ट करें ", फ़ाइल.csv से चुनें *। मैंने पहले "[mappe1 # csv]" वाक्यविन्यास नहीं देखा है - "mappe1 # csv" फ़ाइल का नाम है या यह "mappe1.csv" निर्दिष्ट करने का एक और तरीका है? फ़ाइल एक्सटेंशन ".csv" नहीं है, तो इससे पहले कि यह विफल हो गया है। – blech

+1

मैं इसे आज़मा दूंगा। mappe1 # csv वह नाम है जो कनेक्शन द्वारा प्रदान किया जाएगा। GetSchema()। – SACO

6

यह मैं ACE.OLEDB.12.0 का उपयोग कर सी # में पार्स करने के लिए अर्धविराम-सीमांकित csv हो रही मदद की

[fileIwantToImport.csv] 
Format=Delimited(;) 
ColNameHeader=True 

मेरे लिए काम किया: csv फ़ाइल आप निम्नलिखित सामग्री के साथ आयात करना चाहते हैं के रूप में ही निर्देशिका में Schema.ini पाठ फ़ाइल खा लिया। लेकिन बहुत भाग्यशाली।

कनेक्शन स्ट्रिंग में FORMAT=Delimited(;) फैशन से बाहर चला गया है की तरह लगता है ...

+2

सकल लेकिन यह काम करता है! –

-1

आप डेटासेट बनाने पर विचार किया है?

public static DataSet ConvertTabFiles(string File, string TableName, string delimiter) 
    { 
     //The DataSet to Return 
     DataSet result = new DataSet(); 

     //Open the file in a stream reader. 
     StreamReader s; 
     try 
     { 
      s = new StreamReader(@File); 
     } 
     catch 
     { 
      MessageBox.Show("Can't perform operation on file: " + File); 
      return result; 
     } 

     //Split the first line into the columns 
     string[] columns = null; 
     try 
     { 
      columns = s.ReadLine().Split(delimiter.ToCharArray()); 
     } 
     catch 
     { 
      MessageBox.Show("Can't parse the file " + File + ", please try again!"); 
      return result; 
     } 

     //Add the new DataTable to the RecordSet 
     result.Tables.Add(TableName); 
     //MessageBox.Show("Add the new DataTable to the RecordSet"); 

     //Cycle the colums, adding those that don't exist yet 
     //and sequencing the one that do. 
     foreach (string col in columns) 
     { 
      bool added = false; 
      string next = ""; 
      int i = 0; 
      while (!added) 
      { 
       //Build the column name and remove any unwanted characters. 
       string columnname = col + next; 

       //See if the column already exists 
       if (!result.Tables[TableName].Columns.Contains(columnname)) 
       { 
        //if it doesn't then we add it here and mark it as added 
        result.Tables[TableName].Columns.Add(columnname); 
        added = true; 
       } 
       else 
       { 
        //if it did exist then we increment the sequencer and try again. 
        i++; 
        next = "_" + i.ToString(); 
       } 
      } 
     } 

     //Read the rest of the data in the file.   
     string AllData = s.ReadToEnd(); 

     string[] rows = AllData.Split("\r\n".ToCharArray()); 

     //Now add each row to the DataSet   
     foreach (string r in rows) 
     { 
      //Split the row at the delimiter. 
      string[] items = r.Split(delimiter.ToCharArray()); 
      //Add the item 
      result.Tables[TableName].Rows.Add(r); 
     } 
     //Return the imported data. 
     return result; 
    } 
+0

यह आपके स्वयं के सीएसवी-रीडर को लागू करने की तरह है। हम वर्तमान में एक और सीएसवी-रीडर का उपयोग कर रहे हैं। – SACO

+1

यह एम्बेडेड डिलीमीटर वाले डेटा पर असफल हो जाएगा, उदाहरण के लिए एक कॉमा अंदर एक फ़ील्ड होना चाहिए सीएसवी फ़ाइल। –