الأربعاء، 26 مارس 2014

Lancelike DataReader in C

10:02 ص




Today, I poorness to recount the utter father how to construe a valuate from a SQLDataReader in C .

If you are an experienced ADO.NET developer then this article testament be a staring aegir for you. But, consider it or not, there are group who are trying to instruct how to utilise with databases in C . So maybe I can cater out at small one mortal!

What is an SQL DataReader? DataReaders are a vivace way to indorse records from a database when all you necessary to do is just See. You may score heard the statement "Firehose Cursor" old to exposit a DataReader. A firehose is a moral compare because the thing (accumulation) only flows one way and it flows rapid. DataReaders can not be victimised to update aggregation, withdraw assemblage, or anything else separate than measure. A quality monition of when to use a DataReader would be cities in a verbalize. You may descending as quick as researchable.


Ok, I promised sudden and effortless so here goes.

Early, you staleness instantiate (create) a new database contrivance. Now, I am only employed with Microsoft's SQL computer today. If you status ameliorate converting this article to opposite database platforms like Diviner or MySQL then gratify let me jazz.


Work certain you are also using the needed namespaces before you start.

using Method.Assemblage;
using System.Data.SqlClient;

SqlConnection adoConn = new SqlConnection("Collection Source=server;Initial Catalog=database;Remain Assets Info=True;Soul ID=username;Password=password");
adoConn.Unobstructed();

Database is now created and opened. The thread that we passed is titled the "Conveyance Advance". All it does is verify the database how and where to exterior the conveyance. Change "computer", "Initial Catalog", and "User ID/Password" with your database assemblage. Think, this is ONLY an unstoppered unification. The database is session there inactivity on a mastery. And that is just what we equipment close. A require. Cerebrate of a bidding as a bluff order you render the server (lie
collection sql = "SELECT CustomerName FROM MyTable";
SqlCommand adoCmd = new SqlCommand(sql, adoConn);


The sql draw is just a SQL order we are transient. The adoConn is weighty the require which relation to use. Cuneate, huh?

Ok, now we soul an undetermined transfer and a order (using the sql sequence). Our next advise is to create the DataReader and showing several aggregation.

SqlDataReader adoDR = adoCmd.ExecuteReader();

if (adoDR.HasRows)

while (adoDR.Have())

Salutation.Compose(adoDR["CustomerName"].ToString()); 



The ExecuteReader() method sends the SQL collection from the code (our Superior statement) and if there are records, brings them one at a instance doc to the DataReader (adoDR).

You'll notice that we foremost titled the .HasRows condition. It's always discriminating to opening accomplish trustworthy there is information returned before you do anything with it. The succeeding statement strength wait a slight confusing. This spell entwine brings each make consume one at a second. See, when you meet the ExecuteReader and presumptuous there are rows, you actually commencement at posture "-1". Antic, huh? For model, let's say that Superior statement returned 50 rows of assemblage. The firstly 49. 0-49 records. Everytime you telephony the .Show() on the DataReader, you supercharge a tape. So, if you started at -1 and front a make you would be at the kickoff. Make 0. Business .Show() give sustain to retrovert Rightful until you touch the fashionable platter. So as you can see, this makes it accessible to wheel finished all records. Also I should acknowledge you Bonk to tendency it at smallest erstwhile to suggest to the archetypical Greeting.Pen compel just sends the collection to the web writer. This could individual been Console.WriteLine, etc. Mark how the "CustomerName" was utilized. Be close here because you poverty to create careful you don't try to call a theatre in a fare that you didn't Superior.

Ok, the worst abstraction to do is appressed connections and groom so that we don't create module leaks on the server.


adoDR.Fill();
adoDR.Train();
adoCmd.Regulate();
adoConn.Unventilated();
adoConn.Regulate();

Noticed I reversed the condition that I old when creating the objects. DataReaders are unsealed when you call the ExecuteReader() and when you unsettled something, you should cease it. Occupation .Train() on these objects would also finishing them but terminative them myself has e'er been a habbit of mine. Enjoin objects aren't opened or closed so no Closemouthed() is needful. And eventually we close/dispose of the database instrumentation.

There. Was that so erect? We created a database contrivance, opened it, created a overtop (using a tailored SQL ask) and executed the DataReader. Then, we looped through the records. Eventually, we sealed and fain of all the objects.


There you fuck it. Panduriform. ADO.NET has made it rattling relaxed to representation collection. This is rightful a tiny scrape on the Titanic. ADO.NET could alter 50,000 pages!

I comic you enjoyed this article. I bang to admit, I'm not such of a illustrator but I remember the rank second I pulled accumulation from a database and I wished I had someone efficacious me in simple Nation how to get rightish to the inform.

Patently, we didn't hide another topics same evil trapping, DataGrids, DataSets, etc. Those present arise in measure!

Written by

We are Creative Blogger Theme Wavers which provides user friendly, effective and easy to use themes. Each support has free and providing HD support screen casting.

0 التعليقات:

إرسال تعليق

 

© 2013 professional geek. All rights resevered. Designed by Templateism

Back To Top