C# 在 windows 表单应用程序上从数据库生成新 ID
C# Generating new id from database on windows forms application
我必须在加载 windows 表单应用程序时自动生成新的 AccountID。
因此,例如,当用户在 "Account id" 的文本框中开始 windows 表单 "Add new Account" 时,我必须显示数据库中的最新值。如果我在文本框中的 windows 表单中的数据库中有两个帐户,则值将为三个。
如果我在数据库中至少有一个帐户,我的代码就可以正常工作,但是当我的数据库为空时,我会遇到异常。
这是我的代码:
public int GetLatestAccountID()
{
try
{
command.CommandText = "select Max(AccountID)as maxID from Account";
command.CommandType = CommandType.Text;
connection.Open();
OleDbDataReader reader= command.ExecuteReader();
if (reader.Read())
{
int valueID = Convert.ToInt32(reader["maxID"]);
return valueID + 1;
}
return 1;
}
catch (Exception ex)
{
throw ex;
}
finally
{
if (connection!= null)
{
connection.Close();
}
}
}
我还在 Whosebug 上找到了答案:
object aa = DBNull.Value;
int valueID = (aa as int?).GetValueOrDefault();
但是如果我的数据库是空的,这行代码就可以工作,但是当我在数据库中有一个帐户时,它将始终显示在我的 windows 表单中的帐户 ID 文本框值 1 中。我使用 Microsoft Access 2007 数据库。
感谢任何帮助。
public int GetLatestAccountID()
{
try
{
int accounts = 0;
command.CommandText = "select Max(AccountID)as maxID from Account";
command.CommandType = CommandType.Text;
connection.Open();
OleDbDataReader reader= command.ExecuteReader();
if (reader.Read())
{
accounts = Convert.ToInt32(reader["maxID"]) + 1;
}
return accounts;
}
catch (Exception ex)
{
throw ex;
}
finally
{
if (connection!= null)
{
connection.Close();
}
}
}
你可以使用 SELECT COUNT(column_name) FROM table_name;计算帐户数量而不是选择最大的帐户?
我猜你想要:
public int GetLatestAccountID(string connectionString)
{
using(var dbConn = new OleDbConnection(connectionString))
{
dbConn.Open();
string query = "select Max(AccountID) from Account";
using(var dbCommand = new OleDbCommand(query, dbConn))
{
var value = dbCommand.ExecuteScalar();
if ((value != null) && (value != DBNull.Value))
return Convert.ToInt32(value) + 1;
return 1;
}
}
}
您似乎只打开了一次数据库连接,并在整个程序期间一直保持打开状态。不要那样做;这会导致竞争条件和数据损坏。 .NET 实现了数据库连接池,因此您根本不会通过保持连接打开来提高性能。
您也没有告诉我们您使用 GetLatestAccountID
的目的。如果您尝试将其用作主键,您也会 运行 遇到竞争条件问题。如果你想要一个主键,你应该让数据库创建它,return 创建记录后的值。
您可以像下面这样进一步简化它,
Select isnull(max(accountID),0) as maxID from Account
我必须在加载 windows 表单应用程序时自动生成新的 AccountID。
因此,例如,当用户在 "Account id" 的文本框中开始 windows 表单 "Add new Account" 时,我必须显示数据库中的最新值。如果我在文本框中的 windows 表单中的数据库中有两个帐户,则值将为三个。
如果我在数据库中至少有一个帐户,我的代码就可以正常工作,但是当我的数据库为空时,我会遇到异常。
这是我的代码:
public int GetLatestAccountID()
{
try
{
command.CommandText = "select Max(AccountID)as maxID from Account";
command.CommandType = CommandType.Text;
connection.Open();
OleDbDataReader reader= command.ExecuteReader();
if (reader.Read())
{
int valueID = Convert.ToInt32(reader["maxID"]);
return valueID + 1;
}
return 1;
}
catch (Exception ex)
{
throw ex;
}
finally
{
if (connection!= null)
{
connection.Close();
}
}
}
我还在 Whosebug 上找到了答案:
object aa = DBNull.Value;
int valueID = (aa as int?).GetValueOrDefault();
但是如果我的数据库是空的,这行代码就可以工作,但是当我在数据库中有一个帐户时,它将始终显示在我的 windows 表单中的帐户 ID 文本框值 1 中。我使用 Microsoft Access 2007 数据库。
感谢任何帮助。
public int GetLatestAccountID()
{
try
{
int accounts = 0;
command.CommandText = "select Max(AccountID)as maxID from Account";
command.CommandType = CommandType.Text;
connection.Open();
OleDbDataReader reader= command.ExecuteReader();
if (reader.Read())
{
accounts = Convert.ToInt32(reader["maxID"]) + 1;
}
return accounts;
}
catch (Exception ex)
{
throw ex;
}
finally
{
if (connection!= null)
{
connection.Close();
}
}
}
你可以使用 SELECT COUNT(column_name) FROM table_name;计算帐户数量而不是选择最大的帐户?
我猜你想要:
public int GetLatestAccountID(string connectionString)
{
using(var dbConn = new OleDbConnection(connectionString))
{
dbConn.Open();
string query = "select Max(AccountID) from Account";
using(var dbCommand = new OleDbCommand(query, dbConn))
{
var value = dbCommand.ExecuteScalar();
if ((value != null) && (value != DBNull.Value))
return Convert.ToInt32(value) + 1;
return 1;
}
}
}
您似乎只打开了一次数据库连接,并在整个程序期间一直保持打开状态。不要那样做;这会导致竞争条件和数据损坏。 .NET 实现了数据库连接池,因此您根本不会通过保持连接打开来提高性能。
您也没有告诉我们您使用 GetLatestAccountID
的目的。如果您尝试将其用作主键,您也会 运行 遇到竞争条件问题。如果你想要一个主键,你应该让数据库创建它,return 创建记录后的值。
您可以像下面这样进一步简化它,
Select isnull(max(accountID),0) as maxID from Account