here my code which need to save data from excel sheet to database access using C#. I don't get any error but my excel data could not be updated in database access.. What I have to do for this?
My Database Access Tablename is "addsales1".
My Excel sheet name is "addsales1".
string file_name = Application.StartupPath + "\\" + "databaseset.txt";
System.IO.StreamReader objreader;
objreader = new System.IO.StreamReader(file_name);
string sh = objreader.ReadLine();
string con = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + textBox9.Text
+ ";Extended Properties=Excel 8.0;";
OleDbConnection connection = new OleDbConnection(con);
OleDbCommand cmd = new OleDbCommand("INSERT INTO [MS Access;Database=" + sh +
"].[addsales1] SELECT * FROM [addsales1$]");
cmd.Connection = connection;
connection.Open();
cmd.ExecuteNonQuery();
connection.Close();
MessageBox.Show("Sales Details Imported Successfully!");
Related
Below code snippet working fine for reading some 5000-6000 rows but not for more than 10000. when I use SELECT * FROM Sheet1$A1:DC20000, dtdata has only 8327 rows.
string connstr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + templatepath + ";Extended Properties=Excel 12.0;";
string Extension = Path.GetExtension(templatepath);
if (Extension == ".xls")
connstr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + templatepath + ";Extended Properties=Excel 8.0";
OleDbConnection conn = new OleDbConnection(connstr);
strSQL = "SELECT * FROM Sheet1$A1:DC20000";
OleDbCommand cmd = new OleDbCommand(strSQL, conn);
cmd.CommandTimeout = 5000;
OleDbDataAdapter da = new OleDbDataAdapter(cmd);
da.Fill(dtdata);
dtdata.TableName = "Table0";
dsdata.Tables.Add(dtdata);
This code perfectly work on my machine (I have excel 2010) but when my supervisor tried to run but not work on his machine(he have excel 2016) so for excel 2016 do i need to change connection
ConStr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + fileFullPath + ";Extended Properties=\"Excel 12.0;HDR=" + HDR + ";IMEX=0\"";??
string FolderPath = Dts.Variables["User::FolderPath"].Value.ToString();
string TableName = Dts.Variables["User::TableName"].Value.ToString();
string SchemaName = Dts.Variables["User::SchemaName"].Value.ToString();
string SheetNameToLoad = Dts.Variables["User::SheetNameLike"].Value.ToString();
var directory = new DirectoryInfo(FolderPath);
FileInfo[] files = directory.GetFiles();
//Declare and initilize variables
string fileFullPath = "";
SqlConnection myADONETConnection = new SqlConnection();
myADONETConnection = (SqlConnection)(Dts.Connections["DBconnection"].AcquireConnection(Dts.Transaction) as SqlConnection);
////Get one Book(Excel file at a time)
foreach (FileInfo file in files)
{
fileFullPath = FolderPath + "\\" + file.Name;
MessageBox.Show(fileFullPath);
// //Create Excel Connection
string ConStr;
string HDR;
HDR = "YES";
ConStr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + fileFullPath + ";Extended Properties=\"Excel 12.0;HDR=" + HDR + ";IMEX=0\"";
OleDbConnection cnn = new OleDbConnection(ConStr);
// //Get Sheet Name
cnn.Open();
DataTable dtSheet = cnn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
string sheetname;
sheetname = "";
//Only read data from provided SheetNumber
foreach (DataRow drSheet in dtSheet.Rows)
{
sheetname = drSheet["TABLE_NAME"].ToString();
MessageBox.Show(sheetname);
//Load the Data if Sheet Name contains value of SheetNameLike
if (sheetname.Contains(SheetNameToLoad) == true)
{
//Load the DataTable with Sheet Data so we can get the column header
OleDbCommand oconn = new OleDbCommand("select * from [" + sheetname + "] where CityName ='ARLINGTON'", cnn);
OleDbDataAdapter adp = new OleDbDataAdapter(oconn);
DataTable dt = new DataTable();
adp.Fill(dt);
cnn.Close();
//Load Data from DataTable to SQL Server Table.
using (SqlBulkCopy BC = new SqlBulkCopy(myADONETConnection))
{
BC.DestinationTableName = SchemaName + "." + TableName;
BC.WriteToServer(dt);
}
}
}
Did he get the error says ACE provider is not registered? If so, your supervisor need to download and install this on his machine:
https://www.microsoft.com/en-us/download/details.aspx?id=13255
I am writing a program to read excel using c# and store in a DataTable. When I check the columns in my DataTable while debugging, I see system columns like Table_Schema, Table_Catalog, Table_Name, Table_Type etc. How do I exclude these unwanted columns?
My code:
string sSheetName = null;
string sConnection = null;
int nOutputRow = 0;
DataTable dtTablesList = default(DataTable);
OleDbCommand oleExcelCommand = default(OleDbCommand);
OleDbConnection oleExcelConnection = default(OleDbConnection);
sConnection = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + txtFileName.Text + ";" + "Extended Properties=Excel 8.0;";
oleExcelConnection = new OleDbConnection(sConnection);
oleExcelConnection.Open();
dtTablesList = oleExcelConnection.GetSchema("Tables");
if (dtTablesList.Rows.Count > 0)
{
sSheetName = dtTablesList.Rows[0].Field<string>("TABLE_NAME");
}
dtTablesList.Clear();
dtTablesList.Dispose();
if (!string.IsNullOrEmpty(sSheetName))
{
oleExcelCommand = oleExcelConnection.CreateCommand();
string commandText = "Select * From [" + sSheetName + "]";
oleExcelCommand.CommandType = CommandType.Text;
OleDbDataAdapter daexcel = new OleDbDataAdapter(commandText, oleExcelConnection);
dtTablesList.Locale = System.Globalization.CultureInfo.CurrentCulture;
daexcel.Fill(dtTablesList);
}
oleExcelConnection.Close();
I read an excel file using OLEDB. Below is the code:
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 SSQL = "SELECT * from [ Sheet1$ ]";
OleDbDataAdapter oleDA = new OleDbDataAdapter(SSQL, conn);
DataSet ds = new DataSet();
oleDA.Fill(ds);
DataTable _DtTable = ds.Tables[0];
oleDBCon.Close();
dataGridView1.DataSource = _DtTable;
foreach (DataRow rows in _DtTable.Rows)
{
string Description = rows[0].ToString();
string Code= rows[1].ToString();
textBox1.AppendText("Printing Description: " + Description + " and Code: " + Code + ",Date:" + DateTime.Now.ToString() + Environment.NewLine);
}
The excel file is as follows:
The data printed in textBox1 are:
Printing Description:Desc 2 and Code: Code 2,Date:20/12/2014 12:36:54 μμ
Printing Description: Desc 3 and Code: Code 3,Date:20/12/2014 12:36:54 μμ
So, my problem is that the 1st row of excel is going to the header of the Datatable. How can I avoid that (without adding any extra 1st row to excel)?
Just add "HDR=No" at the end of your connection string which means "No header row that indicates column but it contains data", then you will be able to fetch 1st row data also.
So your complete connection string would be
conn = ("Provider=Microsoft.ACE.OLEDB.12.0;" +
("Data Source=" + _filename + ";" +
"Extended Properties=\"Excel 12.0;\";HDR=No"));
I have code from a colleague, this code creates a few excel sheets with Epplus. With my code I would like to add an database extract 10k+/- lines. Because of the large amount of data it takes too long with Epplus, because you need to write each cell. With OleDB it only takes a few seconds. But I can't open a previously created excel by Epplus with OleDB. Even with different connection strings.
This my code works perfect if you separate the two code blocks.
var excelPath = "C:\\test_" + DateTime.Today.ToString("yyyyMMdd_") + DateTime.Now.ToString("hh") + DateTime.Now.Minute.ToString() + ".xlsx";
using (ExcelPackage xlPackage = new ExcelPackage(new FileInfo(excelPath)))
{
ExcelWorksheet worksheet = xlPackage.Workbook.Worksheets.Add("Schedule V");
worksheet.Cell(1, 1).Value = "test";
xlPackage.Save();
xlPackage.Dispose();
}
var strCn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + excelPath + ";Extended Properties='Excel 12.0 Xml';";
using (OleDbConnection conn = new OleDbConnection(strCn))
{
conn.Open();
OleDbCommand cmd = new OleDbCommand();
cmd.Connection = conn;
cmd.CommandText = "CREATE TABLE [table1] (id INT, name VARCHAR, datecol DATE );";
cmd.ExecuteNonQuery();
cmd.CommandText = "INSERT INTO [table1](id,name,datecol) VALUES(1,'AAAA','2014-01-01');";
cmd.ExecuteNonQuery();
conn.Close();
}
I tried the following connection strings but they all give the same error:
OleDbException was unhandled, External table is not in the expected
format.
My diffrent connection string I tried:
var strCn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + excelPath + ";Extended Properties='Excel 12.0';";
var strCn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + excelPath + ";Extended Properties='Excel 12.0 Xml';";
var strCn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + excelPath + ";Extended Properties='Excel 12.0 Xml;HDR=YES';";
var strCn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + excelPath + ";Extended Properties='Excel 12.0 Xml;HDR=YES;IMEX=1';";
var strCn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + excelPath + ";Extended Properties='Excel 8.0;HDR=YES';";
var strCn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + excelPath + ";Extended Properties='Excel 8.0;HDR=Yes;IMEX=1';";
What am I doing wrong here?
I guess, it should be like this, rather than hardcoding you should use OleDbConnectionStringBuilder class
OleDbConnectionStringBuilder connectionStringBuilder = new OleDbConnectionStringBuilder();
connectionStringBuilder.Provider = "Microsoft.ACE.OLEDB.12.0";
connectionStringBuilder.DataSource = excelPath; // This is your Excel File Full Path
connectionStringBuilder.Add("Mode", "Read");
const string extendedProperties = "Excel 12.0;IMEX=1;HDR=YES";
connectionStringBuilder.Add("Extended Properties", extendedProperties);
String connectionString = connectionStringBuilder.ToString();
// Create connection object by using the preceding connection string.
using (var objConn = new OleDbConnection(connectionString))
{
// Open connection with the database.
objConn.Open();
// Do operations with your File here
}
I was getting this error consistently with .xlsx files created with EPPlus 4.0.4. I remembered this was working prior to upgrading to EPPlus 4.x. I downgraded to 3.1.3.3 and I no longer get this error.