SQLite测试是否存在记录
我正在努力测试SQLite数据库中是否有特定的数据 该方法接受主题代码、人员id和表名。我100%确信这三件事是正确的 这应该做的是尝试选择一个记录。如果可以选择记录,则返回-1,否则返回0 我的问题是,当我的数据库中有记录时,datareader似乎没有读取任何记录SQLite测试是否存在记录,sqlite,validation,select,testing,Sqlite,Validation,Select,Testing,我正在努力测试SQLite数据库中是否有特定的数据 该方法接受主题代码、人员id和表名。我100%确信这三件事是正确的 这应该做的是尝试选择一个记录。如果可以选择记录,则返回-1,否则返回0 我的问题是,当我的数据库中有记录时,datareader似乎没有读取任何记录 public int TestIfExists(string subID, string personID, string table) { _sqlConnection = new SQLiteConnection(_con
public int TestIfExists(string subID, string personID, string table)
{
_sqlConnection = new SQLiteConnection(_conStr);
bool dataRead = false;
int rc = 0;
try
{
string selectQuery = "SELECT * FROM " + table + " WHERE PersonID = '" +
personID + "' AND SubjectCode = '" + subID + "'";
_sqlConnection.Open();
SQLiteCommand sqlCommand = new SQLiteCommand(selectQuery, _sqlConnection);
IDataReader idr = sqlCommand.ExecuteReader();
dataRead = idr.Read();
if (dataRead == true)
{
rc = -1;
}//end if
else
{
rc = 0;
}//end else
idr.Close(); // Closed IDataReader
}//end try
catch (SQLiteException sqlEx) // Catch SQLiteException
{
MessageBox.Show(sqlEx.ToString());
throw new DataStoreError(sqlEx.Message);
}//end catch
catch (Exception ex)
{
throw ex;
}//end catch
finally
{
_sqlConnection.Close();
}//end finally
return rc; //Single return
}
当您试图查看它是否存在时,您可以执行
从表中选择计数(*),其中(…)
这样,0就意味着不存在,或者说是的