How do I extract data from a DataTable?

前端 未结 7 1573
失恋的感觉
失恋的感觉 2020-12-02 07:08

I have a DataTable that is filled in from an SQL query to a local database, but I don\'t know how to extract data from it. Main method (in test program):

<
相关标签:
7条回答
  • 2020-12-02 07:36

    You can set the datatable as a datasource to many elements.

    For eg

    gridView

    repeater

    datalist

    etc etc

    If you need to extract data from each row then you can use

    table.rows[rowindex][columnindex]
    

    or

    if you know the column name

    table.rows[rowindex][columnname]
    

    If you need to iterate the table then you can either use a for loop or a foreach loop like

    for ( int i = 0; i < table.rows.length; i ++ )
    {
        string name = table.rows[i]["columnname"].ToString();
    }
    
    foreach ( DataRow dr in table.Rows )
    {
        string name = dr["columnname"].ToString();
    }
    
    0 讨论(0)
  • 2020-12-02 07:38
      var table = Tables[0]; //get first table from Dataset
      foreach (DataRow row in table.Rows)
         {
           foreach (var item in row.ItemArray)
             {
                console.Write("Value:"+item);
             }
         }
    
    0 讨论(0)
  • 2020-12-02 07:51

    Unless you have a specific reason to do raw ado.net I would have a look at using an ORM (object relational mapper) like nHibernate or LINQ to SQL. That way you can query the database and retrieve objects to work with which are strongly typed and easier to work with IMHO.

    0 讨论(0)
  • 2020-12-02 07:52

    Please consider using some code like this:

    SqlDataReader reader = command.ExecuteReader();
    int numRows = 0;
    DataTable dt = new DataTable();
    
    dt.Load(reader);
    numRows = dt.Rows.Count;
    
    string attended_type = "";
    
    for (int index = 0; index < numRows; index++)
    {
        attended_type = dt.Rows[indice2]["columnname"].ToString();
    }
    
    reader.Close();
    
    0 讨论(0)
  • 2020-12-02 07:53

    The DataTable has a collection .Rows of DataRow elements.

    Each DataRow corresponds to one row in your database, and contains a collection of columns.

    In order to access a single value, do something like this:

     foreach(DataRow row in YourDataTable.Rows)
     { 
         string name = row["name"].ToString();
         string description = row["description"].ToString();
         string icoFileName = row["iconFile"].ToString();
         string installScript = row["installScript"].ToString();
     }
    
    0 讨论(0)
  • 2020-12-02 07:58

    Please, note that Open and Close the connection is not necessary when using DataAdapter.

    So I suggest please update this code and remove the open and close of the connection:

            SqlDataAdapter adapt = new SqlDataAdapter(cmd);
    

    conn.Open(); // this line of code is uncessessary

            Console.WriteLine("connection opened successfuly");
            adapt.Fill(table);
    

    conn.Close(); // this line of code is uncessessary

            Console.WriteLine("connection closed successfuly");
    

    Reference Documentation

    The code shown in this example does not explicitly open and close the Connection. The Fill method implicitly opens the Connection that the DataAdapter is using if it finds that the connection is not already open. If Fill opened the connection, it also closes the connection when Fill is finished. This can simplify your code when you deal with a single operation such as a Fill or an Update. However, if you are performing multiple operations that require an open connection, you can improve the performance of your application by explicitly calling the Open method of the Connection, performing the operations against the data source, and then calling the Close method of the Connection. You should try to keep connections to the data source open as briefly as possible to free resources for use by other client applications.

    0 讨论(0)
提交回复
热议问题