我想檢查數據庫中是否已經存在某個ID。如果沒有,我希望用戶將ID更改爲其他內容。檢查數據庫文件中是否存在ID時出錯
所有這些都在textobx的TextChanged
函數中完成。
的問題是,我得到一個錯誤,因爲查詢看起來不錯,我不知道爲什麼我看到:The SELECT statement includes a reserved word or an argument name that is misspelled or missing, or the punctuation is incorrect.
方法,做了檢查:
private bool DoesIDExist(int dataID, string filePath)
{
HashPhrase hash = new HashPhrase();
DataTable temp = new DataTable();
string hashShortPass = hash.ShortHash(pass);
bool result = false;
// Creating a connection string. Using placeholders make code
// easier to understand.
string connectionString =
@"Provider=Microsoft.ACE.OLEDB.12.0; Data Source={0};
Persist Security Info=False; Jet OLEDB:Database Password={1};";
string sql = string.Format
("SELECT FROM PersonalData WHERE [DataID] = {0}", dataID);
using (OleDbConnection connection = new OleDbConnection())
{
// Creating command object.
// Using a string formatting let me to insert data into
// place holders I have used earlier.
connection.ConnectionString =
string.Format(connectionString, filePath, hashShortPass);
using (OleDbCommand command = new OleDbCommand(sql, connection))
{
// Creating command object.
// Using a string formatting let me to insert data into
// place holders I have used earlier.
connection.ConnectionString =
string.Format(connectionString, filePath, hashShortPass);
try
{
// Open database connection.
connection.Open();
using (OleDbDataReader read = command.ExecuteReader())
{
// Checking if there is any data in the file.
if (read.HasRows)
{
// Reading information from the file.
while (read.Read())
{
if (read.GetInt32(0) == dataID)
return true;
}
}
}
}
catch (Exception ex)
{
MessageBox.Show("Error: " + ex.Message);
}
}
}
return result;
}
大聲笑。天才人總是想念小事:P –