【发布时间】:2011-12-06 14:28:45
【问题描述】:
我正在尝试检查某个 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;
}
【问题讨论】:
标签: c# .net sql datareader