显示ID和广告1 [英] show the ID and ad 1

查看:82
本文介绍了显示ID和广告1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序,主要形式是"AD NEW"按钮.

当我单击按钮时,将打开一个新表单,并在文本框中显示该ID + 1:

我使用max()方法.



I have an application, in the main form a button "AD NEW".

When I click the button, a new form is open, and on a textbox I want to show the that ID+1:

I use max() method.



private void GetNewId()
        {
            int newID = 0;
            Program.Connection.CommandText = "SELECT MAX(ContractId) AS ContractId FROM Contracts";

            DataTable Table = new DataTable();
            Program.Connection.FillDataTable(Table, true);
            
            newID = Convert.ToInt32(Table.Rows[0]["ContractId"]);
            newID++;
            txtContractNumar.Text = newID.ToString();
        }



我正在寻找更好的方法.我在互联网上搜索,发现方法max()不太好.

我正在使用Access,但如果可能的话,我正在寻找一种可在mysql,ms sql和access中工作的方法,如果不是可以访问的方法.



I''m looking for a better way. I search over the internet and I saw the method max() is not so good.

I''m using Access, but if possible I''m searching a method that work in mysql, ms sql, and access, if not a method for access.

推荐答案

您永远不会在生产代码中这样做.在多用户系统中,如果多个客户端同时运行此代码(并且它们将!),您的代码将在多个客户端上生成相同的ContractId.

您让数据库在自动递增列中分配标识值,然后使用@@ SCOPE_IDENTITY数据库检索该记录的ID值.
You do not ever do this in production code. In a multi-user system, your code WILL generate the same ContractId on multiple clients if they run this code at the same time (and they WILL!).

You let the database assign identity values in autoincrementing columns and use the databases @@SCOPE_IDENTITY equivilent to get back what that Id value is for that record.


我不知道一个命令/功能(尽管请参见下文)将适用于Access,MySql和SqlServer.

我的Sql使用LAST_INSERT_ID()@@IDENTITY(尽管有报告说它已损坏),Sql Server使用SCOPE_IDENTITY()IDENT_CURRENT(''table'')(视情况而定)或@@IDENTITY,尽管再次不推荐使用(并且可能再次使用)并根据本文 [@@IDENTITY

来自MSDN, [
因此,只要您可以自己确认@@ IDENTITY可以在要使用的所有系统上工作,就可以使用它.

否则类似:
I do not know of one command/function (although see below) that will work for Access, MySql and SqlServer.

My Sql uses LAST_INSERT_ID() or @@IDENTITY (although there are reports that this is broken), Sql Server uses SCOPE_IDENTITY() or IDENT_CURRENT(''table'') (depending on the circumstances) or @@IDENTITY although once again this is deprecated (and possibly broken) and according to this article[^] (scroll down to the ''Access'' heading) JET/OLEDB now also supports @@IDENTITY

This, from MSDN[^] (See the Remarks Section) should help you decide which to use for Sql Server.

So, provided you can confirm for yourself that @@IDENTITY works for all the systems you want to use, you might be able to use that.

Otherwise something like:
public enum DBSystem
{
  Access,
  MySql,
  SqlServer
}

public string GetNextIDString(DBSystem system)
{
  string nextIDString = "SELECT ";
  switch (system)
  {
    case DBSystem.Access:
      nextIDString += "@@IDENTITY .............";  // the ...... represents the rest of the syntax
      break;
    case DBSystem.MySql
      nextIDString += "LAST_INSERT_ID(Contracts) .............";  // the ...... represents the rest of the syntax
      break;
    case DBSystem.SqlServer:
      nextIDString += "@@IDENTITY .............";  // the ...... represents the rest of the syntax
      // OR
      nextIDString += "SCOPE_IDENTITY .............";  // the ...... represents the rest of the syntax
      nextIDString += "IDENT_CURRENT(Contracts) .............";  // the ...... represents the rest of the syntax

      break;
  }

  return nextIDString;
}



我希望其中的一些帮助.



I hope that some of this helps.


这篇关于显示ID和广告1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆