Bosses, I again come
SqlConnection con=new SqlConnection (" Data Source=. Initial Catalog=mydate; User ID=BC; Password=123 ");
Con. The Open ();
String SQL="select * from student";
SqlCommand CMD=new SqlCommand (SQL, con);
SqlDataAdapter Dr=new SqlDataAdapter (CMD);
The DataSet ds=new DataSet ();
Dr. Fill (ds);
The DataTable TB=ds. Tables [0];
for (int i=0; i
{
Console. WriteLine (ds) Tables [0]. Rows [I] [] "name");//to remind is to remind the line
}
Con. The Close ();
The Console. ReadLine ();
I can output to this code, but remind me at position 7 no line
CodePudding user response:
for (int i=0; i
CodePudding user response: reference 1/f, lazy lazy source reply: for (int I=0; i Why is this? CodePudding user response:
Ds. The ToString (). The Length is what the devil CodePudding user response:
Ds. The ToString () to get is "System. The Data. The DataSet" So you traverse length is the length of the string, when your data is less than or more than the length of time would be an error CodePudding user response:
Ds. The ToString (). Length to TB. Rows. The Count, you need to traverse the DataTable line inside CodePudding user response:
Have a problem is clearly cycle times ~ static void Main (string [] args) { The DataTable DataTable=new DataTable (); Using (SqlDataAdapter adapter=new SqlDataAdapter (" select * from student ", the connectionString)) { Adapter. The Fill (dataTable); } Foreach (DataRow DataRow in dataTable. Rows) { Console. WriteLine (dataRow/" name "); } The Console. ReadKey (true); } CodePudding user response:
refer to 6th floor HerryDong response: is obviously there is something wrong with the cycle times ~ static void Main (string [] args) { The DataTable DataTable=new DataTable (); Using (SqlDataAdapter adapter=new SqlDataAdapter (" select * from student ", the connectionString)) { Adapter. The Fill (dataTable); } Foreach (DataRow DataRow in dataTable. Rows) { Console. WriteLine (dataRow/" name "); } The Console. ReadKey (true); } if I added in the Console. WriteLine (dataRow/" name ", dataRow/" name "); Complains not from object into a string CodePudding user response:
for (int i=0; i { } It's because of the ds. The ToString (). Length, should use the dt. Rows. Length CodePudding user response:
for (int i=0; i { } It's because of the ds. The ToString (). Length, should use the dt. Rows. Length CodePudding user response:
Modify the code into the The DataTable TB=ds. Tables [0]; for (int i=0; i { Console. WriteLine (ds) Tables [0]. Rows [I] [] "name");//to remind is to remind the line } System. Data. DataTable TB=ds. The Tables [0]. for (int i=0; i { System. Data. The DataRow DataRow=TB. Rows [I]; Console. WriteLine (dataRow/" name ");//to remind is to remind the line } CodePudding user response:
System. Data. DataTable TB=ds. The Tables [0]. for (int i=0; I & lt; TB. Rows. Count; I++) { System. Data. The DataRow DataRow=TB. Rows [I]; Console. WriteLine (dataRow/" name ");//to remind is to remind the line } CodePudding user response:
Debugged ds. The ToString (). The Length is what things