change datatable encoding NON ASCII to Unicode in c# - c#

I have a table in foxpro 2.6 dos version and convert it to a datatable. so far so good. but some character do not display, fields that contain some non_ASCII character. how can i change codepage of a datatable to show all character?
public DataTable GetDataTableDBF(string FN)
{
OleDbConnection conn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Path.GetFullPath(FileNameStr).Replace(System.IO.Path.GetFile‌​Name(FN), "") + ";Extended Properties=dBASE IV;");
conn.Open();
string sQuery = "SELECT * FROM [" + System.IO.Path.GetFileName(FN) + "]";
OleDbDataAdapter adapter = new OleDbDataAdapter(sQuery, conn);
DataSet ds = new DataSet();
adapter.Fill(ds);
return ds.Tables[0];
}

Related

Join two DataTable one from excel data and other from text file to display it in datagridview in c#

I want to show data in datagridview in c# using from two source one from excel sheet and other from text file .I want to join these data source .For example if I get columns from Excel sheet excel1 excel2 AND Txt1 Txt2 from text file .Then I want to show result like excel1 excel2 Txt1 Txt2 in data grid view.
Current i can show data from excel file using below code.
private void button_cars_Click(object sender, EventArgs e)
{
String name = "Car List";
String constr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + "c:\\Vehicle_List.xlsx" + ";Extended Properties='Excel 12.0 XML;HDR=YES;';";
OleDbConnection con = new OleDbConnection(constr);
OleDbCommand oconn = new OleDbCommand("Select CARID, BRAND, MODEL, FUEL, CC, YEAR From [" + name + "$]", con);
con.Open();
OleDbDataAdapter sda = new OleDbDataAdapter(oconn);
DataTable data = new DataTable();
sda.Fill(data);
dataGridViewMain.DataSource = data;
}
I handle this situation like this
public DataTable mergeSheetAndTextFile()
{
String name = "Sheet1";
String constr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" +
"D:\\YOURPATH\\List.xlsx" +
";Extended Properties='Excel 12.0 XML;HDR=YES;';";
OleDbConnection con = new OleDbConnection(constr);
OleDbCommand oconn = new OleDbCommand("Select col1,col2 From [" + name + "$]", con);
con.Open();
OleDbDataAdapter sda = new OleDbDataAdapter(oconn);
DataTable data = new DataTable();
sda.Fill(data);
data.Columns.Add("Txt1Col", typeof(System.String));
string[] txtFileLines= File.ReadAllLines("c://YOURPATH//test.txt");
int count=0;
foreach (DataRow row in data.Rows)
{
if(count<txtFileLines.Length){
row["Txt1Col"]=txtFileLines[count]
count++;`enter code here`
}
}
}

Excel OleDb C# - Cannot select range if worksheet has special characters

I have run into a snag when reading info from a workbook in excel that contains a "#" in the name of the sheet. I am able to select the WHOLE worksheet but not a range in the worksheet.
Code:
using (OleDbConnection conn = new OleDbConnection())
{
DataTable dt = new DataTable();
string Import_FileName = "C:/TestExcel/TestWorkbook.xlsm";
conn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + Import_FileName + ";" + "Extended Properties='Excel 12.0 Xml;HDR=YES;'";
using (OleDbCommand comm = new OleDbCommand())
{
comm.CommandText = "Select * from [Sheet #1$A1:A22]";
comm.Connection = conn;
using (OleDbDataAdapter da = new OleDbDataAdapter())
{
da.SelectCommand = comm;
da.Fill(dt);
}
}
}
And the following queries work just fine.
comm.CommandText = "Select * from [Sheet #1$]";
comm.CommandText = "Select * from [Sheet2$A1:A22]";
I get the following exception saying it cannot find the table in question.
The Microsoft Office Access database engine could not find the object 'Sheet .1$A1:A22'.

Reading Excel ROW using OleDb data retrieval

Any help what I am doing wrong here? if I am trying to read the only row for an example TestCaseName to case_1 then i'm getting the data of different row.
How can I make sure its only read what is being requested to read? and I am using the where clause but seems like does not filter it.
string connectionString = String.Format(#"Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=""Excel 8.0;HDR=YES;IMEX=1;""", EXCELFILENAME);
string testCaseName = "case_1
string query = String.Format("SELECT * from [{0}$] WHERE TestCaseName=\"{1}\"", workbookName, testCaseName);
OleDbDataAdapter dataAdapter = new OleDbDataAdapter(query, connectionString);
DataSet dataSet = new DataSet();
dataAdapter.Fill(dataSet);
DataTable myTable = dataSet.Tables[0];
TestCaseName Name Active Status etc...
----------------------------------------------------------------
case_1 Tom yes Completed etc...
----------------------------------------------------------------
case_2 John yes etc...
----------------------------------------------------------------
case_3 Jim yes etc...
----------------------------------------------------------------
case_4 Don yes etc...
----------------------------------------------------------------
case_5 Sam yes Visitor etc...
----------------------------------------------------------------
Here's the code I tested with. It appears to work perfectly. If your doesn't then I can only assume that your spreadsheet is not structured quite the same as mine.
string connectionString = "Provider=Microsoft.Ace.OLEDB.12.0;Data Source=" + filename + ";Extended Properties=\"Excel 8.0;HDR=YES;IMEX=1\"";
string testCaseName = "case_1";
string query = "SELECT * from [Sheet1$] WHERE TestCaseName=\"" + testCaseName + "\"";
DataTable dt = new DataTable();
using (OleDbConnection conn = new OleDbConnection(connectionString))
{
conn.Open();
using (OleDbDataAdapter dataAdapter = new OleDbDataAdapter(query, conn))
{
DataSet ds = new DataSet();
dataAdapter.Fill(ds);
dt = ds.Tables[0];
}
conn.Close();
}

Query excel sheet in c#

I want to read Excel file in c# using following code
string excelFileName = "Book2.xls";
string excelConnectString = #"Provider=Microsoft.Jet.OLEDB.4.0; Data Source=Book2.xls;Extended Properties=""Excel 8.0;HDR=YES;""";
//string excelConnectString = #"Provider = Microsoft.Jet.OLEDB.4.0;Data Source = " + excelFileName + ";" + "Extended Properties = Excel 8.0; HDR=Yes;IMEX=1";
OleDbConnection objConn = new OleDbConnection(excelConnectString);
OleDbCommand objCmd = new OleDbCommand("Select * From [Sheet1$]", objConn);
OleDbDataAdapter objDatAdap = new OleDbDataAdapter();
objDatAdap.SelectCommand = objCmd;
DataSet ds = new DataSet();
objDatAdap.Fill(ds);
Everything is working fine.Now my requirement is to read the excel file something like below
SELECT A,B,D From [Sheet1];
The Select-command should look like this if you want to read A1 to D1:
SELECT * FROM [SHEETNAME_HERE$A1:D1]
Whole Code:
OleDbConnection con = new OleDbConnection(
"provider=Microsoft.Jet.OLEDB.4.0;data source="
+ XLS_FILE_NAME_AND_PATH_HERE
+ ";Extended Properties=Excel 8.0;");
StringBuilder stbQuery = new StringBuilder();
stbQuery.Append("SELECT * FROM [" + SHEETNAME_HERE + "$A1:D1]");
OleDbDataAdapter adp = new OleDbDataAdapter(stbQuery.ToString(), con);
DataSet dsXLS = new DataSet();
adp.Fill(dsXLS);
DataView dvEmp = new DataView(dsXLS.Tables[0]);
dataGridView1.DataSource = dvEmp;
DataTable Contents = new DataTable();
using (OleDbDataAdapter adapter = new OleDbDataAdapter("Select * From [Sheet1$]", objConn))
{
adapter.Fill(Contents);
}
Console.WriteLine(Contents.Rows[0][0]);
You can select a particular cell by passing the proper index.
You can just constuct use query like that:
SELECT FirstName, LastName, Mobile FROM [Sheet1$]
i.e. use first row values as column names.

Read alphanumeric characters from csv file in C#

I am using the following code to read my csv file:
public 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.ACE.OLEDB.12.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);
//fill the DataTable
dAdapter.Fill(dTable);
dAdapter.Dispose();
return dTable;
}
But the above doesn't reads the alphanumeric value from the csv file. it reads only i either numeric or alpha.
Whats the fix i need to make to read the alphanumeric values? Please suggest.
I suggest you use A Fast CSV Reader which does not have this issue and is much more faster.
Remove IMEX=1 from the connection string. I don't think you need it for CSV files.
Try this OleDBAdapter Excel QA I posted via stack overflow.
I have not tried this out, but it sounds interesting! LinqToExcel
they say it can be used on .CSV files as well...
hi all this code is gets alphanumeric values also
using System.Data.OleDb;
string ConnectionString = #"Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + filepath + ";" + "Extended Properties="+(char)34+"Excel 8.0;IMEX=1;"+(char)34;
string CommandText = "select * from [Sheet1$]";
OleDbConnection myConnection = new OleDbConnection(ConnectionString);
myConnection.Open();
OleDbDataAdapter myAdapter = new OleDbDataAdapter(CommandText, myConnection);
ds = null;
ds = new DataSet();
myAdapter.Fill(ds);

Categories

Resources