dateGridView将数据从数据库中掉入内存,比如
OracleConnection conn = new OracleConnection(Properties.Settings.Default.ConnectionString);
OracleDataAdapter adapter = new OracleDataAdapter("select * from test", conn);
DataTable dt = new DataTable();
this.bindingSource1.DataSource = dt;
adapter.Fill(dt);请问此时如何对表进行update操作(update完后页面要刷新),然后再将update的数据回写到数据库怎么做

解决方案 »

  1.   

    参考MSDN:ms-help://MS.MSDNQTR.v80.chs/MS.MSDN.v80/MS.VisualStudio.v80.chs/dv_fxmclictl/html/1660f69c-5711-45d2-abc1-e25bc6779124.htm
      

  2.   

    如果你没有装MSDN,给你抄个例子:using System;
    using System.Data;
    using System.Data.SqlClient;
    using System.Windows.Forms;public class Form1 : System.Windows.Forms.Form
    {
        private DataGridView dataGridView1 = new DataGridView();
        private BindingSource bindingSource1 = new BindingSource();
        private SqlDataAdapter dataAdapter = new SqlDataAdapter();
        private Button reloadButton = new Button();
        private Button submitButton = new Button();    [STAThreadAttribute()]
        public static void Main()
        {
            Application.Run(new Form1());
        }    // Initialize the form.
        public Form1()
        {
            dataGridView1.Dock = DockStyle.Fill;        reloadButton.Text = "reload";
            submitButton.Text = "submit";
            reloadButton.Click += new System.EventHandler(reloadButton_Click);
            submitButton.Click += new System.EventHandler(submitButton_Click);        FlowLayoutPanel panel = new FlowLayoutPanel();
            panel.Dock = DockStyle.Top;
            panel.AutoSize = true;
            panel.Controls.AddRange(new Control[] { reloadButton, submitButton });        this.Controls.AddRange(new Control[] { dataGridView1, panel });
            this.Load += new System.EventHandler(Form1_Load);
            this.Text = "DataGridView databinding and updating demo";
        }    private void Form1_Load(object sender, System.EventArgs e)
        {
            // Bind the DataGridView to the BindingSource
            // and load the data from the database.
            dataGridView1.DataSource = bindingSource1;
            GetData("select * from Customers");
        }    private void reloadButton_Click(object sender, System.EventArgs e)
        {
            // Reload the data from the database.
            GetData(dataAdapter.SelectCommand.CommandText);
        }    private void submitButton_Click(object sender, System.EventArgs e)
        {
            // Update the database with the user's changes.
            dataAdapter.Update((DataTable)bindingSource1.DataSource);
        }    private void GetData(string selectCommand)
        {
            try
            {
                // Specify a connection string. Replace the given value with a 
                // valid connection string for a Northwind SQL Server sample
                // database accessible to your system.
                String connectionString =
                    "Integrated Security=SSPI;Persist Security Info=False;" +
                    "Initial Catalog=Northwind;Data Source=localhost";            // Create a new data adapter based on the specified query.
                dataAdapter = new SqlDataAdapter(selectCommand, connectionString);            // Create a command builder to generate SQL update, insert, and
                // delete commands based on selectCommand. These are used to
                // update the database.
                SqlCommandBuilder commandBuilder = new SqlCommandBuilder(dataAdapter);            // Populate a new data table and bind it to the BindingSource.
                DataTable table = new DataTable();
                table.Locale = System.Globalization.CultureInfo.InvariantCulture;
                dataAdapter.Fill(table);
                bindingSource1.DataSource = table;            // Resize the DataGridView columns to fit the newly loaded content.
                dataGridView1.AutoResizeColumns( 
                    DataGridViewAutoSizeColumnsMode.AllCellsExceptHeader);
            }
            catch (SqlException)
            {
                MessageBox.Show("To run this example, replace the value of the " +
                    "connectionString variable with a connection string that is " +
                    "valid for your system.");
            }
        }}
      

  3.   

    如果此时(Fill完)update ,更改的数据库的数据 还是内存的数据啊
      

  4.   

    TO:
    如果此时(Fill完)update ,更改的数据库的数据 还是内存的数据啊数据库中的数据..
      

  5.   

    核心就是SqlDataAdapter+SqlCommandBuilder+BindingSource...写了个简单的例子,测试通过:两个按钮,一个"加载",一个"更新",还有一个"DataGridView"using System.Data;
    using System.Data.SqlClient;namespace DataGridViewUpdateToDataBase
    {
        public partial class Form1 : Form
        {
            private SqlDataAdapter sda = new SqlDataAdapter();
            private BindingSource bs = new BindingSource();
            public Form1()
            {
                InitializeComponent();
            }        //加载
            private void btnLoad_Click(object sender, EventArgs e)
            {
                DataBind();
            }        //数据绑定
            private void DataBind()
            {
                SqlConnection con = new SqlConnection("server=.;database=student;uid=sa;pwd=0421");
                sda = new SqlDataAdapter("select * from studentDetails", con);
                SqlCommandBuilder buider = new SqlCommandBuilder(sda);
                DataSet ds = new DataSet();
                sda.Fill(ds, "student");
                bs .DataSource =ds.Tables ["student"];
                this.dataGridView1.DataSource = bs;
            }        //更新
            private void btnUpdate_Click(object sender, EventArgs e)
            {
                sda.Update((DataTable)bs.DataSource);
            }
        }
    }
      

  6.   

    注意,用SqlCommandBuider要求数据库中的表有主键...如果没有主键,请注意更改表结构,即设置主键..
      

  7.   

    还是没明白我的意思哦,我是先把数据调到内存里,然后update,delete,insert等操作是对内存的数据修改,然后在更新到数据库.还有update只能对单表操作吧,多表咋办
      

  8.   

    你先select,再加个update的例子啊
      

  9.   

    没太明白,你是说把数据从数据库中取出来,然后放在比如一个DataTable中,然后再对DataTable进行update,insert,delete ,然后再保存到数据库中,是这样吗?
      

  10.   

    对  是这个样子  最好是定义个dataset 因为有多个表格
      

  11.   

    我就不写例子了,MSDN上有相关例子,你先看下吧,搞不定再说..public DataSet CreateCmdsAndUpdate(DataSet dataSet, string connectionString,
        string queryString, string tableName) 
    {
        using (OleDbConnection connection = new OleDbConnection(connectionString))
        {
            OleDbDataAdapter adapter = new OleDbDataAdapter();
            adapter.SelectCommand = new OleDbCommand(queryString, connection);
            OleDbCommandBuilder builder = new OleDbCommandBuilder(adapter);        connection.Open();        DataSet customers = new DataSet();
            adapter.Fill(customers);        //code to modify data in dataset here
            //注意这里你对DataTable中的值进行update,insert,delete ..        adapter.Update(customers, tableName);        return customers;
        }
    }以上是OleDb的,你换成相应SqlDataAdapter,SqlCommandBuilder等一样用...