External table not in expected format - c#

I know this question have been asked couple of times here but I followed the suggestions and still getting an error : External table is not in expected format.
My application has DataGrid that displays properly the excel file content and when I refresh the DataGrid it shows added row in excel file.But when I open the excel file to check if the row is added ,it's not-no new row is saved.
I'd appreciate your help.
My Wpf application has a connection string in App.config:
add name="ExcelConnectionString" connectionString="Provider=Microsoft.ACE.OLEDB.12.0;Data Source=excel_file\\events2.xlsx;Extended Properties="Excel 12.0 Xml;HDR=YES""
I'm using OLEDB to connect with excel file:
public Events()
{
InitializeComponent();
System.Data.OleDb.OleDbConnection excelConnection;
System.Data.OleDb.OleDbCommand myCommand = new System.Data.OleDb.OleDbCommand();
var connectionString = ConfigurationManager.ConnectionStrings["ExcelConnectionString"].ToString();
excelConnection = new System.Data.OleDb.OleDbConnection(connectionString);
try
{
String sql = null;
excelConnection.Open();
myCommand.Connection = excelConnection;
sql = "Select * from [events$]";
myCommand.CommandText = sql;
myCommand.ExecuteNonQuery();
System.Data.OleDb.OleDbDataAdapter dataAdp = new System.Data.OleDb.OleDbDataAdapter(myCommand);
DataTable dt = new DataTable("events");
dataAdp.Fill(dt);
dgData.ItemsSource = dt.DefaultView;
dataAdp.Update(dt);
excelConnection.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
finally
{
excelConnection.Close();
}
}

Related

XLWorkbook only saving first row of data from an SQL Query

I am trying to use C# to get data from MySql database and export that data into Excel. For some reason, I am only getting the first row of data. I am not sure what is wrong. Below is my code:
public void MySqlConnectionHandler(string SQL_QUERY)
{
try
{
ConnectionString = " SERVER = some_ip_address;"
+ "DATABASE = database ;"
+ "UID=user;"
+ "PASSWORD=password";
connection = new MySqlConnection(ConnectionString);
MySqlDataAdapter dataAdapter = new MySqlDataAdapter();
dataAdapter.SelectCommand = new MySqlCommand(SQL_QUERY,connection);
DataTable dbdataset = new DataTable();
dataAdapter.Fill(dbdataset);
XLWorkbook wb = new XLWorkbook();
wb.Worksheets.Add(dbdataset, "myworktable");
wb.SaveAs("myworktable.xlsx");
MessageBox.Show("Connected");
}
catch (MySqlException ex)
{
MessageBox.Show(ex.Message, "MySQL Query Failure");
}
}
static void Main(string[] args)
{
AggAutomation agg = new AggAutomation();
agg.MySqlConnectionHandler("SELECT * FROM data_base;");
}
Resolved. It was a silly mistake. The datatable that I was reading in actually only had one data point

Failed to read correct date from excel (oledb)

We are trying to automate some of the processes that our done daily, for this we have made some Excel files which store the instruction for these processes. In a few such excel files we are using =TODAY() function to populate the cell with today's date.
Now while trying to read these excel files, I am not able to get the correct date in the datatable if excel is not opened at least once on a particular date. The date picked is from the last time the excel file was opened. This is causing a major problem while automating the problem at hand.
I would want to know if this can be achieved using OLEDB drivers and if not what other options are there?
This is the code for reading from the excel, the datasource is constructed at runtime.
private string excelObject = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties=\"Excel 8.0;HDR=YES\"";
public DataTable GetSchema()
{
DataTable dtSchema = null;
if (this.Connection.State != ConnectionState.Open) this.Connection.Open();
dtSchema = this.Connection.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, "TABLE" });
return dtSchema;
}
public DataTable ReadTable(string tableName, string criteria)
{
try
{
DataTable resultTable = null;
if (this.Connection.State != ConnectionState.Open)
{
this.Connection.Open();
onReadProgress(10);
}
string cmdText = "Select * from [{0}]";
if (!string.IsNullOrEmpty(criteria))
{
cmdText += " Where " + criteria;
}
OleDbCommand cmd = new OleDbCommand(string.Format(cmdText, tableName));
cmd.Connection = this.Connection;
OleDbDataAdapter adpt = new OleDbDataAdapter(cmd);
onReadProgress(30);
DataSet ds = new DataSet();
onReadProgress(50);
adpt.Fill(ds, tableName);
onReadProgress(100);
if (ds.Tables.Count == 1)
{
return ds.Tables[0];
}
else
{
return null;
}
}
catch
{
MessageBox.Show("Table Cannot be read");
return null;
}
}

How to properly import excel columns that have wrap text enabled using Microsoft.ACE.OLEDB and SqlBulkCopy

I'm importing rows from an excel sheet into an sql server table using bulk insert.
Some columns in the excel source have wrap text enable and they are causing some issues.
If have say "wrapped text (1)" and "wrapped text (2)", they will appear like below in the excel cell.
wrapped text (1)
wrapped text (2)
The problem i'm getting is, when that row is imported into SQL server,they will appear like below
wrapped text (1)wrapped text (2)//Note that there is no space between the two texts
I would wish that they appear like below
wrapped text (1) wrapped text (2)//note the space between them
Below is the peace of code i'm using for the import
public static void insertdata1(string strexcelConnectionString, string strcommand, string strsqlConnectionString, string strtrunsqlQuery, string strtablename)
{
using (OleDbConnection connection = new OleDbConnection(strexcelConnectionString))
{
connection.Open();
OleDbCommand command = new OleDbCommand(strcommand, connection);
//truncate the table before inserting new data.
SqlConnection cnntrunc = new SqlConnection(strsqlConnectionString);
//open the connection.
cnntrunc.Open();
//begin the transaction.
SqlTransaction myTrans = cnntrunc.BeginTransaction();//New a transaction
SqlCommand truntble = new SqlCommand();
truntble.Transaction = myTrans;
truntble.Connection = cnntrunc;
try
{
//Create DbDataReader to Data Worksheet.
using (DbDataReader dr = command.ExecuteReader())
{
//Bulk Copy to SQL Server.
using (SqlBulkCopy bulkCopy = new SqlBulkCopy(strsqlConnectionString))
{
bulkCopy.DestinationTableName = strtablename;
bulkCopy.WriteToServer(dr);
}
//commit the transaction.
myTrans.Commit();
}
}
catch (OleDbException ex)
{
//my error handling code here
}
catch (InvalidOperationException ex)
{
myTrans.Rollback();
//more logging here
throw ex;
}
finally
{
cnntrunc.Close();
}
}
}
And blow is my excel connection string
strexcelConnectionString = #"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + Server.MapPath(strfilename) + "; Extended Properties=\"Excel 12.0 XML;HDR=YES;IMEX=1;\"";
After my BulkInsert, i'm updating my problematic columns as described here. How to check my data in SQL Server have carriage return and line feed?

How to query a Foxpro .DBF file with .NDX index file using the OLEDB driver in C#

I have a Foxpro .DBF file. I am using OLEDB driver to read the .DBF file. I can query the DBF and utilize its .CDX index file(cause it is automatically opened). My problem is that I want to query it with the .NDX index file (which is not automatically opened when the .DBF is opened). How can I open the .NDX file in C# using OLEDB driver cause the DBF is really big to search for a record without the index? Thanks all! Here is the code I am using to read the DBF.
OleDbConnection oleDbConnection = null;
try
{
DataTable resultTable = new DataTable();
using (oleDbConnection = new OleDbConnection("Provider=VFPOLEDB.1;Data Source=P:\\Test\\DSPC-1.DBF;Exclusive=No"))
{
oleDbConnection.Open();
if (oleDbConnection.State == ConnectionState.Open)
{
OleDbDataAdapter dataApdapter = new OleDbDataAdapter();
OleDbCommand command = oleDbConnection.CreateCommand();
string selectCmd = #"select * from P:\Test\DSPC-1 where dp_file = '860003'";
command.CommandType = CommandType.Text;
command.CommandText = selectCmd;
dataApdapter.SelectCommand = command;
dataApdapter.Fill(resultTable);
foreach(DataRow row in resultTable.Rows)
{
//Write the data of each record
}
}
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
finally
{
try
{
oleDbConnection.Close();
}
catch (Exception e)
{
Console.WriteLine("Failed to close Oledb connection: " + e.Message);
}
}
ndx files wouldn't be opened by default and those are a thing of the past really, why wouldn't you simply add your index to your CDX. If it is not an option, then ExecScript suggestion by DRapp is what you can do. He was very close. Here is how you could do that:
string myCommand = #"Use ('P:\Test\DSPC-1') alias myData
Set Index To ('P:\Test\DSPC-1_Custom.NDX')
select * from myData ;
where dp_file = '860003' ;
into cursor crsResult ;
nofilter
SetResultset('crsResult')";
DataTable resultTable = new DataTable();
using (oleDbConnection = new OleDbConnection(#"Provider=VFPOLEDB;Data Source=P:\Test"))
{
oleDbConnection.Open();
OleDbCommand command = new OleDbCommand("ExecScript", oleDbConnection);
command.CommandType = CommandType.StoredProcedure;
command.Parameters.AddWithValue("code", myCommand);
resultTable.Load(cmd.ExecuteReader());
oleDbConnection.Close();
}
Your connection string should only reference the PATH to where the .dbf files are located.
Then, your query is just by the table name.
new OleDbConnection("Provider=VFPOLEDB.1;Data Source=P:\\Test\\;Exclusive=No"))
selectCmd = #"select * from DSPC-1 where dp_file = '860003'";
As for using the .NDX, how / where was that created... Is that an old dBASE file you are using the Visual Foxpro driver for?
If it is a separate as described, you might need to do via an ExecScript() to explicitly open the file first WITH the index, THEN run your query. This is just a SAMPLE WITH YOUR FIXED value. You would probably have to PARAMETERIZE it otherwise you would be open to sql-injection.
cmd.CommandText = string.Format(
#"EXECSCRIPT('
USE DSPC-1 INDEX YourDSPC-1.NDX
SELECT * from DSPC-1 where dp_file = '860003'" );
Also, you might have issue with your table names being hyphenated, you may need to wrap it in [square-brackets], but not positive if it is an issue.

Data Corruption: select into to create a duplicate table as alternative for alter table for excel using ado.net

I have tried to use Alter table on an excel spreadsheet to add a new column to existing spreadsheet but found out that I cannot use alter table on excel.
From browsing the net I found that I can use select into to create a duplicate worksheet with new column in it. Here is the code for this.
static private bool CopyAndCreateNewSheet()
{
try
{
DataTable dataTable = new DataTable();
using (OleDbConnection conn = new OleDbConnection(ConfigHelper.ConnectionStringReadWriteWorkBook))
{
conn.Open();
//string selectQuery = "SELECT 0 As IsProcessed, * INTO [Copy] FROM [" + ConfigHelper.WorkSheetName + "$]";
string selectQuery = "SELECT 0 As IsProcessed, * INTO [Excel 12.0;HDR= Yes;DATABASE=c:\\data\\Apr12.xlsx].[copyd] FROM [" + ConfigHelper.WorkSheetName + "$]";
OleDbCommand cmd = new OleDbCommand(selectQuery, conn);
int count = cmd.ExecuteNonQuery();
MessageBox.Show(count.ToString());
conn.Close();
}
}
catch (OleDbException e)
{
MessageBox.Show(e.Message.ToString());
}
catch (Exception e)
{
MessageBox.Show(e.Message);
}
return true;
}
My config for connectionstring is as follows.
<add name="ReadWriteWorkBook" connectionString='Provider = Microsoft.ACE.OLEDB.12.0; Data Source="c:\data\Apr12.xlsx"; Extended Properties="Excel 12.0;HDR=Yes;READONLY=FALSE"'/>
I have tried with both commented query and uncommented query to run select into but with no luck...
When I open my workbook after above method call I am getting the following error. "Excel found unreadable connect in the file. do you want to recover the contents of this workbook? If you trust the source of this workbook, click yes."
When I click yes, my new tab is there and also new headers are there but no data as excel has removed data from it.
Any help much appreciated.
Thanks in advance.
G'day,
I was able to replicate this and have it work perfectly. The code I used looks like this:
public static void Main(string[] Arguments)
{
try
{
string sConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=\"Test.xlsx\";Extended Properties=\"Excel 12.0;HDR=Yes;READONLY=FALSE\"";
DataTable dataTable = new DataTable();
using (OleDbConnection conn = new OleDbConnection(sConnectionString))
{
conn.Open();
string selectQuery = "SELECT 0 As IsProcessed, * INTO [Excel 12.0;HDR= Yes;DATABASE=Test.xlsx].[copyd] FROM [TestWorkSheet1$]";
OleDbCommand cmd = new OleDbCommand(selectQuery, conn);
int count = cmd.ExecuteNonQuery();
Console.WriteLine(count.ToString());
conn.Close();
}
}
catch (OleDbException e)
{
Console.WriteLine(e.Message.ToString());
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
Note that this code was modified slightly to run inside a console application. This doesn't really help you out at all however. Excel can be a real pain to work with sometimes, have you considered investigating the following:
Permissions
Does the original file open in Excel without issues (i.e., is that file readable by Excel in the first place?)
Has Excel somehow TSR'd and is interfering with your project in some fashion? I've seen this happen before, usually when Excel isn't closed properly
Good luck.
Cheers!

Categories