EDN Admin
Well-known member
Cand You Hellp me to resolv this error format of the initialization string does not conform to the ole db specification. I try to read form excel (.xls) in Datagrid then i want to use those data for an optimization proces . What i need to change ? This is
the code :
<pre style="background:white; color:black; font-family:Consolas <span style="color:blue using System;
<span style="color:blue using System.Collections.Generic;
<span style="color:blue using System.ComponentModel;
<span style="color:blue using System.Data;
<span style="color:blue using System.Drawing;
<span style="color:blue using System.Linq;
<span style="color:blue using System.Text;
<span style="color:blue using System.Windows.Forms;
<span style="color:blue using System.IO;
<span style="color:blue using System.Data.SqlClient;
<span style="color:blue using System.Windows.Documents;
<span style="color:blue using System.Windows.Controls;
<span style="color:blue using ADOX;
<span style="color:blue using System.Data.OleDb;
<span style="color:blue using ADODB;
<span style="color:blue namespace Import
{
<span style="color:blue public <span style="color:blue partial <span style="color:blue class <span style="color:#2b91af Form1 : <span style="color:#2b91af Form
{
<span style="color:blue public Form1()
{
InitializeComponent();
}
<span style="color:blue public <span style="color:blue static <span style="color:blue string SelectedTable = <span style="color:blue string.Empty;
<span style="color:blue private <span style="color:blue void button1_Click_1(<span style="color:blue object sender, <span style="color:#2b91af EventArgs e)
{
<span style="color:#2b91af OpenFileDialog fdlg = <span style="color:blue new <span style="color:#2b91af OpenFileDialog();
fdlg.Title = <span style="color:#a31515 "Select file";
fdlg.InitialDirectory = <span style="color:#a31515 @"cocuments";
fdlg.FileName = txtFileName.Text;
fdlg.Filter = <span style="color:#a31515 "Excel Sheet(*.xls)|*.xls|All Files(*.*)|*.*";
fdlg.FilterIndex = 1;
fdlg.RestoreDirectory = <span style="color:blue true;
<span style="color:blue if (fdlg.ShowDialog() == <span style="color:#2b91af DialogResult.OK)
{
txtFileName.Text = fdlg.FileName;
Import();
<span style="color:#2b91af Application.DoEvents();
}
}
<span style="color:blue private <span style="color:blue void Import()
{
<span style="color:blue if (txtFileName.Text.Trim() != <span style="color:blue string.Empty)
{
<span style="color:blue try
{
<span style="color:blue string[] strTables = GetTableExcel(txtFileName.Text);
<span style="color:#2b91af frmSelectTables objSelectTable = <span style="color:blue new <span style="color:#2b91af frmSelectTables(strTables);
objSelectTable.ShowDialog(<span style="color:blue this);
objSelectTable.Dispose();
<span style="color:blue if ((SelectedTable != <span style="color:blue string.Empty) && (SelectedTable != <span style="color:blue null))
{
<span style="color:#2b91af DataTable dt = GetDataTableExcel(txtFileName.Text, SelectedTable);
dataGridView1.DataSource = dt.DefaultView;
}
}
<span style="color:blue catch (<span style="color:#2b91af Exception ex)
{
<span style="color:#2b91af MessageBox.Show(ex.Message.ToString());
}
}
}
<span style="color:blue public <span style="color:blue static <span style="color:#2b91af DataTable GetDataTableExcel(<span style="color:blue string strFileName, <span style="color:blue string Table)
{
System.Data.OleDb.<span style="color:#2b91af OleDbConnection conn = <span style="color:blue new System.Data.OleDb.<span style="color:#2b91af OleDbConnection(<span style="color:#a31515 "Provider=Microsoft.ACE.OLEDB.12.0; Data Source ={0} " + strFileName + <span style="color:#a31515 "; Extended Properties = "Excel 12.0;HDR=Yes;IMEX=1";");
conn.Open();
<span style="color:blue string strQuery = <span style="color:#a31515 "SELECT * FROM [" + Table + <span style="color:#a31515 "]";
System.Data.OleDb.<span style="color:#2b91af OleDbDataAdapter adapter = <span style="color:blue new System.Data.OleDb.<span style="color:#2b91af OleDbDataAdapter(strQuery, conn);
System.Data.<span style="color:#2b91af DataSet ds = <span style="color:blue new System.Data.<span style="color:#2b91af DataSet();
adapter.Fill(ds);
<span style="color:blue return ds.Tables[0];
}
<span style="color:blue public <span style="color:blue static <span style="color:blue string[] GetTableExcel(<span style="color:blue string strFileName)
{
<span style="color:blue string[] strTables = <span style="color:blue new <span style="color:blue string[100];
<span style="color:#2b91af Catalog oCatlog = <span style="color:blue new <span style="color:#2b91af Catalog();
ADOX.<span style="color:#2b91af Table oTable = <span style="color:blue new ADOX.<span style="color:#2b91af Table();
ADODB.<span style="color:#2b91af Connection oConn = <span style="color:blue new ADODB.<span style="color:#2b91af Connection();
<span style="color:#2b91af String _ConnectionString = <span style="color:#a31515 "Provider=Microsoft.ACE.OLEDB.12.0; Data Source =" + strFileName + <span style="color:#a31515 "; Extended Properties=Excel 8.0;HDR=YES;";
oConn.Open(_ConnectionString);
<span style="color:green // oConn.Open("Microsoft.ACE.OLEDB.12.0; Data Source ={0} " + strFileName + "; Extended Properties = "Excel 12.0;HDR=Yes;IMEX=1";", "", "", 0);
oCatlog.ActiveConnection = oConn;
<span style="color:blue if (oCatlog.Tables.Count > 0)
{
<span style="color:blue int item = 0;
<span style="color:blue foreach (ADOX.<span style="color:#2b91af Table tab <span style="color:blue in oCatlog.Tables)
{
<span style="color:blue if (tab.Type == <span style="color:#a31515 "TABLE")
{
strTables[item] = tab.Name;
item++;
}
}
}
<span style="color:blue return strTables;
}
<span style="color:blue private <span style="color:blue void dataGridView1_CellContentClick(<span style="color:blue object sender, <span style="color:#2b91af DataGridViewCellEventArgs e)
{
}
<span style="color:blue private <span style="color:blue void button2_Click(<span style="color:blue object sender, <span style="color:#2b91af EventArgs e)
{
}
<span style="color:blue private <span style="color:blue void button4_Click(<span style="color:blue object sender, <span style="color:#2b91af EventArgs e)
{
<span style="color:#2b91af Form2 form2 = <span style="color:blue new <span style="color:#2b91af Form2();
form2.ShowDialog();
}
<span style="color:blue private <span style="color:blue void saveToAFileToolStripMenuItem_Click(<span style="color:blue object sender, <span style="color:#2b91af EventArgs e)
{
}
<span style="color:blue private <span style="color:blue void openToolStripMenuItem_Click(<span style="color:blue object sender, <span style="color:#2b91af EventArgs e)
{
<span style="color:#2b91af OpenFileDialog fdlg = <span style="color:blue new <span style="color:#2b91af OpenFileDialog();
fdlg.Title = <span style="color:#a31515 "Select file";
fdlg.InitialDirectory = <span style="color:#a31515 @"c:";
fdlg.FileName = txtFileName.Text;
fdlg.Filter = <span style="color:#a31515 "Excel Sheet(*.xls)|*.xls|All Files(*.*)|*.*";
fdlg.FilterIndex = 1;
fdlg.RestoreDirectory = <span style="color:blue true;
<span style="color:blue if (fdlg.ShowDialog() == <span style="color:#2b91af DialogResult.OK)
{
txtFileName.Text = fdlg.FileName;
Import();
<span style="color:#2b91af Application.DoEvents();
}
}
<span style="color:blue private <span style="color:blue void button1_Click(<span style="color:blue object sender, <span style="color:#2b91af EventArgs e)
{
<span style="color:#2b91af OpenFileDialog fdlg = <span style="color:blue new <span style="color:#2b91af OpenFileDialog();
fdlg.Title = <span style="color:#a31515 "Select file";
fdlg.InitialDirectory = <span style="color:#a31515 @"c:";
fdlg.FileName = txtFileName.Text;
fdlg.Filter = <span style="color:#a31515 "Excel Sheet(*.xls)|*.xls|All Files(*.*)|*.*";
fdlg.FilterIndex = 1;
fdlg.RestoreDirectory = <span style="color:blue true;
<span style="color:blue if (fdlg.ShowDialog() == <span style="color:#2b91af DialogResult.OK)
{
txtFileName.Text = fdlg.FileName;
Import();
<span style="color:#2b91af Application.DoEvents();
}
}
<span style="color:blue private <span style="color:blue void txtFileName_TextChanged(<span style="color:blue object sender, <span style="color:#2b91af EventArgs e)
{
}
<span style="color:blue private <span style="color:blue void comboBox1_SelectedIndexChanged(<span style="color:blue object sender, <span style="color:#2b91af EventArgs e)
{
}
<span style="color:blue private <span style="color:blue void label2_Click(<span style="color:blue object sender, <span style="color:#2b91af EventArgs e)
{
}
}
}[/code]
View the full article
the code :
<pre style="background:white; color:black; font-family:Consolas <span style="color:blue using System;
<span style="color:blue using System.Collections.Generic;
<span style="color:blue using System.ComponentModel;
<span style="color:blue using System.Data;
<span style="color:blue using System.Drawing;
<span style="color:blue using System.Linq;
<span style="color:blue using System.Text;
<span style="color:blue using System.Windows.Forms;
<span style="color:blue using System.IO;
<span style="color:blue using System.Data.SqlClient;
<span style="color:blue using System.Windows.Documents;
<span style="color:blue using System.Windows.Controls;
<span style="color:blue using ADOX;
<span style="color:blue using System.Data.OleDb;
<span style="color:blue using ADODB;
<span style="color:blue namespace Import
{
<span style="color:blue public <span style="color:blue partial <span style="color:blue class <span style="color:#2b91af Form1 : <span style="color:#2b91af Form
{
<span style="color:blue public Form1()
{
InitializeComponent();
}
<span style="color:blue public <span style="color:blue static <span style="color:blue string SelectedTable = <span style="color:blue string.Empty;
<span style="color:blue private <span style="color:blue void button1_Click_1(<span style="color:blue object sender, <span style="color:#2b91af EventArgs e)
{
<span style="color:#2b91af OpenFileDialog fdlg = <span style="color:blue new <span style="color:#2b91af OpenFileDialog();
fdlg.Title = <span style="color:#a31515 "Select file";
fdlg.InitialDirectory = <span style="color:#a31515 @"cocuments";
fdlg.FileName = txtFileName.Text;
fdlg.Filter = <span style="color:#a31515 "Excel Sheet(*.xls)|*.xls|All Files(*.*)|*.*";
fdlg.FilterIndex = 1;
fdlg.RestoreDirectory = <span style="color:blue true;
<span style="color:blue if (fdlg.ShowDialog() == <span style="color:#2b91af DialogResult.OK)
{
txtFileName.Text = fdlg.FileName;
Import();
<span style="color:#2b91af Application.DoEvents();
}
}
<span style="color:blue private <span style="color:blue void Import()
{
<span style="color:blue if (txtFileName.Text.Trim() != <span style="color:blue string.Empty)
{
<span style="color:blue try
{
<span style="color:blue string[] strTables = GetTableExcel(txtFileName.Text);
<span style="color:#2b91af frmSelectTables objSelectTable = <span style="color:blue new <span style="color:#2b91af frmSelectTables(strTables);
objSelectTable.ShowDialog(<span style="color:blue this);
objSelectTable.Dispose();
<span style="color:blue if ((SelectedTable != <span style="color:blue string.Empty) && (SelectedTable != <span style="color:blue null))
{
<span style="color:#2b91af DataTable dt = GetDataTableExcel(txtFileName.Text, SelectedTable);
dataGridView1.DataSource = dt.DefaultView;
}
}
<span style="color:blue catch (<span style="color:#2b91af Exception ex)
{
<span style="color:#2b91af MessageBox.Show(ex.Message.ToString());
}
}
}
<span style="color:blue public <span style="color:blue static <span style="color:#2b91af DataTable GetDataTableExcel(<span style="color:blue string strFileName, <span style="color:blue string Table)
{
System.Data.OleDb.<span style="color:#2b91af OleDbConnection conn = <span style="color:blue new System.Data.OleDb.<span style="color:#2b91af OleDbConnection(<span style="color:#a31515 "Provider=Microsoft.ACE.OLEDB.12.0; Data Source ={0} " + strFileName + <span style="color:#a31515 "; Extended Properties = "Excel 12.0;HDR=Yes;IMEX=1";");
conn.Open();
<span style="color:blue string strQuery = <span style="color:#a31515 "SELECT * FROM [" + Table + <span style="color:#a31515 "]";
System.Data.OleDb.<span style="color:#2b91af OleDbDataAdapter adapter = <span style="color:blue new System.Data.OleDb.<span style="color:#2b91af OleDbDataAdapter(strQuery, conn);
System.Data.<span style="color:#2b91af DataSet ds = <span style="color:blue new System.Data.<span style="color:#2b91af DataSet();
adapter.Fill(ds);
<span style="color:blue return ds.Tables[0];
}
<span style="color:blue public <span style="color:blue static <span style="color:blue string[] GetTableExcel(<span style="color:blue string strFileName)
{
<span style="color:blue string[] strTables = <span style="color:blue new <span style="color:blue string[100];
<span style="color:#2b91af Catalog oCatlog = <span style="color:blue new <span style="color:#2b91af Catalog();
ADOX.<span style="color:#2b91af Table oTable = <span style="color:blue new ADOX.<span style="color:#2b91af Table();
ADODB.<span style="color:#2b91af Connection oConn = <span style="color:blue new ADODB.<span style="color:#2b91af Connection();
<span style="color:#2b91af String _ConnectionString = <span style="color:#a31515 "Provider=Microsoft.ACE.OLEDB.12.0; Data Source =" + strFileName + <span style="color:#a31515 "; Extended Properties=Excel 8.0;HDR=YES;";
oConn.Open(_ConnectionString);
<span style="color:green // oConn.Open("Microsoft.ACE.OLEDB.12.0; Data Source ={0} " + strFileName + "; Extended Properties = "Excel 12.0;HDR=Yes;IMEX=1";", "", "", 0);
oCatlog.ActiveConnection = oConn;
<span style="color:blue if (oCatlog.Tables.Count > 0)
{
<span style="color:blue int item = 0;
<span style="color:blue foreach (ADOX.<span style="color:#2b91af Table tab <span style="color:blue in oCatlog.Tables)
{
<span style="color:blue if (tab.Type == <span style="color:#a31515 "TABLE")
{
strTables[item] = tab.Name;
item++;
}
}
}
<span style="color:blue return strTables;
}
<span style="color:blue private <span style="color:blue void dataGridView1_CellContentClick(<span style="color:blue object sender, <span style="color:#2b91af DataGridViewCellEventArgs e)
{
}
<span style="color:blue private <span style="color:blue void button2_Click(<span style="color:blue object sender, <span style="color:#2b91af EventArgs e)
{
}
<span style="color:blue private <span style="color:blue void button4_Click(<span style="color:blue object sender, <span style="color:#2b91af EventArgs e)
{
<span style="color:#2b91af Form2 form2 = <span style="color:blue new <span style="color:#2b91af Form2();
form2.ShowDialog();
}
<span style="color:blue private <span style="color:blue void saveToAFileToolStripMenuItem_Click(<span style="color:blue object sender, <span style="color:#2b91af EventArgs e)
{
}
<span style="color:blue private <span style="color:blue void openToolStripMenuItem_Click(<span style="color:blue object sender, <span style="color:#2b91af EventArgs e)
{
<span style="color:#2b91af OpenFileDialog fdlg = <span style="color:blue new <span style="color:#2b91af OpenFileDialog();
fdlg.Title = <span style="color:#a31515 "Select file";
fdlg.InitialDirectory = <span style="color:#a31515 @"c:";
fdlg.FileName = txtFileName.Text;
fdlg.Filter = <span style="color:#a31515 "Excel Sheet(*.xls)|*.xls|All Files(*.*)|*.*";
fdlg.FilterIndex = 1;
fdlg.RestoreDirectory = <span style="color:blue true;
<span style="color:blue if (fdlg.ShowDialog() == <span style="color:#2b91af DialogResult.OK)
{
txtFileName.Text = fdlg.FileName;
Import();
<span style="color:#2b91af Application.DoEvents();
}
}
<span style="color:blue private <span style="color:blue void button1_Click(<span style="color:blue object sender, <span style="color:#2b91af EventArgs e)
{
<span style="color:#2b91af OpenFileDialog fdlg = <span style="color:blue new <span style="color:#2b91af OpenFileDialog();
fdlg.Title = <span style="color:#a31515 "Select file";
fdlg.InitialDirectory = <span style="color:#a31515 @"c:";
fdlg.FileName = txtFileName.Text;
fdlg.Filter = <span style="color:#a31515 "Excel Sheet(*.xls)|*.xls|All Files(*.*)|*.*";
fdlg.FilterIndex = 1;
fdlg.RestoreDirectory = <span style="color:blue true;
<span style="color:blue if (fdlg.ShowDialog() == <span style="color:#2b91af DialogResult.OK)
{
txtFileName.Text = fdlg.FileName;
Import();
<span style="color:#2b91af Application.DoEvents();
}
}
<span style="color:blue private <span style="color:blue void txtFileName_TextChanged(<span style="color:blue object sender, <span style="color:#2b91af EventArgs e)
{
}
<span style="color:blue private <span style="color:blue void comboBox1_SelectedIndexChanged(<span style="color:blue object sender, <span style="color:#2b91af EventArgs e)
{
}
<span style="color:blue private <span style="color:blue void label2_Click(<span style="color:blue object sender, <span style="color:#2b91af EventArgs e)
{
}
}
}[/code]
View the full article