How can I create a schema.ini file? I need to export my CSV file to datagridview

I want to export a CSV file to datagridview. I need to create a schema.ini file. But I do not know how I can create it?

There is my code:

  public DataTable exceldenAl(string excelFile) { try { string fileName = Path.GetFileName(excelFile); string pathOnly = Path.GetDirectoryName(excelFile); string cmd = "Select * From [" + fileName + "$]"; string cnstr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + pathOnly + "\\;Extended Properties=\"Text;HDR=Yes;FORMAT=Delimited\""; OleDbConnection ocn = new OleDbConnection(cnstr); ocn.Open(); OleDbCommand command = new OleDbCommand(cmd,ocn); OleDbDataAdapter adap = new OleDbDataAdapter(command); DataTable dt = new DataTable(); dt.Locale = CultureInfo.CurrentCulture; adap.Fill(dt); return dt; } finally { } } private void btnExcelReader_Click(object sender, EventArgs e) { string dosya; string cevap; openFileDialog1.ShowDialog(); dosya = openFileDialog1.FileName.ToString(); ClsExcelReader er = new ClsExcelReader(); cevap = er.exceldenAl(dosya).ToString(); dataGridView1.DataSource = cevap; //listViewExcelOku.DataBindings = } } 
+4
source share
2 answers

Open notepad and create a file similar to this one:

 [YourCSVFileName.csv] ColNameHeader=True Format=CSVDelimited DateTimeFormat=dd-MMM-yyyy Col1=A DateTime Col2=B Text Width 100 Col3=C Text Width 100 Col4=D Long Col5=E Double 

Modify the above file to fit your specific data schema. Save it as SCHEMA.ini in the same directory where your * .CSV file is located.

Read this link ( Import a CSV file into a database ), this is a good example to pick you up and understand how Schema.ini works

+5
source

I wrote these Excel formulas to create the contents of this file if you can get an Excel sheet with column headings. It is pretty simple. Add and remove features as desired. It accepts all delimited text. Then paste the following formula (including all your options) into A2:

 ="[no headers.csv] "&"ColNameHeader=false "&"MaxScanRows=0 "&"Format=Delimited(;) Col"&COLUMN()&"="&A1&" text " 

And the following formula is in B2.

 =A2&"Col"&COLUMN()&"="&B1&" text " 

Then drag to the right to get the base schema.ini (the rightmost cell). You can configure the parameters in the excel cell below the column name. Each column has its own definition. I got closing and opening quotes as a result when copying to a text file.

+1
source

Source: https://habr.com/ru/post/1432217/


All Articles