简体   繁体   English

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

[英]C# : Not Able to update data on database using DataGridView and SqlCeConnection

I tried everything . 我尝试了一切。 Not able to fix and find the work around. 无法修复并找到解决方法。

Error: A parameter is missing. 错误:缺少参数。 [ Parameter ordinal = 2 ] on command a.update(t); [a.update(t)命令上的[参数序数= 2];

Technology : C#,Visual Studio 2008 技术: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);
    }
}

I would try to change your code in this way, sorry but cannot test now... 我会尝试以这种方式更改您的代码,抱歉,但是现在无法测试...

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);
       }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用C#将datagridview更新到数据库? - how to update datagridview to database using c#? 使用 C# 使用 DataGridView 中的值更新数据库 - Update database with values in DataGridView using C# 无法在 Windows 窗体应用程序 C# 中更新 datagridview 数据 - Not able to update datagridview data in windows form application C# c# - 如何为datagridview实现更新按钮并使用dataHandler类(数据访问类)将值保存到数据库 - how to implement update button for datagridview and save values to database using a dataHandler class(data access class) c# C#SqlCeConnection不更新.sdf文件,该文件中没有数据 - C# SqlCeConnection not updating .sdf file, no data in the file 如何在C#中使用datagridview更新Access数据库 - How to update Access database using a datagridview in C# 使用C#将新添加的datagridview行更新到oledb数据库 - Update newly added rows of datagridview to oledb database using c# 如何使用C#将数据从Datagridview更新到Excel - How to update data from datagridview into excel using c# C#-2个问题-在DataGridView_Row Leicester中引用单元格会导致nullreference错误; 无法使用DataGridView数据更新数据库 - C# - 2 problems - referencing a cell in DataGridView_RowsAdded causes nullreference error; Cant update database with DataGridView data C#DataGridView在数据库中不更新 - C# DataGridView doesn't update in Database
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM