adapter 需要有自己的insert ,update,delete等语句。如果你的adapter 是用向导生成的就应该有,如果自己手动添加的,这些代码需要自己添加。

解决方案 »

  1.   

    哦。飞翔大哥,能加一下我的QQ不?帮我调调!我自学C#,好多不会!
      

  2.   

    to:运行,单击按钮:系统指到[adapter.Update (ds,"Score");]当前ds中已经有同名的表数据填充不应该放在添加数据的按钮事件中 可以放在例如Form_Load事件中to:其他信息: 当传递具有新行的 DataRow 集合时,更新要求有效的 InsertCommand。更新数据集时 如果没有使用CommandBuilder对象,则需要手动指定DataAdapter的Command
      

  3.   

    有MSDN吗?看看这里..ms-help://MS.MSDNQTR.v80.chs/MS.MSDN.v80/MS.VisualStudio.v80.chs/dv_fxmclictl/html/1660f69c-5711-45d2-abc1-e25bc6779124.htm
      

  4.   

    如果没有,我把例子给你贴出来,看看就明白了..下面的完整代码示例提供的按钮用于从数据库重新加载数据和向数据库提交更改。 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.");
            }
        }}
      

  5.   

    还有个问题就是有点不明白..你为什么还要点击Button才添加数据呢?直接在DataGrid控件中进行添加不行吗?这样做:
    Form_load时绑定数据到DataGrid,然后用户在DataGrid中进行添加数据,添加完后,用户点击一个"更新"按钮,数据更新到数据库,然后再重新绑定数据源...
      

  6.   

    各位大哥,能不能就我这个问题提出解决办法!是我没有在数据适配器里配置UPDATA语句的问题?我该怎么写这个UPDATA语句???
      

  7.   

    try..SqlDataAdapter adapter =new SqlDataAdapter (select,conn);
    SqlCommandBuilder builder=new SqlCommandBuilder(adapter);
    DataSet ds =new DataSet ();
    adapter.Fill (ds,"Score");//填充数据
    DataTable dt = ds.Tables ["Score"];
    DataRow dr = dt.NewRow();
    dr[0]="1006";dr[1]="小梁";
    dt.Rows .Add (dr);//添加新行
    dataGrid1.DataSource =dt.DefaultView ;
    adapter.Update (ds,"Score");
      

  8.   

    另外:用SqlCommandBuilder要求你数据库中的表必须有主键,如果你的Scroe表没有主键,请更改表结构,即设置一下主键..