有关数据表和SQL数据库的帮助 [英] Help with datatable and SQL database
问题描述
我希望有人可以帮助我,我对这一切都是陌生的,所以我可能不是做我想做的最好的方法.任何帮助都会很棒!
我具有以下功能:
I hope someone can help me, I am very new to all of this so I this might not be the best way to do what I want to do. Any help would be great!
I have the following function:
public void fnDelete(int i)
{
con = new SqlConnection(conString);
//ask user if wanting to delete
DialogResult dr = MessageBox.Show("Are you sure you want to delete this contact ? ", "Confirm deleting", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (dr == DialogResult.Yes) //if
{
//user clicked the "Delete" button
DataTable tbl = new DataTable();
tbl = this.dSet.Tables[0];
tbl.Rows[i].Delete(); //delete the row
int j;
j = 20;
SqlCommand DeleteCommand = new SqlCommand("sp_deleteContact ''" + j + "''", con);
dAdapter.DeleteCommand = DeleteCommand;
this.dAdapter.Update(tbl); //update the table
dSet.AcceptChanges();
}
}
我有一个看起来像这样的数据表:
I have a datatable that looks like this:
contactID | firtsName
2 | Terence
4 | John
当我调用该函数时,我这样称呼它:
When I call the function I call it like this:
fnDelete(currManager.Position);
这样就可以从datatable
中删除该行,但是如何将contactID
的值转换为j
,以便可以从数据库中删除该行呢?
添加了以下内容:
So that deletes the row out of the datatable
, but how can I get the value of the contactID
into j
so I can delete from the database to?
Added this:
var j = tbl.Rows[i][0];
好的,这就是我要提出的,这是一种好方法吗?
Ok so this is what I have come up with, is this a good way of doing it?
public void fnDelete(int i)
{
con = new SqlConnection(conString);
//ask user if wanting to delete
DialogResult dr = MessageBox.Show("Are you sure you want to delete this contact ? ", "Confirm deleting", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (dr == DialogResult.Yes) //if
{
//user clicked the "Delete" button
DataTable tbl = new DataTable();
tbl = this.dSet.Tables[0];
<big>var j = tbl.Rows[i][0];</big>
SqlCommand DeleteCommand = new SqlCommand("sp_deleteContact ''" + j + "''", con);
dAdapter.DeleteCommand = DeleteCommand;
tbl.Rows[i].Delete(); //delete the row
this.dAdapter.Update(tbl); //update the table
dSet.AcceptChanges();
}
}
推荐答案
您看到您所说的内容了吗?
Do you see the bit where you say
this.dAdapter.Update(tbl); //update the table
看看 ^ ]
这篇关于有关数据表和SQL数据库的帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!