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.
Related
I have timer-function. It handles data from sql database every N time (it's doesn't matter actually what time). The subsequent algorithm does not work. Or I don't know how to make it work.
I put data into Dataset.
const string query = "SELECT id FROM SomeTable LIMIT 100";
await using var cmd = new MySqlCommand(query, connection);
var sqlAdapter = new MySqlDataAdapter(cmd);
sqlAdapter.TableMappings.Add("Table", "SomeTable");
sqlAdapter.Fill(_mapping);
_mapping.AcceptChanges();
Where _mapping is dataset.
If this is the first filling of the dataset, then just fill it.
If this is not the first filling, then try to update the data in it. I tried to use sqlAdapter.Update(_mapping). But it doesn't update any data Or I cant get updated data. I tried fill it again and check _mapping.hasChanges(DataRowState.Modified | DataRowState.Added))
But it hasn't any changes.
So how can I fill dataset and catch changed or added rows while I get data from Database?
Or may be I need to use another things like dataTable or something else?
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);
I have mysql database and there is a table with a name Cars.
I bring Cars table from mysql by using MySqlDataAdapter then fill a datatable.
I use only that datatable to feed the controls that represent a car information on the form and by using a loop I take the next row in this.carsDT. then I feed datagridview source from this data table
MySqlDataAdapter carsMSDA = new MySqlDataAdapter(mysql, conn);
carsMSDA.Fill(this.carsDT);
dataGridViewCars.DataSource = this.carsDT;
The problem is after updatting a row in anothe place of the code, I call again the lines above to make something like refresh or renew this.carsDT. But I find that rows in this.carsDT and dataGridViewCars are doubled for e.g.
if there was 3 rows they will become 6 rows, 3 are the old from previouse state bfore updating a row, and 3 other one of them the updated row.
So I used new datatable like this:
MySqlDataAdapter carsMSDA = new MySqlDataAdapter(mysql, conn);
DataTable carsDT = new DataTable();
carsMSDA.Fill(carsDT);
this.carsDT = carsDT;
//carsMSDA.Fill(this.carsDT);
dataGridViewCars.DataSource = this.carsDT;
I want to notice that after close the form and run again I find it normal and uptaded as it should be.
Is this good idea?
We see that System.Data.CommandType has only 3 enums: "StoredProcedure", "TableDirect" and "Text". Why don't we see "View" as an option? What is the Framework specific reason Microsoft does not provide this option?
I am specifically talking about the SQL Server Views here.
Your answer will be highly appreciated.
Because a View is accessible via TableDirect or Text. The most common access is Text anyway because you're generally doing something like this:
DataTable dt = new DataTable();
using (SqlConnection c = new SqlConnection(cString))
{
using (SqlDataAdapter sda = new SqlDataAdapter(sql, c))
{
sda.SelectCommand.Parameters.Add("parm1", value1);
c.Open();
sda.Fill(dt);
}
}
Finally, a view is actually represented as a table in SQL Server. It has a real table definition, it just happens to be built with a query.
Most of the times View is not used by itself but rather as a part of a query or SP. Hence it's not necessary to provide a dedicated CommandType. Same thing goes for Functions - there's no CommandType.Function.
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.