compliation error while using Oledbconnection - c#

i am using OleDbConnection for connection string here but I am getting error at line
if (conn.State == ConnectionState.Closed)
Error as CS0019: Operator '==' cannot be applied to operands of type 'System.Data.ConnectionState' and 'ConnectionState'
here is my code
protected void btnSave_Click(object sender, EventArgs e)
{
DataTable dtExcel = new DataTable();
dtExcel.Clear();
string StrCount = String.Empty;
string connString = "";
HttpPostedFile File = FileUpload1.PostedFile;
string strFileType = Path.GetExtension(FileUpload1.FileName).ToLower();
string path = FileUpload1.PostedFile.FileName;
string Filename = path.Substring(path.LastIndexOf("\\") + 1, path.Length - path.LastIndexOf("\\") - 1);
path = Server.MapPath(#"~/Excels/" + "/" + Filename.ToString());
File.SaveAs(path);
if (strFileType.Trim() == ".xls")
{
connString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + path + ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=2\"";
}
else if (strFileType.Trim() == ".xlsx")
{
connString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties=\"Excel 12.0;HDR=Yes;IMEX=2\"";
}
string query = "SELECT * FROM [Sheet 1$]";
OleDbConnection conn = new OleDbConnection(connString);
conn.Close();
if (conn.State == ConnectionState.Closed)
conn.Open();
OleDbCommand cmd = new OleDbCommand(query, conn);
OleDbDataAdapter daExcel = new OleDbDataAdapter(cmd);
daExcel.Fill(dtExcel);
conn.Close();}
I dont know why?
I tried the solutions from other link but it didn't helped

It seems to me you have an ambiguous class or property name. ConnectionState appears to have two meanings.
Try to prefix ConnectionState as follows with its full namespace:
if (conn.State == System.Data.ConnectionState.Closed)

After reading your comments on Patrick's answer, I see else is missing here
if (strFileType.Trim() == ".xls")
{
connString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + path + ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=2\"";
}
else if (strFileType.Trim() == ".xlsx")
{
connString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties=\"Excel 12.0;HDR=Yes;IMEX=2\"";
}
May be connString is left blank as strFileType is not .xls.or .xlsx.

Related

Read 10000+ rows from Excel using OleDbConnection, OleDbCommand

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);

How to load multiple sheet of excel(2016) file in ssis

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

Importing Excel File to DataGridView in C#, some columns are missing

**
I want to import Excel to DataGridView and Save to Database but first
of all I'm getting some blank data in columns despite it has data.
Data is presented in Excel file as shown, I want to import this data
to my DataGridView and Save it to my database name Records.SDF.
**
private void importFromExcelToolStripMenuItem_Click(object sender, EventArgs e)
{
openFileDialog1.ShowDialog();
try
{
string filePath = openFileDialog1.FileName;
string extension = Path.GetExtension(filePath);
string conStr;
conStr = string.Empty;
switch (extension)
{
case ".xls": //Excel 97-03
string Excel03ConString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filePath + ";Extended Properties='Excel 12.0 XML;HDR=YES;';";
conStr = Excel03ConString; //string.Format(Excel03ConString, filePath);
break;
case ".xlsx": //Excel 07
string Excel07ConString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" +
filePath +
";Extended Properties='Excel 12.0 Xml;HDR=YES;IMEX=1';";
conStr = Excel07ConString;
break;
}
String name = "Sheet1";
OleDbConnection con = new OleDbConnection(conStr);
OleDbCommand oconn = new OleDbCommand("Select * From [" + name + "$]", con);
con.Open();
OleDbDataAdapter sda = new OleDbDataAdapter(oconn);
System.Data.DataTable data = new System.Data.DataTable();
sda.Fill(data);
RecordsDataGridView.DataSource = data;
DialogResult result = MessageBox.Show("Are you sure you want to Save the Recreations?", "Save Format",
MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2);
if (result == DialogResult.Yes) { SaveData(); }
}
catch (Exception ex) { MessageBox.Show(ex.Message.ToString(), "Exception Occured"); }
}
public void SaveData()
{
// Save the data.
SqlCeConnection conn =
new SqlCeConnection(
#"Data Source=|DataDirectory|\Records.sdf;Persist Security Info=False");
SqlCeCommand com;
string str;
conn.Open();
for (int index = 0; index < RecordsDataGridView.Rows.Count - 1; index++)
{
str = #"Insert Into ChequeRecords(ID,BankName,Date,AccountNo, Chequebook, ChequeNo, Payee, Amount, Remarks) Values(" + RecordsDataGridView.Rows[index].Cells[0].Value.ToString() + ", '" + RecordsDataGridView.Rows[index].Cells[1].Value.ToString() + "'," + RecordsDataGridView.Rows[index].Cells[2].Value.ToString() + "," + RecordsDataGridView.Rows[index].Cells[3].Value.ToString() + "," + RecordsDataGridView.Rows[index].Cells[4].Value.ToString() + "," + RecordsDataGridView.Rows[index].Cells[5].Value.ToString() + "," + RecordsDataGridView.Rows[index].Cells[6].Value.ToString() + "," + RecordsDataGridView.Rows[index].Cells[7].Value.ToString() + "," + RecordsDataGridView.Rows[index].Cells[8].Value.ToString() + ")";
com = new SqlCeCommand(str, conn);
com.ExecuteNonQuery();
}
conn.Close();
}
}
Not sure why I'm getting blank data in 2nd, 4th and 6th column.
My Table column does not have space but does this matter?
Try it like this.
using System;
using System.Drawing;
using System.Windows.Forms;
using Excel = Microsoft.Office.Interop.Excel;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
try
{
System.Data.OleDb.OleDbConnection MyConnection ;
System.Data.DataSet DtSet ;
System.Data.OleDb.OleDbDataAdapter MyCommand ;
MyConnection = new System.Data.OleDb.OleDbConnection("provider=Microsoft.Jet.OLEDB.4.0;Data Source='c:\\csharp.net-informations.xls';Extended Properties=Excel 8.0;");
MyCommand = new System.Data.OleDb.OleDbDataAdapter("select * from [Sheet1$]", MyConnection);
MyCommand.TableMappings.Add("Table", "TestTable");
DtSet = new System.Data.DataSet();
MyCommand.Fill(DtSet);
dataGridView1.DataSource = DtSet.Tables[0];
MyConnection.Close();
}
catch (Exception ex)
{
MessageBox.Show (ex.ToString());
}
}
}
}
If that doesn't work there must be something in Excel that is throwing it off. Step through this code sample line by line (F11) and see where it is failing.
http://csharp.net-informations.com/excel/csharp-excel-oledb.htm

Reading Custom column name instead of Value from excel using Oledb

I have Excel Data Like above. Whenever i trying to get the value as 12/1/2015. I used to get 1-Dec,2-Dec etc Like this image
bool hasHeaders = true;
string HDR = hasHeaders ? "Yes" : "No";
string strConn;
if (path.Substring(path.LastIndexOf('.')).ToLower() == ".xlsx")
strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties=\"Excel 12.0 Xml; HDR =" + HDR + ";IMEX=1;TypeGuessRows=0;ImportMixedTypes=Text\"";
else
strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + path + ";Extended Properties=\"Excel 8.0;HDR=" + HDR + ";IMEX=1;TypeGuessRows=0;ImportMixedTypes=Text\"";
retrieving the data from Excel.
OleDbDataAdapter dataHeader = new OleDbDataAdapter("SELECT * FROM [" + sheet + Coords + "]", Connection);
dataHeader.Fill(tableHeader);
please find the mistake.

Opening an Excel (created with EPPLUS) with OleDB

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.

Categories

Resources