从类型化数据集中获取标识值

发布于 2024-12-11 10:32:56 字数 2179 浏览 0 评论 0原文

我的表单中有一个类型化的数据集。使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

秋日私语 2024-12-18 10:32:56

根据 文档 您必须使用TableAdapterRowUpdated 方法

private static void OnRowUpdated(
  object sender, OleDbRowUpdatedEventArgs e)
{
    // Conditionally execute this code block on inserts only.
    if (e.StatementType == StatementType.Insert)
    {
        OleDbCommand cmdNewID = new OleDbCommand("SELECT @@IDENTITY",
            connection);
        // Retrieve the Autonumber and store it in the CategoryID column.
        e.Row["CategoryID"] = (int)cmdNewID.ExecuteScalar();
        e.Status = UpdateStatus.SkipCurrentRow;
    }
}

我实际使用的另一个选项是创建 Select 查询,它只是从您的 id< 获取最大值/代码>列:

SelectLastAddedId:
SELECT MAX(id) FROM obmiar

只需将查询执行模式设置为标量,您就可以在代码中将其引用为
int lastId = TableAdapter.SelectLastAddedId()

According to documentation you have to use RowUpdated method of your TableAdapter

private static void OnRowUpdated(
  object sender, OleDbRowUpdatedEventArgs e)
{
    // Conditionally execute this code block on inserts only.
    if (e.StatementType == StatementType.Insert)
    {
        OleDbCommand cmdNewID = new OleDbCommand("SELECT @@IDENTITY",
            connection);
        // Retrieve the Autonumber and store it in the CategoryID column.
        e.Row["CategoryID"] = (int)cmdNewID.ExecuteScalar();
        e.Status = UpdateStatus.SkipCurrentRow;
    }
}

Another option, which I actually use is to create Select query which simply gets maximum value from you id column:

SelectLastAddedId:
SELECT MAX(id) FROM obmiar

Just set query execute mode to scalar and you can refer to it in your code as
int lastId = TableAdapter.SelectLastAddedId()

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文