Comparison of dataAdapter .Fill and .Update

后端 未结 2 808
余生分开走
余生分开走 2021-01-19 03:27

I\'ve been reading through the MSDN resources and several forums and still don\'t understand what\'s the difference between those two dataAdapter.Fill() and

2条回答
  •  醉话见心
    2021-01-19 04:08

    For short the definition.

    DataAdapter.Fill() stands for SELECT query statement to database from the Server.

    // 1
    // Open connection
    using (SqlConnection c = new SqlConnection(
            Properties.Settings.Default.DataConnectionString))
    {
       c.Open();
       // 2
       // Create new DataAdapter
       using (SqlDataAdapter a = new SqlDataAdapter("SELECT * FROM EmployeeIDs", c))
         {
          // 3
          // Use DataAdapter to fill DataTable
             DataTable t = new DataTable();
             a.Fill(t);
    
             // 4
             // Render data onto the screen
             // dataGridView1.DataSource = t; // <-- From your designer
        }
      }
    

    DataAdapter.Update() stands for Update, Insert and Delete query statement to database from the Server.

    public DataSet CreateCmdsAndUpdate(DataSet myDataSet,string myConnection,string mySelectQuery,string myTableName) 
    {
        OleDbConnection myConn = new OleDbConnection(myConnection);
        OleDbDataAdapter myDataAdapter = new OleDbDataAdapter();
        myDataAdapter.SelectCommand = new OleDbCommand(mySelectQuery, myConn);
        OleDbCommandBuilder custCB = new OleDbCommandBuilder(myDataAdapter);
    
        myConn.Open();
    
        DataSet custDS = new DataSet();
        myDataAdapter.Fill(custDS);
    
        //code to modify data in dataset here
    
        //Without the OleDbCommandBuilder this line would fail
        myDataAdapter.Update(custDS);
    
        myConn.Close();
    
        return custDS;
     }
    

    Reference:
    C# SqlDataAdapter
    DataAdapter.Update Method

提交回复
热议问题