Extensive use of SqlDataSource and too many 3306 time wait connections - c#

Is it possible to exist the connection between the extensive use of asp:SqlDataSource and a lot of 3306 ports in time wait status?
Is it true that the asp:sqldatasource closes the connection automatically? if does not, how to close the connection made by a slqdatasource?
Thanks in advance,
Firmino

Try <asp:SqlDataSource DataSourceMode="DataSet" />.
From MSDN:
The data retrieval mode identifies how a SqlDataSource control retrieves data from the underlying database.
When the DataSourceMode property is set to the DataSet value, data is loaded into a DataSet object and stored in memory on the server. This enables scenarios where user interface controls, such as GridView, offer sorting, filtering, and paging capabilities.
When the DataSourceMode property is set to the DataReader value, data is retrieved by a IDataReader object, which is a forward-only, read-only cursor. The specific type of the IDataReader object depends on the NET data provider that the SqlDataSource uses, which is identified by the ProviderName property. By default, the SqlDataSource control uses the provider for Microsoft SQL Server, the System.Data.SqlClient, and the data reader is a SqlDataReader object.
DataReader requires open connection to operate - it returns data directly from DB. In constrast DataSet loads data from DB to memory so connection are opened only for actual read / update / insert / delete and can be closed (=returned to pool) afterwards.
Source: SqlDataSource.DataSourceMode

Related

BindingSource.EndEdit() vs TableAdapterManager.UpdateAll()

In the .NET framework, in order to save data into a databse item, one has to use:
Me.Validate();
Me.CustomersBindingSource.EndEdit();
Me.TableAdapterManager.UpdateAll(Me.CustomerDataSet);
Can someone explain me why? What's going on behind the scenes? If the .EndEdit() "applies changes to the underlying data source" why isn't it enough to apply those changes?
It is enough to "apply those changes"... to the data source. The data source is a DataTable, which is an object in your application. The UpdateAll call is what saves the changes from that DataTable - in fact, all DataTables in your DataSet - to the database.
ADO.NET is based on a disconnected model. That means that your application is not directly connected to your database. Using ADO in VB6, changes you made to a Recordset were made directly to the database. Not so in ADO.NET. When you call Fill, a connection to the database is opened, data is copied from the database into a DataTable and then the connection is closed. Any changes you make locally affect only that local copy. When you call Update or UpdateAll, the connection is opened again and the local changes saved to the database.

Is it possible to keep DataSet automatically synced with a SQLite database?

I'm trying to learn to use SQLite, but I'm very frustrated and confused. I've gotten as far as finding System.Data.SQLite, which is apparently the thing to use for SQLite in C#.
The website has no documentation whatsoever. The "original website", which is apparently obsolete from 2010 onwards, has no documentation either. I could find a few blog tutorials, but from what I can tell their method of operation is basically:
Initialize a database connection.
Feed SQL statements into the connection.
Take out stuff that comes out of the connection.
Close connection.
I don't want to write SQL statements in my C# code, they're ugly and I get no assistance from the IDE because I have to put the SQL code in strings.
Can't I just:
Create a DataSet.
Tell the DataSet that it should correspond to the SQLite database MyDB.sqlite.
Manipulate the DataSet using its member functions.
Not worry about SQLite because the DataSet automatically keeps itself in sync with the SQLite database on disc.
I know that I can fill a DataSet with the contents of a database, but if I want access to the entire database I will have to fill the DataSet with all of its contents. If my database is 1 GB, I have just used up 1 GB of RAM (not to mention the time needed to write all of it at once).
Can't I simply take a SQLite database connection and pretend it's just an ordinary DataSet (that perhaps needs to be asked occasionally if it's done syncing yet)?
The answer to the question is no.
No you cannot simply take a SQLite connection pretend it's just a DataSet.
If you don't want to code SQL statements then consider Entity Framework.
Using SQLite Embedded Database with Entity Framework and Linq-to-SQL
You shouldn't treat a DataSet as a database. It's just a result of a query.
You query the database to get a subset of data (you never want ALL the data from your DB) and this subset is used to populate your DataSet.
You are required to synchronize your changes manually because DataSet doesn't know which updates should be a part of which transaction. This is your system knowledge.
The DataSet is an in memory cache and will only synchronize to the underlying data store when the developer allows it. You could put a timer wrapper around in and do it on a schedule but you still need to keep the Dataset and data store synchronized manually.
Storing 1GB+ of data is really not recommended as the memory usage would be very high and the performance very low. You also don't want to be sending that amount of data over a network or god forbid an internet connection.
Why would you want to keep 1GB of data in memory?

Is a Dataset the same as a Recordset

I used to work in VB.net and used Dataset's all the time when working with ADO, but now i'm Working in C# and the Research is showing me a lot of Recordsets.
Are they the same thing?
If no what is the difference?
Datasets vs RecordSets
Essentially it is to do with how it fetches the data and allows you to intereact with it, recordsets would typically only allow you to fetch data from one table at a time (with default settings) whereas datasets can retrieve the entire set of data. there is more information at that link
Dataset is a connectionless data holder whereas RecordSet is connection oriented Data holder.Though DataSet you can refer more than 1 table at a time, but in the case of Recordset only 1 table is processed at a time. Through Dataset you can process more than 1 record,but in case of recordset recordset you have to make travesel to each record and after that you can make processing.
a direct quote backing up what i said
Difference between ADO.NET Dataset and ADO Recordset?
ADO.NET is an object-oriented set of libraries that allows you to interact with data sources.
http://www.job4india.in/interview-questions/net-interview-questions
ADO :-
1.It is a COM based library.
2.Classic ADO requires active connection with the data store.
3.Locking feature is available.
4.Data is stored in binary format.
5.XML integration is not possible.
ADO.NET :-
1.It is a CLR based library.
2.ADO.NET architecture works while the data store is disconnected.
3.Locking feature is not available.
4.Data is stored in XML.
Read More :- http://www.job4india.in/net-interview-questions/what-difference-between-ado-and-adonet
No, the DataSet class (System.Data namespace) has the same name in C#. I'm not familiar with anything called recordset in ADO.NET (whereas I am with DataSet). Could you post some examples?

DataSet or Reader or What?

When using a Class to get one row of Data from the Database what is best to use:
A DataSet?
A Reader and do what store the data in a Structure?
What else?
Thanks for your time, Nathan
A DataReader is always your best choice--provided that it is compatible with your usage. DataReaders are very fast, efficient, and lightweight--but they carry the requirement that you maintain an active/open db connection for their lifecycle, this means they can't be marshalled across AppDomains (or across webservices, etc).
DataSets are actually populated by DataReaders--they are eager-loaded (all data is populated before any is accessed) and are therefore less performant, but they have the added benefit of being serializable (they're essentially just a DTO) and that means they're easy to carry across AppDomains or webservices.
The difference is sometimes summed up by saying "DataReaders are ideal for ADO.NET ONLINE (implying that it's fine to keep the db connection open) whereas DataSets are ideal for ADO.NET OFFLINE (where the consumer can't necessarily connect directly to the database).
DataAdapter (which fills a DataSet) uses a DataReader to do so.
So, DataReader is always more lightweight and easier to use than a DataAdapter. DataSets and DataTables always have a huge overhead in terms of memory usage. Makes no difference if you are fetching a single row, but makes a huge difference for bigger result sets.
If you are fetching a fixed number of items, in MS SQL Server, output variables from a stored proc (or parameterized command) usually perform best.
if you use a reader you must have a open connection to your database generally a DataReader is used for fetch a combo or dataGrid, but if you want to stock your data in memory and you close our data base connexion you must use Datatable
Note : excuse my english level
If you just want read-only access to the data, then go with a raw DataReader; it's the fasted and most lightweight data access method.
However, if you intend to alter the data and save back to the database, then I would recommend using a DataAdapter and a DataSet (even a typed DataSet) because the DataSet class takes care of tracking changes, additions and deletions to the set which makes saves much easier. Additionally, if you have multiple tables in the dataset, you can model the referential constraints between them in the dataset.

what is the difference between data adapter and data reader?

What is the difference between data adapter and data reader?
Please see DataReader, DataAdapter & DataSet - When to use? :
ADO.NET provides two central Data
Access Components. The excellent thing
is that, they are common across all
Databases, be it SQL Server or other
competitive databases. Its only the
namespace to be used, that differs,
while using a Database other than SQL
Server.
A DataReader is an object returned from the ExecuteReader method of a DbCommand object. It is a forward-only cursor over the rows in the each result set. Using a DataReader, you can access each column of the result set, read all rows of the set, and advance to the next result set if there are more than one.
A DataAdapter is an object that contains four DbCommand objects: one each for SELECT, INSERT, DELETE and UPDATE commands. It mediates between these commands and a DataSet though the Fill and Update methods.
Data Reader is an object used in connected Environment.
Data Adapter is an object used in Disconnected environment using Dataset.
DataReader is a faster way to retrieve the records from the DB. DataReader reads the column. DataReader demands live connection but DataAdapter needs disconnected approach.
Here is a nice article on the above topic:
Difference Between DataReader, DataSet, DataAdapter and DataTable in C#
Key differences in simple terms:
Unlike classic ADO, which was primarily designed for tightly coupled client/server systems,ADO.NET was built with the disconnected world in mind, using DataSets/DataAdapter.
DataAdapter follows connectionless-oriented architecture which simply means you need not necessarily be connected to a data-source whereas DataReader is a connection-oriented architecture which means it needs an active connection to a data-source for it to operate.
DataAdapter is an intermediate layer/ middleware which acts a bridge between the DataSet and a Database whereas DataReader provides forward-only, read-only access to data using a server-side cursor (simply put it is ued to read the data).
Using DataSet we can manipulate and update a DataSet's contents while disconnected from the Datasource and send any modified data back for processing using a related DataAdapter whereas DataReader can only read data from a Database & cannot modify it.
DataAdapter object is used to read the data from database and populates that data to DataSet whereas DataReader simply reads the data using the Read() method.
DataAdapter is comparatively slower whereas using DataReader can increase application performance both by retrieving data as soon as it is available, and (by default) storing only one row at a time in memory, reducing system overhead.
Data reader is an object through which you can read a sequential stream of data. it's a forward only data wherein you cannot go back to read previous data.
data set and data adapter object help us to work in disconnected mode. data set is an in cache memory representation of tables. the data is filled from the data source to the data set thro' the data adapter. once the table in the dataset is modified, the changes are broadcast to the database back thro; the data adapter.
DataAdapter
DataAdapter will acts as a Bridge between DataSet and database. This dataadapter object is used to read the data from database and bind that data to dataset. Dataadapter is a disconnected oriented architecture.
DataReader
DataReader is used to read the data from database and it is a read and forward only connection oriented architecture during fetch the data from database. DataReader will fetch the data very fast when compared with dataset. Generally we will use ExecuteReader object to bind data to datareader

Categories