C# OLEDBConnection to Excel - c#

I am copying an Excel sheet into a Datatable as such:
OleDbCommand command = new OleDbCommand();
command = new OleDbCommand("Select * from [working sheet$]", oleDBConnection);
OleDbDataAdapter dataAdapter = new OleDbDataAdapter();
dataAdapter.SelectCommand = command;
dataAdapter.Fill(dt);
Is there a similar method where I can just simply copy the datatable back to an Excel sheet? The examples I keep finding are copying cell by cell, but this can be noticably slow with large data sets.
Thanks

You're looking for the DataAdapter.Update method, which applies any changes made in the DataTable to the database (or spreadsheet, in this case)

Don't really know much about OleDB with Excel, but since you mentioned a Database, I assume this runs on a server? Microsoft actually does not recomment running Excel on a server.
I would use OpenXML for tasks like this. It's a bit more complicated, but it's save and stable.

Related

WPF DataGrid - MS Access Table - Column headings display but not data is pulled

I followed the information found here
https://learn.microsoft.com/en-us/dotnet/desktop/wpf/controls/walkthrough-display-data-from-a-sql-server-database-in-a-datagrid-control?view=netframeworkdesktop-4.8
This article is for using SQL server as the source but I'm using Access accdb instead. This seemed to be the most straightforward article that referenced pulling data. I'm very new to WPF/C# but trying to learn as I think I can build a better interface for a tool that is built in Access right now since there are more UI options and I want to expand a bit outside VBA which I know very well but is a less than ideal language.
Anyhow, below I think is the relevant code. If I preview the data set in designer, I can see it does pull back data into a control that is a data grid itself and it mentions this Fill, GetData() so maybe there is somewhere I'm supposed to call that.
If I can figure out where/how I'll answer my own question and close this.
XAML:
<DataGrid Name="processScheduleGrid" />
C#:
var query = from ps in dataSet.PROCESS_SCHEDULES
orderby ps.SCHEDULE_NAME
select new { ps.SCHEDULE_NAME, ps.PROCESS_NAME, ps.DAY_OF_MONTH, ps.DAY_OF_WEEK, ps.START_TIME, ps.END_TIME, ps.STATUS, ps.STATUS_TM, ps.PARALLEL, ps.MINUTES_BEFORE_REPEAT, ps.ENABLED, ps.ERR_NUM };
processScheduleGrid.ItemsSource = query.ToList();
I was able to get this working with another object model that is a lot more familiar looking to me being used to working in access with both DAO and ADO. It's more similar to the latter
The revised code is much simpler, and it doesn't do any of the auto-generating that I really am not liking while learning to use C# and Visual Studio more. I think I could have made the above work if I had gone through every column and added handling for nulls but this code handles that with no issue, so it seems like this is the right way to go vs the other object model (System.Windows.Data vs System.Data/System.Data.OleDb) and syntax.
OleDbConnection con = new OleDbConnection(provider);
OleDbCommand cmd = new OleDbCommand(sql, con);
con.Open();
cmd.CommandType = System.Data.CommandType.Text;
OleDbDataAdapter da = new OleDbDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds, "PROCESS_SCHEDULES");
processScheduleGrid.ItemsSource = ds.Tables["PROCESS_SCHEDULES"].DefaultView;

asp.net oledbcommand return all rows

I am using Oledbconnection to connect to a Microsoft Access database, and I am using OleDbCommand to retrieve some information. I have a query in the database called retrieveInfo, which retrieves 3 rows of data. There are some duplicates in the fields but that's how it's supposed to be. My data looks like this:
Name Email
A A#gmail.com
B A#gmail.com
B C#gmail.com
My C# code behind looks like this:
DataTable dt = new DataTable();
using (OleDbConnection conn = new OleDbConnection(connectionString))
{
string query = "SELECT * FROM retrieveInfo";
try
{
conn.Open();
DataTable info = new DataTable();
OleDbCommand command = new OleDbCommand(query, conn);
OleDbDataAdapter dataAdapter = new OleDbDataAdapter(command);
info.Clear();
dataAdapter.Fill(info);
}
I ran the query retrieveInfo in MS Access and it returned 3 rows like shown above. However when I run this command using C# and loaded the data into a datatable, it only shows 2 rows. The datatable only has 1st and 2nd row. I don't know if this has anything to do with the original table properties, or is my C# code wrong? I also tried using a data reader, execute reader and using a while loop to read data. But it also only return 2 rows.
Any help would be appreciated!
Thank you
Chances are you are using a 'LIKE' statement somewhere in retrieveInfo's definition, either in that query itself or in one of its sub-queries. OleDbDataAdapter cannot resolve MS Access LIKE statement as it's written in Access SQL. You need to use ANSI LIKE (see link).
Stackoverflow Explanation
Now I use
Like "oldmcdonal%" Or Like "oldmcdonal*"
This provides the expected result set regardless of whether it's executed in MS Access or via .net code.

Working with large csv files

I'm trying to find out some of the best ways to work with large data files. I have a scenario where I will have several CSV files, of which I would like the ability to query data. One of the csv files I will read line by line but I need to be able to query a second CSV file based on a key from the line I'm currently reading. I don't want to (at least I don't think) load the entire CSV into a memory object as they can be millions of lines and will eat tons of RAM. I've considered writing them to some sort of database file on the fly but that just doesn't seem efficient as your essentially duplicating the data. Any suggestions?
You can try OleDb, load data in data table using data adapter, and perform query on it. This link has explained
String conn = #"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\;
Extended Properties=""Text;HDR=No;FMT=Delimited""";
OleDbConnection cn = new OleDbConnection(conn);
OleDbCommand cmd = new OleDbCommand(#"SELECT * FROM C:\Temp\teams.csv", cn);
OleDbDataAdapter da = new OleDbDataAdapter(cmd);
cn.Open();
DataTable dt = new DataTable();
da.Fill(dt);

Put SqlDataReader data into a DataTable while i process it

I want to query a database and with the results, i want to process them. While im processing them, some of them will need to be inserted into another database. Since i cannot run another query with an open SqlDataReader (that i know of). I was thinking about putting the data from the SqlDataReader into a DataTable while i process it. Is there a built in way to do this or is there another solution that can accomplish the same idea?
SqlDataReader reader = com.ExecuteReader();
DataTable dt = new DataTable();
dt.Load(reader);
With the rest of the standard set-up and tear-down of SqlCommand objects, of course.
The easiest way would be to use a DataAdapter to fill a datatable. Then process the data and update the database. Filling a dataset does not tie up the connection once the fill is complete.

White spaces and oledb

I'm reading an excel file using OleDb on ASP. NET (C#).
All the information is returned ok and I was surprised to see that even the cell type defined on the Excel file is returned to my code.
The problem is that I have column where all the cells "general" types and since the values are only numbers Excel assumes it to be Number.
If there are no spaces olebd driver returns the right value to my code but if there are space it returns ""...
Here's how I'm getting the information:
OleDbConnection oleDbConn = new OleDbConnection(connString);
oleDbConn.Open();
OleDbCommand oleDbComm = new OleDbCommand("SELECT * FROM [Sheet1$]", oleDbConn);
OleDbDataAdapter oleDbDtAdapter = new OleDbDataAdapter();
oleDbDtAdapter.SelectCommand = oleDbComm;
DataSet dtSet = new DataSet();
oleDbDtAdapter.Fill(dtSet, "SMSs");
Object testZeroZero = dtSet.Tables[0].Rows[0][0];
I can't go to the Excel and change the cell type to "text" because the end user must not worry on changing this so how can I overcome this?
Regards!
Have you considered your connection string?
"IMEX=1;" tells the driver to always
read "intermixed" (numbers, dates,
strings etc) data columns as text.
Note that this option might affect
excel sheet write access negative.
-- http://www.connectionstrings.com/excel

Categories