我在窗口里创建并配置好了sqlConnection,sqlDataAdapter和DataSet组建当我在事件中执行如下代码时报错:
      sqlDataAdapter1.Update(workerDataSet1);报错:
未处理的“System.Data.SqlClient.SqlException”类型的异常出现在 system.data.dll 中。
其他信息: 系统错误。

解决方案 »

  1.   

    using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;
    using System.Data.SqlClient;namespace CMS
    {
    /// <summary>
    /// WorkerForm 的摘要说明。
    /// </summary>
    public class WorkerForm : System.Windows.Forms.Form
    {
    private System.Windows.Forms.Label label1;
    private System.Windows.Forms.DataGrid dataGrid1;
    private System.Windows.Forms.Button btnSave;
    private System.Windows.Forms.Button btnEdit;
    private System.Windows.Forms.Button btnCancel;
    private System.Windows.Forms.Button btnExit;
    private System.Data.SqlClient.SqlDataAdapter sqlDataAdapter1;
    private System.Data.SqlClient.SqlCommand sqlSelectCommand1;
    private System.Data.SqlClient.SqlCommand sqlInsertCommand1;
    private System.Data.SqlClient.SqlCommand sqlUpdateCommand1;
    private System.Data.SqlClient.SqlCommand sqlDeleteCommand1;
    private System.Data.SqlClient.SqlConnection sqlConnection1;
    private CMS.WorkerDataSet workerDataSet1;
                      /// <summary>
    /// 必需的设计器变量。
    /// </summary>
    private System.ComponentModel.Container components = null; public WorkerForm()
    {
    //
    // Windows 窗体设计器支持所必需的
    //
    InitializeComponent(); //
    // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
    //
    }
                      /// <summary>
    /// 清理所有正在使用的资源。
    /// </summary>
    protected override void Dispose( bool disposing )
    {
    if( disposing )
    {
    if(components != null)
    {
    components.Dispose();
    }
    }
    base.Dispose( disposing );
    }
                      private void btnSave_Click(object sender, System.EventArgs e)
    {
    sqlDataAdapter1.Update(workerDataSet1);
    /*    执行到这里的时候弹出对话框,报错的内容为sqlDataAdapter1.Update(workerDataSet1);报错:
    未处理的“System.Data.SqlClient.SqlException”类型的异常出现在 system.data.dll 中。
    其他信息: 系统错误。
    */ MessageBox.Show("The Woker table is updated.");
    } private void btnCancel_Click(object sender, System.EventArgs e)
    {
    workerDataSet1.Clear();
    sqlDataAdapter1.Fill(workerDataSet1); //出现上面同样的问题
    } private void btnExit_Click(object sender, System.EventArgs e)
    {
    this.Close();
    }
    }
    }
      

  2.   

    sqlDataAdapter1 默认有四个Command子对象,分别是选择,插入,删除,更新,默认填充DataSet只需一个SelectCommand就OK,当你要王数据库里面更新数据的时候,就需要赋值哦。不过有一个最简单的方法,用CommandBulider去完成。
    相关操作参见MSDN,有例子的~