Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/266.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#:无法使用DataGridView和SqlCeConnection更新数据库上的数据_C#_Datagridview - Fatal编程技术网

C#:无法使用DataGridView和SqlCeConnection更新数据库上的数据

C#:无法使用DataGridView和SqlCeConnection更新数据库上的数据,c#,datagridview,C#,Datagridview,我什么都试过了。无法修复和找到周围的工作 错误:缺少一个参数。命令a.update(t)上的[参数序号=2] 技术:C#,Visual Studio 2008 public partial class Doctor : Form { SqlCeConnection con = new SqlCeConnection("Data Source=C:\\Users\\user\\Documents\\Visual Studio 2008\\Projects\\DBConnec

我什么都试过了。无法修复和找到周围的工作

错误:缺少一个参数。命令a.update(t)上的[参数序号=2]

技术:C#,Visual Studio 2008

    public partial class Doctor : Form
    {
    SqlCeConnection con = new SqlCeConnection("Data Source=C:\\Users\\user\\Documents\\Visual Studio 2008\\Projects\\DBConnectionCSharp\\DBConnectionCSharp\\DBTesting1.sdf");
    SqlCeCommand cmd;
    DataTable t = new DataTable();
    SqlCeDataAdapter a;
    DataSet ds;
    SqlCeCommandBuilder cam;


    public Doctor()
    {
        InitializeComponent();
        this.Visible = true;
        con.Open();
        cmd = con.CreateCommand();
        cam = new SqlCeCommandBuilder(a);
        cmd.CommandText = "update Doctor set Name=@p2 where ID=@p1";

    }

    private void Doctor_Load(object sender, EventArgs e)
    {
       using (a = new SqlCeDataAdapter("SELECT * FROM Doctor", con))
        {
            a.Fill(t);
            DoctorView.DataSource = t;

        }
        con.Close();
    }

    private void Save_Click(object sender, EventArgs e)
    {
        a.UpdateCommand = cmd;
        a.Update(t);
    }
}

我会尝试以这种方式更改您的代码,抱歉,现在无法测试

public partial class Doctor : Form
{

    public Doctor()
    {
       InitializeComponent();
       // Remove all the code used to initialize the global objects here
    }

    private void Doctor_Load(object sender, EventArgs e)
    {
       // Open the connection just when needed, 
       // Initialize the adapter and fill the grid
       using(SqlCeConnection con = new SqlCeConnection(.....))
       {
            DataTable t = new DataTable();
            SqlCeDataAdapter a = new SqlCeDataAdapter("SELECT * FROM Doctor", con);
            a.Fill(t);
            DoctorView.DataSource = t;
       }
    }

    private void Save_Click(object sender, EventArgs e)
    {
       using(SqlCeConnection con = new SqlCeConnection(.....))
       {

            // Prepare again the adapter with a valid select command
            SqlCeDataAdapter a = new SqlCeDataAdapter("SELECT * FROM Doctor", con);

            // Force the building of the internal command objects of the adapter
            SqlCeCommandBuilder cb = new SqlCeCommandBuilder(a);

            // Recover the datatable from the datasource of the grid            
            DataTable t = DoctorView.DataSource as DataTable;

            // Update the table with DataRows changed, deleted or inserted
            a.Update(t);
       }
    }
}

代码中有些地方不太正确。在您调用的表单的构造函数中
a.UpdateCommand=cmd但此时SqlCeDataAdapter a未初始化。在单击Save_之前,您应该会得到一个空引用异常,上面的代码就是空引用异常。此外,在Load表单中,围绕DataAdapter的初始化退出using语句将调用适配器上的Dispose,当您尝试在Save_单击中使用该变量时,仍然会出现错误。你确定这就是你的代码吗?实际上为了调试的目的,我添加了额外的代码。我可以在DataViewGrid上获取和显示数据,但不能更新修改后的数据。那么最好更新上面的代码,只留下产生错误的代码。表Doctor中有多少字段?只有2列是ID和Doctor\u Name。我是C#的新手。今天开始构建应用程序。错误1:我遇到以下错误“DataAdapter.SelectCommand.Connection属性需要初始化”;错误2:如果我添加以下代码
code
SqlCeCommandBuilder cmd;cmd=新的SqlCeCommandBuilder(a);SqlCeCommand cm=新的SqlCeCommand();cm.CommandText=“更新医生集名称=@p2,其中ID=@p1”;a、 UpdateCommand=cm
code
DataGrid中不显示任何内容初始化DataAdapter的代码似乎是正确的,因为连接是在构造函数中传递的,如下所述。现在在另一个数据库上测试,但它现在可以工作请澄清这个疑问:如果我们在Save_click中创建另一个SqlCeDataAdapter实例,为什么它可以工作?当我们在全局范围中声明它时,为什么不可以?问题是我们需要重新应用到适配器的连接。表单load close and dispose(按照良好实践的要求)中代码周围的using语句会关闭并释放连接,但是,当您使用Save_Click方法时,适配器中的全局范围内不再有有效连接。因此,您需要重新打开连接并将其重新应用于适配器。据我所知,除了在构造函数调用中,没有办法将连接分配给适配器。