I have a query for the SQLCE database like
select mobileNumber from customers where balance > 10000
Till now, I was looping over the SqlCeResultSet like
while (resultSet.Read())
{
mobileNumberList.Add(resultSet.GetValue(0));
}
So I want to get the list of mobile numbers in a single fetch may be as a list. How do i do that? Because the getting in a single fetch will be faster, right?
UPDATE
Few things I read after reading the answers:
SqlDataReader vs SqlDataAdapter
currently you are using a DataReader which is a read only, forward only "cursor" scrolling the result set and works only while connected to the data source.
If you would change your ExecuteReader with a data adapter and Fill dataset you would get a DataSet/DataTable object which would contain all results and can still work disconnected, after you have closed the connection.
What you want to use depends on your real use case and needs, generally DataReaders are faster than datasets so you might be already using the fastest available approach.
You can use SqlCeDataAdapter to fill DataSet in a single fetch with all the records returned by query.
e.g:
SqlCeDataAdapter adp = new SqlCeDataAdapter("select mobileNumber from customers where balance > 10000");
DataSet ds = new DataSet();
adp.Fill(ds);
I don't see the dataset getting populated here. Shouldn't it be ds.Fill(adp) bit apd.Fill(ds)? Doesn't that fill a SqlCeDataAdapter using an empty DataSet that was never populated or am I totally confused?
SqlCeDataAdapter adp = new SqlCeDataAdapter("select mobileNumber from customers where balance > 10000");
DataSet ds = new DataSet();
adp.Fill(ds);
Related
For example, I have a script that contains
insert into #temp from TABLES
insert into #temp1 from #temp
select * from #temp
I already have a C# MVC application. I just want to be able to execute the script above to display the #temp table on my front-end. I have seen the function ExecuteNonQuery(), however, it only shows the rows affected. Is there anyone that can point me a guide to solve this issue?
EDIT: I just want to run the script above and obtain the results, i don't want to rewrite the script in a framework.
Here is a sample for ExecuteNonQuery:
private void ExecuteNonQuery(string queryString, string connectionString)
{
using (SqlConnection connection = new SqlConnection(
connectionString))
{
SqlCommand command = new SqlCommand(queryString, connection);
command.Connection.Open();
command.ExecuteNonQuery();
}
}
From MSDN
And you can call your inserts like this: ExecuteNonQuery("insert into #temp from TABLES", connectionString);
For reading you can use ExecuteReader (MSDN) But if your table has many columns, this can be tedious to display all the columns. I prefer to use micro ORM (or data mapper otherwise) like Dapper - it is easy to use, minimum set up and with plenty of documentation.
Some people advise to use Entity Framework, but for this case it looks like will be an overkill. Also it has a steep learning curve.
If you want to read data from temporary table so use the ExecuteReader() and refer the following code.
using(SqlDataReader rdr = cmd.ExecuteReader())
{
while (rdr.Read())
{
var myString = rdr.GetString(0); //The 0 stands for "the 0'th column", so the
first column of the result.
// Do somthing with this rows string, for example to put them in to a list
listDeclaredElsewhere.Add(myString);
}
}
For select you have can use DataSet object and using DataAdapter fill data to DataSet
DataSet ds=new DataSet();
SqlDataAdapter da=new SqlDataAdapter(commandObject);
da.fill(ds);
DataSet ds=new DataSet();
SqlDataAdapter da=new SqlDataAdapter(commandObject);
da.fill(ds);
yes, that would work as well. but be aware that you are working with a DataSet (not DataTable on purpose?), and both DataSet and it's matching DataTable(s) are all dynamic objects without type safety (i.e. you are working with ds.Tables[0].Rows[0][2] as string; ).
good luck with the runtime exceptions.
i am creating an Application in which i want to get only new data from database, suppose i have two records in database table and i select that data in my gridview, then next time when i load data from that table i don't want that previous 2 rows in my grid, i just want new rows if available in database table.
WHAT I HAVE DONE:
I have loaded my database table in gridview and counted rows of that grid. then store rows in int variable and next time when i load grid, i again count rows in grid and if there is new row in grid, i transfer these new rows in to new datatable and then assign them to the new grid view.
in this method i am confused because this works for if only 1 row is newly added since the data is previously loaded.
WHAT I WANT:
i want best method to load only the new record in my database so that the previous data will not displayed and processed again.
please help me !
this is the sample code i am using to load data in first dataGridView
using (var con = new SqlConnection(ConStr))
{
string query = "SELECT * FROM CHECKINOUT";
using (var cmd = new SqlCommand(query, con))
{
con.Open();
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
dataGridView1.DataSource = ds.Tables[0].DefaultView;
}
}
this is the screenshot of table from which i am selecting data.
http://imgur.com/a/eIaQ6
Add column InsertDate to your table. Maintain lastDataQueriedDate in your code..
Compare these two dates in your query.
I can't comment so I'll try posting my suggestions or maybe my answer here.
First of all, Try to fix your Select Query How can you select the newest data if you use Select *?
Second From the answer of Satish Better to add new column like DateInserted to have a identifier or clue for all data from your database.
And Lastly, If you can't modify your database design, then try creating a new table that can relate or link the data of your database and insert your identifier there, like the DateInserted.
P.S Try Satish answer. It is more easier.
My code is construct to read data in a datagridView named (dg) from my database.
Its actually work well whit a SqlDataAdapter.
First Is it a good idea to change my SqlDataAdapter for a SqlCommand ?
If YES
I want to use this for change my SqlDataAdapter.
//SqlCommand cmd = new SqlCommand("Command String", con);
//SqlDataReader readdata;
CODE
SqlConnection con = new SqlConnection(dc.Con);
SqlDataAdapter da = new SqlDataAdapter();
con.Open();
da.SelectCommand = new SqlCommand("SELECT * FROM tblContacts", con);
DataTable dt = new DataTable();
da.Fill(dt);
con.Close();
dg.DataSource = dt;
I typically use the DataAdapter for data access when I need to do data-binding to controls. It's very convenient and efficient in those scenarios. Otherwise, I use the Command objects directly. Performance-wise, I'm inclined to agree w/ punzki. There shouldn't be much difference between the two.
http://msforums.ph/forums/p/9057/9057.aspx
Actually, from what I remember, SqlDataAdapter uses SqlDataReader to retrieve records. So It's always good to use SQLDataReader when you're going to just retrieve data from the backend. But if you're going to retrieve data and then update (insert, update, delete) data later on, then it's better to use SqlDataAdapter. I think it's more efficient that way.
http://msforums.ph/forums/t/29256.aspx
There IS an effect on performance.
SqlDataReader is no doubt faster than a SqlDataAdapter as the DataReader reads data in a forward only mode and you can get a specific type of value returned back to you, such as a string or int etc... however with the SqlDataAdapter, it will fill a datatable or dataset will records it finds in your select statement, taking with it the correct value type for the columns and is a disconnected representation of in memory database and is ideal and easier to use if you are going to show large amounts of records to a binding source, as with a SqlDataReader, it is not possible but to only obtain a value for a column you specify per row.
The SqlDataAdapter also allows you to Update, Delete or Insert rows into the Dataset/DataTable which is an advantage and will execute the appropriate command, if you implemented it correctly, based on how the rows were modified in the Dataset/DataTable.
SqlDataAdapter is expensive compared to a fast forward read on the SqlDataReader, and has more advantages but entirely depends on your solution and what you require.
You are stating that you are going to show alot of records, whilst that is all very well, it would be even better for the benefit of the performance and memory usage to only obtain records that you require to be shown and a SqlDataAdapter would be suitable for this also but still you are required to select records which are the ones you will most likely show to the user, either by input search criteria, or perhaps by paging.
http://social.msdn.microsoft.com/Forums/en-US/adodotnetdataproviders/thread/c2d762fd-f4a0-4875-8bb8-42f7480e97c8/
I have a DataTable with a lot of rows (Over a hundred million) and am writing an application that needs to insert into that table.
I will be using OleDbDataAdapter for the job and I am puzzled whats the best way to do this. I only need to insert into this enormous table, however I don't want to hard code the insert statement into application.
I figured I could use
OleDbDataAdapter adapter = new OleDbDataAdapter();
adapter.SelectCommand = new OleDbCommand("select * from table_name");
OleDbCommandBuilder cb = new OleDbCommandBuilder(adapter);
...
adapter.Fill(data_set_name, "Table_name");
But this would be really bad since I don't need/want the data and the memory usage would be awful. So I was interesting if I could alter SelectCommand with TOP? It would look like so:
adapter.SelectCommand = new OleDbCommand("select TOP 1 * from table_name");
Now the Fill command would be really fast and I would have the data I needed for all the future insert statements. I could add rows to datatable and then just call
adapter.Update(data_set_name, "Table_name");
Would this work? And is this a valid / recommended way of doing this? It is really important that the application is fast and uses only the necessary resources. Is there a better way of doing this?
Thank you for your input!
If you don't need the data you can change the select command to
SELECT * FROM Table_Name WHERE 1=2
Then you won't get any rows back
IMO, the best way would be to:
Use the OleDbDataAdapter.FillSchema(data_set_name, SchemaType.Source) method to create the DataTable with a structure mapped from the datasource. You are basically trying to do the same thing by pulling a single row in your Select statement. Your Select statement in this case could remain "select * from table_name". I believe that you do not need to call the OleDbDataAdapter.Fill method now.
Instead of using a CommandBuilder, create your InsertCommand statement yourself.
I know this might be a bit awkward but I am trying to modify the order of certain columns in a MS Access database in C# with OLE DB. How can I commit a certain change in the order of the columns of a datatable in a dataset? If that is not possible, how can I reorder columns of database table by using a dataset?
Here is a sample of what I have (in C#):
command.Connection = conn;
command.CommandText = tableName;
command.CommandType = CommandType.TableDirect;
adapter = new OleDbDataAdapter(command);
dataset = new DataSet(tableName);
adapter.Fill(dataset, tableName);
dataset.Tables[0].Columns[dataset.Tables[0].Columns.Count-1].SetOrdinal(CB_PositionCol.SelectedIndex);
dataset.Tables[0].AcceptChanges();
The AcceptChanges does not seem to work for what I wish to do since I believe it only commits changes in DataRows...
Thank you for any help!
Use an ALTER TABLE statement with an OleDbCommand. DataSet, DataTable and DataTableAdapters are meant to be transparent to the underlying structure of the data. You can actually use Table Adapters to transform data from two different structures.
http://www.functionx.com/vbnet/oledb/Lesson02.htm