Three ways of Sqldataread in C # traverse reading individual field values

Source: Internet
Author: User

-------------------------------------------------------------------------------------------------

The query parser for the database writes the following code:

Ccreate Table Studentname
(
ID int primary key identity (.) NOT NULL,
Name nvarchar (+) NOT NULL
)

INSERT into studentname values (' long ')

INSERT into studentname values (' long ')

INSERT into studentname values (' long ')

INSERT into studentname values (' long ')

INSERT into studentname values (' long ')

INSERT into studentname values (' Chao ')

INSERT into studentname values (' Chao ')

--sql Server 2012 passed.

---------------------------------------------------------------------------------------------------

Here's the first method:

Using System.Data;

Using System.Data.SqlClient;

String constr = "Server=localhost;uid=sa;pwd=longshicheng;database=student";

SqlConnection conn = new SqlConnection (CONSTR);

SqlCommand cmd = new SqlCommand ("SELECT * from Studentname", conn);

Conn. Open ();

SqlDataReader rdr = cmd. ExecuteReader ();
String makestr = "";
DataTable table = new DataTable ();
Table.  Load (RDR); The data sheet is loaded into the dataReader.
String result= "";
for (int i = 0; i < table. Rows.Count; i++)//Traverse row
{
for (int j = 0; j < table. Columns.count; J + +)//Traversal column
{
Result + = table. ROWS[I][J]. ToString (); Displays the numeric value of each cell row by column.
}
result + = Environment.NewLine; A row of traversal is completed to write a carriage return.
}
Txtlab. Text = result;


/***************************************************************/

Here's the second method:

String constr = "Server=localhost;uid=sa;pwd=longshicheng;database=student";

SqlConnection conn = new SqlConnection (CONSTR);

SqlCommand cmd = new SqlCommand ("SELECT * from Studentname", conn);

Conn. Open ();

SqlDataReader rdr = cmd. ExecuteReader ();
String makestr = "";
for (int k = 0; k < rdr. FieldCount; k++)//Get field name
{
Makestr + = rdr.  GetName (k) + "\ T"; Load Field Name
}
Makestr + = Environment.NewLine; Line break
Makestr = makestr + "---------------------------------------------" +environment.newline;
while (RDR. Read ())//row by line reading each record
{
for (int t = 0; t < RDR. FieldCount; t++)//column-wise read out
{
Makestr = Makestr + rdr[t]. ToString () + "\ T"; Accumulate each row, that is, the corresponding values are read out by field
}
Makestr = Makestr + Environment.NewLine; Another line
}
Txtinfo. Text = Makestr;
Rdr. Close (); Shut down
Conn. Close ();
}

The following is the third method:

String constr = "Server=localhost;uid=sa;pwd=longshicheng;database=student";

SqlConnection conn = new SqlConnection (CONSTR);

SqlCommand cmd = new SqlCommand ("SELECT * from Studentname", conn);

Conn. Open ();

SqlDataReader rdr = cmd. ExecuteReader ();
String makestr = "";

for (int k = 0; k < rdr. FieldCount; k++)//Get field name
{
Makestr + = rdr.  GetName (k) + "\ T"; Load Field Name
}
Makestr + = Environment.NewLine; Line break
Makestr = makestr + "---------------------------------------------" + Environment.NewLine;
while (RDR. Read ())//row by line reading each record
{
Makestr = Makestr + rdr. GetValue (0) + "\ T" + rdr. GetValue (1). ToString () + "\ T";
Makestr = Makestr + Environment.NewLine;
}
Txtother. Text = Makestr;
Rdr. Close (); Shut down
Conn. Close ();

The results of all the above methods are as follows:

ID Name
---------------------------------------------
1 long
2 LONG
3 Long
4 LONG
5 Long
6 Chao
7 Chao

Three ways of Sqldataread in C # traverse reading individual field values

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.