現(xiàn)在已經(jīng)從數(shù)據(jù)庫中把數(shù)據(jù)取出來然后放在datatable中 代碼如下:
- C# code
System.Data.DataTable dt = new System.Data.DataTable();
string sql = "select [ID],[GroupName],[State],[Point],[Time] from OnOff ";
SqlConnection connection = null;
DataSet dsData = null;
try
{
connection = new SqlConnection(connnectionString);
connection.Open();
SqlCommand command = new SqlCommand(sql, connection);
SqlDataAdapter daAdapter = new SqlDataAdapter();
daAdapter.SelectCommand = command;
dsData = new DataSet();
daAdapter.Fill(dsData);
connection.Close();
}
catch (Exception ee)
{
MessageBox.Show("數(shù)據(jù)庫連接失敗 !" + ee.Message.ToString());
return;
}
dt = dsData.Tables[0];
我現(xiàn)在想遍歷這個DataTable,可以一行一行取出來,,應該怎么寫,? 希望高手指點!,!最好能幫寫下代碼?。?br>感激不盡??!
C# code
System.Data.DataTable dt = new System.Data.DataTable();
string sql = "select [ID],[GroupName],[State],[Point],[Time] from OnOff ";
SqlConnection connection = null;
DataSet dsData = null;
try
{
connection = new SqlConnection(connnectionString);
connection.Open();
SqlCommand command = new SqlCommand(sql, connection);
SqlDataAdapter daAdapter = new SqlDataAdapter();
daAdapter.SelectCommand = command;
dsData = new DataSet();
daAdapter.Fill(dsData);
connection.Close();
}
catch (Exception ee)
{
MessageBox.Show("數(shù)據(jù)庫連接失敗 !" + ee.Message.ToString());
return;
}
dt = dsData.Tables[0];
if(dt!=null)
{
foreach(DataRow dr in dt.Rows)
{
訪問dr對象,然后就可以了,;
}
}
dt =
dsData.Tables[0];
=》
- C# code
dt = dsData.Tables[0];
for(int i=0;i<dt.Rows.Count;i++)
{
MessageBox.Show(string.Format("ID:{0},GroupName:{1},Time:{2}",dt.Rows["ID"],dt.Rows["GroupName"],dt.Rows["Time"]));
}
//dt is a datatable
for (int i = 0; i < dt.Rows.Count;
i++) { DataRow dr = dt.Rows;
for (int j = 0; j < dr.ItemArray.Length; j++)
Console.Write(dr[j]); Console.WriteLine();
}
for(int i=0;i { DataRow
dr =dt.NewRow(); dr[0]=ds1.Tables[0].Rows["字段"].ToString(); dr[1]=ds1.Tables[0].Rows["字段"].ToString(); dr[2]=ds1.Tables[0].Rows["字段"].ToString(); dt.Rows.Add(dr); }
|
|