使用 C# 检查 Access 数据库中是否存在记录
Check for the existence of a record in Access database using C#
首先,我确实先搜索了这个,发现这个问题得到了回答:Previous Answer
我对这个答案的问题是使用这个方法会导致 NullReferenceException
。显然代码仍然可以使用 try/catch 块,但我一直明白故意使用异常作为控制流程的手段是糟糕的设计。有更好的方法吗?
为了明确参数,我正在使用 OleDbConnection 和 OleDbCommand read/write Access 2010 数据库(.accdb 格式)。
下面是我目前使用上述答案方法的代码:
public bool ReadAccessDb(string filePath, string queryString, bool hasRecords)
{
string connectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;" +
@"Data Source=" + filePath + ";" +
@"User Id=;Password=;";
using (OleDbConnection connection = new OleDbConnection(connectionString))
using (OleDbCommand command = new OleDbCommand(queryString, connection))
{
try
{
connection.Open();
int count = (int)command.ExecuteScalar();
//This works, but if no records exist it uses the Exception system to halt further action. Look for better approach.
if(count > 0)
{
hasRecords = true;
}
}
catch (System.Exception ex)
{
}
}
return hasRecords;
}
您可以使用:
int count = command.ExecuteScalar() is DBNull ? 0 : Convert.ToInt32(command.ExecuteScalar());
或使用:
object obj = command.ExecuteScalar();
int count = obj is DBNull ? 0 : Convert.ToInt32(obj);
我发布这个答案是因为这个问题有点模棱两可,并且(当前)接受的答案可以想象是 "fooled" 如果存在一行但第一列 return 由 SELECT语句恰好包含一个NULL。考虑测试 table
CREATE TABLE MyTable (ID COUNTER PRIMARY KEY, NullableColumn INTEGER NULL)
INSERT INTO MyTable (NullableColumn) VALUES (NULL)
看起来像
ID NullableColumn
-- --------------
1 <NULL>
如果用于测试的 SELECT 语句是
string sql = "SELECT NullableColumn FROM MyTable WHERE ID=1";
然后方法
static bool RecordExists(string queryString, OleDbConnection conn)
{
bool rtn;
using (var command = new OleDbCommand(queryString, conn))
{
object obj = command.ExecuteScalar();
int count = obj is DBNull ? 0 : Convert.ToInt32(obj);
rtn = (count != 0);
}
return rtn;
}
会returnFalse
,而这个方法
static bool RecordExists(string queryString, OleDbConnection conn)
{
bool rtn;
string rowCountString = String.Format("SELECT COUNT(*) AS n FROM ({0})", queryString);
using (var command = new OleDbCommand(rowCountString, conn))
{
int count = (int)command.ExecuteScalar();
rtn = (count != 0);
}
return rtn;
}
会 return True
.
首先,我确实先搜索了这个,发现这个问题得到了回答:Previous Answer
我对这个答案的问题是使用这个方法会导致 NullReferenceException
。显然代码仍然可以使用 try/catch 块,但我一直明白故意使用异常作为控制流程的手段是糟糕的设计。有更好的方法吗?
为了明确参数,我正在使用 OleDbConnection 和 OleDbCommand read/write Access 2010 数据库(.accdb 格式)。
下面是我目前使用上述答案方法的代码:
public bool ReadAccessDb(string filePath, string queryString, bool hasRecords)
{
string connectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;" +
@"Data Source=" + filePath + ";" +
@"User Id=;Password=;";
using (OleDbConnection connection = new OleDbConnection(connectionString))
using (OleDbCommand command = new OleDbCommand(queryString, connection))
{
try
{
connection.Open();
int count = (int)command.ExecuteScalar();
//This works, but if no records exist it uses the Exception system to halt further action. Look for better approach.
if(count > 0)
{
hasRecords = true;
}
}
catch (System.Exception ex)
{
}
}
return hasRecords;
}
您可以使用:
int count = command.ExecuteScalar() is DBNull ? 0 : Convert.ToInt32(command.ExecuteScalar());
或使用:
object obj = command.ExecuteScalar();
int count = obj is DBNull ? 0 : Convert.ToInt32(obj);
我发布这个答案是因为这个问题有点模棱两可,并且(当前)接受的答案可以想象是 "fooled" 如果存在一行但第一列 return 由 SELECT语句恰好包含一个NULL。考虑测试 table
CREATE TABLE MyTable (ID COUNTER PRIMARY KEY, NullableColumn INTEGER NULL)
INSERT INTO MyTable (NullableColumn) VALUES (NULL)
看起来像
ID NullableColumn
-- --------------
1 <NULL>
如果用于测试的 SELECT 语句是
string sql = "SELECT NullableColumn FROM MyTable WHERE ID=1";
然后方法
static bool RecordExists(string queryString, OleDbConnection conn)
{
bool rtn;
using (var command = new OleDbCommand(queryString, conn))
{
object obj = command.ExecuteScalar();
int count = obj is DBNull ? 0 : Convert.ToInt32(obj);
rtn = (count != 0);
}
return rtn;
}
会returnFalse
,而这个方法
static bool RecordExists(string queryString, OleDbConnection conn)
{
bool rtn;
string rowCountString = String.Format("SELECT COUNT(*) AS n FROM ({0})", queryString);
using (var command = new OleDbCommand(rowCountString, conn))
{
int count = (int)command.ExecuteScalar();
rtn = (count != 0);
}
return rtn;
}
会 return True
.