[英]C# : Not Able to update data on database using DataGridView and SqlCeConnection
我嘗試了一切。 無法修復並找到解決方法。
錯誤:缺少參數。 [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);
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.