.Dat file to dataset - c#

I have a .dat file that I need to import into a datatable. I am using .net framework 4.5 and it delimited by ~. How do I import this? I tried
string mySelectQuery = "SELECT * FROM " + fileName;
OleDbConnection myConnection = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;DataSource=" + fileLocation + ";Extended Properties=\"text;HDR=YES;FMT=Delimited\"");
OleDbDataAdapter dsCmd = new OleDbDataAdapter(mySelectQuery, myConnection);
//Fill the DataSet object
dsCmd.Fill(myData, "Packets");
Please help

Related

Can't open csv file using OleDB

I am new in this so i cant find the solution for my problem.I want to read .csv file in DataTable using OleDB.Here is my code
string file = "D:\\MyFile.csv";
string dir = Path.GetDirectoryName(file);
String sConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + dir + ";Extended Properties='text;HDR=Yes;FMT=Delimited'";
OleDbConnection objConn = new OleDbConnection(sConnectionString);
objConn.Open();
DataTable dt = new DataTable();
OleDbCommand objCmdSelect = new OleDbCommand("SELECT * FROM [" + file + "]", objConn);
OleDbDataAdapter objAdapter1 = new OleDbDataAdapter();
objAdapter1.SelectCommand = objCmdSelect;
objAdapter1.Fill(dt);
objConn.Close();
But I am getting an error: The Microsoft Jet database engine could not find the object 'D:\MyFile.csv'. Make sure the object exists and that you spell its name and the path name correctly.
The file is on the right place, so can you tell me what could be the problem?
Place the file inside some folder in D:\ and then retry
GetDirectoryName functions usually returns '' when using such like
GetDirectoryName('C:\asd.txt') returns '' so use it like
GetDirectoryName('C:\myfol\asd.txt') it will returns 'C:\myfol'
and your code is concatinating directory name so chances are you are concatinating a blank string ''
in the line
1. String sConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + dir + ";
2. sConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0.; Data Source = ''; // as dir is ''
check
http://msdn.microsoft.com/en-us/library/system.io.path.getdirectoryname(v=vs.110).aspx
for additional help.
You need to remove the path from the file name in this line:
OleDbCommand objCmdSelect = new OleDbCommand("SELECT * FROM [" + file + "]", objConn);
It should read:
OleDbCommand objCmdSelect = new OleDbCommand("SELECT * FROM [" + Path.GetFileName(file) + "]", objConn);
In the connection string you're telling the engine the directory, in the command you're telling the engine the file to use (the path could be seen as the equivalent of a database, the file as the equivalent of a table).
You could improve this as follows:
string file = "D:\\MyFile.csv";
string dir = Path.GetDirectoryName(file);
string name = Path.GetFileName(file);
String sConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + dir + ";Extended Properties='text;HDR=Yes;FMT=Delimited'";
...
OleDbCommand objCmdSelect = new OleDbCommand("SELECT * FROM [" + name + "]", objConn);
...
Try with actual filename in select query.
SELECT * FROM [MyFile.csv]
OleDbCommand objCmdSelect = new OleDbCommand("SELECT * FROM [MyFile.csv]", objConn);

Error in uploading excel file to database

I am working on a webform which has a file upload and a button and a gridview. user can upload excel file to database and also see preview in gridview .It is workin fine.
my code is
string filename = Path.GetFileName(FileUpload1.FileName);
FileUpload1.SaveAs(Server.MapPath("~/Publisher/ExcelFiles/") + filename);
DataSet ds = new DataSet();
string path = #"~/Publisher/ExcelFiles/" + filename;
OleDbConnection myCon = new OleDbConnection(#"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties=Excel 12.0;");
OleDbCommand myComm = new OleDbCommand("select * from [GIRLS$] ", myCon);
OleDbDataAdapter da = new OleDbDataAdapter(myComm);
da.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
but my problem is path of excel file it gives me an error at da.Fill(ds);
'C:\Program Files\Common Files\Microsoft Shared\DevServer\10.0\~\Publisher\ExcelFiles\glist.xls' is not a valid path. Make sure that the path name is spelled correctly and that you are connected to the server on which the file resides.
excel file resides in my website directory. This error don't come if I changed the path to
string path= #"D:\Vikas Rana\New folder (4)\glist.xls";
any help will be appreciated.
Thanks in advance.
try this
string filename = Path.GetFileName(FileUpload1.FileName);
DataSet ds = new DataSet();
string path = Server.MapPath("~/Publisher/ExcelFiles/" + filename);
FileUpload1.SaveAs(path);
OleDbConnection myCon = new OleDbConnection(#"Provider=Microsoft.ACE.OLEDB.12.0;
DataSource=" + path + ";Extended Properties=Excel 12.0;");
OleDbCommand myComm = new OleDbCommand("select * from [GIRLS$] ", myCon);
OleDbDataAdapter da = new OleDbDataAdapter(myComm);
da.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
if you upload .xls file use this
Provider=Microsoft.Jet.OLEDB.4.0;Data Source=path(which you prefer);Extended Properties="Excel 8.0;HDR=YES;IMEX=1;"
if you upload .xlsx file use this
Provider=Microsoft.ACE.OLEDB.12.0;Data Source=path(which you prefer);Extended Properties="Excel 12.0;HDR=YES;IMEX=1;"
I mean
OleDbConnection myCon = new OleDbConnection(#"Provider=Microsoft.Jet.OLEDB.4.0;
DataSource=" + path + ";Extended Properties=Excel 8.0;HDR=YES;IMEX=1;");

Missing rows reading generated Excel file

I am creating an Excel File using OleDB, it works perfectly, next, I add rows to the file using directly in Office Excel , after, when i try read the file using OleDB again, the rows i added manually does not appear, only the columns and rows i wrote when i created the file.
Here is the code:
//Creation of the file
String connectionString = "Provider=Microsoft.Jet.OleDb.4.0; Data Source=" + path + "; Mode=ReadWrite; Extended Properties= 'Excel 8.0; HDR=Yes; IMEX=0'";
OleDbConnection oledbconnection;
oledbconnection = new OleDbConnection(connectionString);
oledbconnection.Open();
String query = "CREATE TABLE [Sheet1] (....) ";
OleDbCommand cmd = new OleDbCommand(query, oledbconnection);
cmd.ExecuteNonQuery();
oledbconnection.Close();
it works perfectly, the file is created correctly, then, when im trying read the file after modify it, I'm doing this
String connectionStringRead = "Provider=Microsoft.Jet.OleDb.4.0; Data Source=" + path + "; Extended Properties= 'Excel 8.0; HDR=Yes; IMEX=1'";
OleDbConnection oledbconnection;
oledbconnection = new OleDbConnection(connectionStringRead);
oledbconnection.Open();
DataTable table = new DataTable();
String sheetName;
sheetName = oledbconnection.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null).Rows[0]["TABLE_NAME"].ToString();
OleDbDataAdapter adapter = new OleDbDataAdapter("SELECT * FROM [" + sheetName + "]", oledbconnection);
adapter.Fill(table);
oledbconnection.Close();
return table;
The table contains ONLY the original information, not the rows I added using MS Excel.
I don't understand this, can you help me please ?
thanks in advance
Never try like that before, but if
sheetName = "Sheet1" '-------> or whatever
It should be
OleDbDataAdapter adapter = new OleDbDataAdapter("SELECT * FROM [" + sheetName + "$]", oledbconnection);

Read An Excel File With OLEDB?

Hi I am reading an excel file with oledb(The file has 100000 rows). I must read file quickly.
string conn;
conn = ("Provider=Microsoft.ACE.OLEDB.12.0;" +
("Data Source=" + _filename + ";" +
"Extended Properties=\"Excel 12.0;\""));
OleDbConnection oleDBCon = new OleDbConnection(conn);
oleDBCon.Open();
DataTable dt = oleDBCon.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
string excelsheetname = dt.Rows[0].ItemArray[2].ToString();
string SSQL = "SELECT * from [" + excelsheetname + "]";
OleDbDataAdapter oleDA = new OleDbDataAdapter(SSQL, conn);
DataSet ds = new DataSet();
oleDA.Fill(ds);
DataTable _DtTable = ds.Tables[0]; // or [ ds ]
oleDBCon.Close();
and then in _DtTable with a for loop I am inserting these cells to DB.. How can I read this very large excel quickly? And insert to DB? I used Parallel.For but it is not true solution for me.. Any idea?
To add records to MyTable using ADO, you can use code similar to the following:
'Create a new connection object for Book1.xls
Dim conn As New ADODB.Connection
conn.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=C:\Book1.xls;Extended Properties=Excel 8.0;"
conn.Execute "Insert into MyTable (FirstName, LastName)" & _
" values ('Bill', 'Brown')"
conn.Execute "Insert into MyTable (FirstName, LastName)" & _
" values ('Joe', 'Thomas')"
conn.Close
This is from MSDN: http://support.microsoft.com/kb/247412
Make use of OpenXML of SQL to insert data in bulk which do fater work for you
here is code did by me for same work : Bulk Insertion of Data Using C# DataTable and SQL server OpenXML function
You could look at some of the ways the database can consume Excelfiles

Error when getting records from xlsx file in C#

I am getting an error when I attempt to read the records from my xlsx file.
The error is "Cannot update. Database or object is read-only."
I have verified that the file is not read-only. Any ideas what could be causing the error?
My code is:
string strFileName = System.IO.Path.GetFileName(txtSourcePath.Text);
string strFilePath = txtSourcePath.Text;
string strDirectoryPath = strFilePath.Substring(0, (txtSourcePath.TextLength - (strFileName.Length + 1)));
string conn = string.Format(#"Provider=Microsoft.Jet.OLEDB.4.0; Data Source={0}; Extended Properties=""text;HDR=YES;FMT=DELIMITED""", strDirectoryPath);
OleDbConnection oleDBConn = new OleDbConnection(conn);
oleDBConn.Open();
OleDbDataAdapter da = new OleDbDataAdapter("Select * FROM [" + strFileName + "]", conn);
DataSet ds = new DataSet();
da.Fill(ds);
When I hover over ds I don't see any records
Try This
for xls
<add key="xlsConnection" value="Provider=Microsoft.Jet.OLEDB.4.0; Data Source=##PATH##;Extended Properties='Excel 8.0;HDR=YES;IMEX=1'"/>
for xlsx
<add key="xlsxConnection" value="Provider=Microsoft.ACE.OLEDB.12.0;Data Source=##PATH##;Extended Properties='Excel 12.0;HDR=YES;'"/>
Required to install : microsoft.ace.oledb.12.0 driver
Replace Path of your excel file path

Categories