Lm th? no ? x? l? nhi?u k?t qu? b?ng cch s? d?ng DataReader trong Visual C#.NET

D?ch tiu ? D?ch tiu ?
ID c?a bi: 311274 - Xem s?n ph?m m bi ny p d?ng vo.
Bung t?t c? | Thu g?n t?t c?

? Trang ny


Bi vi?t ny ch?a hm chung chung m b?n c th? s? d?ng ? x? l? nhi?u recordsets v cc tin nh?n khc ?c tr? v? t? lu tr? th? t?c ho?c th?c hi?n ?t SQL pht bi?u.

M t? k? thu?t

ActiveX Data Objects (ADO) c th? nh?n ?c 5 lo?i khc nhau c?a d? li?u t? my ch?:
  • RecordSet
  • S? h? s ?c thay ?i b?i m?t truy v?n hnh ?ng (v d? nh INSERT, UPDATE, DELETE, ho?c ch?n vo)
  • Thng tin th ho?c c?nh bo
  • Thng bao li
  • Gi tr? tr? l?i th? t?c ?c lu tr? v ?u ra tham s?
Khi b?n ?c cc k?t qu? c?a m?t lo?t cc l?nh SQL, b?n c th? s? d?ng cc NextResult phng php ? ?nh v? cc DataReader v k?t qu? ti?p theo trong resultset.

Yu c?u

Danh sch sau v?ch ra ?c ? ngh? ph?n c?ng, ph?n m?m, h? t?ng m?ng, v gi d?ch v? m b?n c?n:
  • Microsoft Windows 2000 Professional, Windows 2000 Server, Windows 2000 Advanced Server, ho?c my ch? Windows NT 4.0
  • Microsoft Visual Studio.NET
  • Microsoft SQL Server 7.0 ho?c cao hn
Bi vi?t ny gi? ?nh r?ng b?n ? quen thu?c v?i sau cc ch? ?:
  • Visual Studio.NET
  • ADO.Nguyn t?c c b?n NET v c php

T?o d? n v thm m?

M? m?u ny s? d?ng cc tc gi? b?ng SQL my ch? qun r?u m?u c s? d? li?u.
  1. Dn cc cu sau y vo phn tch truy v?n SQL cng c? ho?c ti?n ch ISQL:
        SELECT * FROM Authors
        SELECT * FROM Authors WHERE State = 'CA'
  2. B?t ?u Visual Studio.NET.
  3. T?o m?t d? n ?ng d?ng Windows m?i trong Visual C#.NET. Form1 s? ?c thm vo cc d? n theo m?c ?nh.
  4. ?m b?o r?ng d? n c?a b?n c ch?a m?t tham chi?u ?n cc System.Data khng gian tn, v thm m?t tham chi?u ?n khng gian tn ny n?u c khng.
  5. Ni m?t Ch? huy nt trn Form1. Thay ?i cc Tn ti s?n c?a cc nt ? btnTest, v thay ?i cc Vn b?n b?t ?ng s?n ? Test.
  6. S? d?ng cc b?ng cch s? d?ng tuyn b? v? s? Cac vn , System.Data.OleDb, v System.Data.SqlClient khng gian tn do b?n khng ph?i v?t qua v?ng lo?i t? khai trong nh?ng khng gian tn sau trong m? c?a b?n. Thm m? sau vo v? t?ng Khai bo ph?n c?a Form1:
    using System;
    using System.Data.OleDb;
    using System.Data.SqlClient;
  7. Thm m? sau y ? cc btnTest_Click s? ki?n:

    Chu y B?n ph?i thay ?i ID ng?i dng <user name="">Ti kho?n gi tr? sang trng m?c c quy?n thch h?p ? th?c hi?n cc ho?t ?ng trn c s? d? li?u.</user>
        String myConnString  = "User ID=<username>;password=<strong password>;Initial Catalog=pubs;Data Source=myServer";
        SqlConnection myConnection = new SqlConnection(myConnString);
        SqlCommand myCommand = new SqlCommand();
        SqlDataReader myReader ;
        myCommand.CommandType = CommandType.StoredProcedure;
        myCommand.Connection = myConnection;
        myCommand.CommandText = "MyProc";
        int RecordCount=0; 
    	myReader = myCommand.ExecuteReader();
    	while (myReader.Read())
     	    //Write logic to process data for the first result.
    	    RecordCount = RecordCount + 1;
    	MessageBox.Show("Total number of Authors: " + RecordCount.ToString());
    	RecordCount = 0;
    	while (myReader.Read())
    	    //Write logic to process data for the second result.
    	    RecordCount = RecordCount + 1;
    	MessageBox.Show("Authors from California: " + RecordCount.ToString());
        catch(Exception ex) 
  8. S?a ?i (chu?i) k?t n?imyConnString) ph h?p v?i mi tr?ng c?a b?n.
  9. Lu d? n c?a b?n. Trn cc G? l?i tr?nh n, nh?p vo B?t ?u ? ch?y d? n c?a b?n.
  10. Nh?p vo Test. Thng bo r?ng cc h?p thng i?p s? hi?n th? cc d? li?u l ?c lu tr? tr? v? th? t?c.


Cho thng tin thm v? cch g?i th? t?c ?c lu tr?, Click vo bi vi?t s? d?i y ? xem bi vi?t trong c s? ki?n th?c Microsoft:
306574 Lm th? no ? g?i SQL my ch? lu tr? th? t?c trong ASP.NET
? thm thng tin v? qu?n l? l?i, b?m vo s? bi vi?t d?i y ? xem cc bi vi?t trong cc C s? ki?n th?c Microsoft:
308650 Lm th? no ? c ?c n?m bn d?i nh cung c?p l?i b?ng cch s? d?ng ADO.NET trong Visual C#.NET
? thm thng tin v? cc thng s? v th? t?c ?c lu tr?, nh?n s? bi vi?t d?i y ? xem cc bi vi?t trong cc C s? ki?n th?c Microsoft:
308621 PRB: Cc thng s? ?u ra khng tr? l?i khi b?n ch?y m?t ADO.NET l?nh trong Visual C#.NET
? bi?t thm chi ti?t v? ADO.Cc ?i t?ng NET v c php, ch? ?n Microsoft sau.NET Framework Kit pht tri?n ph?n m?m (SDK) ti li?u:
Truy c?p vo d? li?u v?i ADO.NET
http://msdn2.Microsoft.com/en-US/Library/e80y5yhx (vs.71) .aspx

Thu?c tnh

ID c?a bi: 311274 - L?n xem xt sau cng: 27 Thang Tam 2011 - Xem xt l?i: 2.0
p d?ng
  • Microsoft ADO.NET 1.1
  • Microsoft Visual C# .NET 2002 Standard Edition
  • Microsoft Visual C# .NET 2003 Standard Edition
T? kha:
kbhowtomaster kbsqlclient kbsystemdata kbmt KB311274 KbMtvi
My d?ch
QUAN TRONG: Bi vi?t ny ?c d?ch b?ng ph?n m?m d?ch my c?a Microsoft ch? khng ph?i do con ng?i d?ch. Microsoft cung c?p cc bi vi?t do con ng?i d?ch v c? cc bi vi?t do my d?ch ? b?n c th? truy c?p vo t?t c? cc bi vi?t trong C s? Ki?n th?c c?a chng ti b?ng ngn ng? c?a b?n. Tuy nhin, bi vi?t do my d?ch khng ph?i lc no c?ng hon h?o. Lo?i bi vi?t ny c th? ch?a cc sai st v? t? v?ng, c php ho?c ng? php, gi?ng nh m?t ng?i n?c ngoi c th? m?c sai st khi ni ngn ng? c?a b?n. Microsoft khng ch?u trch nhi?m v? b?t k? s? thi?u chnh xc, sai st ho?c thi?t h?i no do vi?c d?ch sai n?i dung ho?c do ho?t ?ng s? d?ng c?a khch hng gy ra. Microsoft c?ng th?ng xuyn c?p nh?t ph?n m?m d?ch my ny.
Nh?p chu?t vo y ? xem b?n ti?ng Anh c?a bi vi?t ny:311274

Cung cp Phan hi


Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com