4

I want to export a CSV file to a datagridview. I need to create the file schema.ini. But I don't know, how can I 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 =
    }
}

2 Answers 2

6

Open up notepad and create a file similar to this:

[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 (Importing CSV File Into Database), it is a good example to get you up and understanding how the Schema.ini works

1
1

I wrote these Excel formulas to generate the content of this file if you can get hold of the Excel sheet with column headers. It's pretty basic. Add and remove features as desired. It assumes all text with delimiters. Then insert the following formula (including all your options) in A2:

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

And the following formula in B2.

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

Then drag to the right to get your basic schema.ini (rightmost cell). You can adjust options in the excel cell below the column name. Each column has it's own definition. I got closing and opening quotes in the result when copied to text file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.