-------------------------------------------------------------------------------------------------
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