问题描述
您好开发者的
我在datatable中加载csv文件时遇到了很大的问题.原因是csv文件值用(;)分隔.当我在oledb或odbc分隔符中给出(;)时,他们无法识别它们并给出索引121类型错误",因此,我将创建schema.ini文件并将其保存在项目的bin \ debug文件夹中.但之后再次将csv文件加载到datatable中.
我还告诉你,我要加载许多具有不同名称的csv文件.是否可以不在schema.ini文件中添加文件名.
我的项目代码是请阅读
Hi developer''s
I have a great problem to load a csv file in datatable . A reason is csv file values are seprated by (;). and when I given a (;) in oledb or odbc delimiter they can not identified them and given a "Index 121 type error", so then i am create schema.ini file and save them in bin\debug folder of the project . but after that again csv file not load in datatable.
I also tell u that i m load a many csv file that have a diffrent name then. is it possible to not add a file name in schema.ini file.
my code of project is please read this
public static DataTable ParseCSV(string path)
{
if (!File.Exists(path))
return null;
string full = Path.GetFullPath(path);
string file = Path.GetFileName(full);
string dir = Path.GetDirectoryName(full);
//create the "database" connection string
string connString = "Provider=Microsoft.Jet.OLEDB.4.0;"
+ "Data Source=\"" + dir + "\\\";"
+ "Extended Properties=\"text;HDR=yes;FMT=Delimited\";";
//create the database query
string query = "SELECT * FROM " + file;
//create a DataTable to hold the query results
DataTable dTable = new DataTable();
//create an OleDbDataAdapter to execute the query
OleDbDataAdapter dAdapter = new OleDbDataAdapter(query, connString);
try
{
//fill the DataTable
dAdapter.Fill(dTable);
}
catch (System.Exception ex)
{
MessageBox.Show(ex.Message);
}
dAdapter.Dispose();
return dTable;
}
private void button1_Click(object sender, EventArgs e)
{
try
{
DialogResult result = this.openFileDialog1.ShowDialog();
if (result == DialogResult.OK)
{
string filename = openFileDialog1.FileName;
textBox1.Text = filename;
}
else
{
MessageBox.Show("dear user please select path");
}
DataTable dt = Form1.ParseCSV(textBox1.Text);
dataGridView1.DataSource = dt.DefaultView;
}
catch (System.Exception ex)
{
MessageBox.Show(ex.Message);
}
}
编写此代码后,我将编写一个schema.ini文件代码
[file]//这是一个可变名称,已添加到数据表代码中
格式=分隔符(;)
请解决我的问题,即如何在数据集或datable中加载csv(;)分隔的文件?
我总是非常感谢您,请解决我的问题.
after write this code I m write a schema.ini file code
[file] // this is a vaariable name that is add in a datatable code
Format=delimited(;)
Please solve my problem that how load a csv(;)seprated file in dataset or a datable?
I always thankfull to you, please solve my problem.
推荐答案
//create the "database" connection string
string connString = "Provider=Microsoft.Jet.OLEDB.4.0;"
+ "Data Source=\"" + dir + "\\\";"
+ "Extended Properties=\"text;HDR=yes;FMT=Delimited\";";
应该是这样的:
should be this:
//create the "database" connection string
string connString = "Provider=Microsoft.Jet.OLEDB.4.0;"
+ "Data Source=" + dir + "\\;"
+ "Extended Properties=\"text;HDR=Yes;FMT=Delimited(;)\"";
这篇关于oledb无法识别的schema.ini文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!