从类型化数据集中获取标识值
我的表单中有一个类型化的数据集。使用 BindingSource 遍历行、插入和更新记录。
一切都很好,但我需要插入记录标识值来为表中的 GenelatedCode
字段生成字符串。
获取此值后,我会将值发送到我的 CodeGen()
方法并生成字符串,并使用该值更新同一行的 CodeGen
字段。
我用的是Access数据库。我知道 Access 有 @@Identity
功能,但我该如何使用它呢?我不想使用 OleDbCommand 或类似的东西。
我怎样才能做到这一点?
string GenCode(int pCariId)
{
string myvalue;
int totalDigit = 7;
myvalue = "CR" + pCariId.ToString();
for (int digit = myvalue.Length; digit <= totalDigit - 1; digit++)
{
myvalue = myvalue.Insert(2, "0");
}
return myvalue;
}
private void dataNavigator_ButtonClick(object sender, NavigatorButtonClickEventArgs e)
{
switch (e.Button.ButtonType)
{
case NavigatorButtonType.EndEdit:
try
{
this.Validate();
if (KaydetFlags == 1)
{
this.bndCariKayit.EndEdit();
datagate_muhasebeDataSet.TB_CARI.Rows[datagate_muhasebeDataSet.Tables["TB_CARI"].Rows.Count - 1]["INS_USR"] = 0;
datagate_muhasebeDataSet.TB_CARI.Rows[datagate_muhasebeDataSet.Tables["TB_CARI"].Rows.Count - 1]["INS_TRH"] = DateTime.Now;
XtraMessageBox.Show("Yeni Cari Kaydı Tamamlandı.");
KaydetFlags = 0;
}
else
{
DataRowView currentRow = (DataRowView)bndCariKayit.Current;
currentRow.Row["UPD_USR"] = "0";
currentRow.Row["UPD_TRH"] = DateTime.Now;
XtraMessageBox.Show("Cari Kaydı Güncellendi.");
this.bndCariKayit.EndEdit();
}
this.tB_CARITableAdapter.Update(datagate_muhasebeDataSet.TB_CARI);
}
catch (System.Exception ex)
{
XtraMessageBox.Show("Kayıt İşlemi Başarısız. Lütfen Tekrar Deneyiniz.");
}
break;
I got a typed dataset in my form. Using BindingSource to walk in rows, inserting and updating records.
Everything is fine but I need inserted records identity value for generating a string for my GeneratedCode
field in my table.
After getting this value I'll send value to my CodeGen()
method and generate string, and update same row's CodeGen
field with this value.
I'm using Access database. I know there is that @@Identity
thing for Access, but how can I use it? I don't want to use OleDbCommand
or something like this.
How can I do that?
string GenCode(int pCariId)
{
string myvalue;
int totalDigit = 7;
myvalue = "CR" + pCariId.ToString();
for (int digit = myvalue.Length; digit <= totalDigit - 1; digit++)
{
myvalue = myvalue.Insert(2, "0");
}
return myvalue;
}
private void dataNavigator_ButtonClick(object sender, NavigatorButtonClickEventArgs e)
{
switch (e.Button.ButtonType)
{
case NavigatorButtonType.EndEdit:
try
{
this.Validate();
if (KaydetFlags == 1)
{
this.bndCariKayit.EndEdit();
datagate_muhasebeDataSet.TB_CARI.Rows[datagate_muhasebeDataSet.Tables["TB_CARI"].Rows.Count - 1]["INS_USR"] = 0;
datagate_muhasebeDataSet.TB_CARI.Rows[datagate_muhasebeDataSet.Tables["TB_CARI"].Rows.Count - 1]["INS_TRH"] = DateTime.Now;
XtraMessageBox.Show("Yeni Cari Kaydı Tamamlandı.");
KaydetFlags = 0;
}
else
{
DataRowView currentRow = (DataRowView)bndCariKayit.Current;
currentRow.Row["UPD_USR"] = "0";
currentRow.Row["UPD_TRH"] = DateTime.Now;
XtraMessageBox.Show("Cari Kaydı Güncellendi.");
this.bndCariKayit.EndEdit();
}
this.tB_CARITableAdapter.Update(datagate_muhasebeDataSet.TB_CARI);
}
catch (System.Exception ex)
{
XtraMessageBox.Show("Kayıt İşlemi Başarısız. Lütfen Tekrar Deneyiniz.");
}
break;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
根据 文档 您必须使用
TableAdapter
的RowUpdated
方法我实际使用的另一个选项是创建
Select
查询,它只是从您的id< 获取最大值/代码>列:
只需将查询执行模式设置为标量,您就可以在代码中将其引用为
int lastId =
TableAdapter.SelectLastAddedId()
According to documentation you have to use
RowUpdated
method of yourTableAdapter
Another option, which I actually use is to create
Select
query which simply gets maximum value from youid
column:Just set query execute mode to
scalar
and you can refer to it in your code asint lastId =
TableAdapter.SelectLastAddedId()