原谅我贴一大段代码.但是自己实在找不出原因了.希望有耐心的高手帮帮.出现的问题是   无法保存到数据库.设计界面上有datagridview1,raidobutton1,radiobutton2,bindingsource1,bindingnavigator1,
在bindingnavigator1上加了个保存BUTTON--------------------------------------------- class SqlClass
    {
        public SqlDataAdapter adaper = new SqlDataAdapter();
        public  DataTable table = new DataTable();
        public SqlCommandBuilder buider = new SqlCommandBuilder();
        public SqlConnection conn = new SqlConnection();
        public SqlClass()
        { }
        public SqlClass(string selectstring, string  connstring)
        {
            try
            {
                conn = new SqlConnection(connstring);
                adaper = new SqlDataAdapter(selectstring, conn);
                buider = new SqlCommandBuilder(adaper);
                adaper.DeleteCommand = buider.GetDeleteCommand();
                adaper.UpdateCommand = buider.GetUpdateCommand();
                adaper.InsertCommand = buider.GetUpdateCommand();
                adaper.Fill(table);            }
            catch (SqlException ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }    }
}
-------------------------------------------------------------------------
namespace WindowsFormsApplication4
{
    public partial class Form2 : Form
    {
        string table = "table1";
        
        SqlClass sqlclass1 = new SqlClass();
    
        public Form2()
        {
            InitializeComponent();
        }        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButton1.Checked == true)
            {
                table = "table1";
            }
            else
            {
                table = "table2";
            }
            rebuild();
            
        }
        private void rebuild()
        {
            string connstring = Properties.Settings.Default.testConnectionString;
            string selectstring1 = "select * from " + table;
            sqlclass1 = new SqlClass(selectstring1, connstring);            bindingSource1.DataSource = sqlclass1.table;
            dataGridView1.DataSource = bindingSource1;
            bindingNavigator1.BindingSource = bindingSource1;
            
        }        private void 保存SToolStripButton_Click(object sender, EventArgs e)
        {
            try
            {
                this.Validate();
                bindingSource1.EndEdit();
                sqlclass1.adaper.Update(sqlclass1.table);
                MessageBox.Show("成功");
            }
            catch
            {
                MessageBox.Show("保存错误");
            }
        }
    }
}
-----------------------------------------------------------------------------------------------------

解决方案 »

  1.   

    调式下,获得调试下的SQL语句,然后直接在SQL server 中执行,看看SQL语句对不,如果没有问题,那就是你执行sql语句有问题
      

  2.   

    adaper.DeleteCommand = buider.GetDeleteCommand(); 
    adaper.UpdateCommand = buider.GetUpdateCommand(); 
    adaper.InsertCommand = buider.GetUpdateCommand(); 
    你看一下这三个属性的只是什么?这样好像不行。
      

  3.   

    增删改一块执行?
    adaper.DeleteCommand = buider.GetDeleteCommand();
                    adaper.UpdateCommand = buider.GetUpdateCommand();
                    adaper.InsertCommand = buider.GetUpdateCommand(); 
    GetUpdateCommand方法代码在哪?
      

  4.   

    先看下你的Sql语句是否正确,然后断点调试下具体问题出在哪。