【问题标题】:Error while checking if ID exists in database file检查数据库文件中是否存在 ID 时出错
【发布时间】: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


    【解决方案1】:

    我认为您的选择缺少一些您想要提取的列??

    string sql = string.Format
        ("SELECT FROM PersonalData WHERE [DataID] = {0}", dataID);
    

    不应该是这样的:

    string sql = string.Format
        ("SELECT * FROM PersonalData WHERE [DataID] = {0}", dataID);
    

    【讨论】:

    • 大声笑。天才的人总是会错过一些小事:P
    【解决方案2】:

    您缺少要选择的内容

    string sql = string.Format
        ("SELECT FROM PersonalData WHERE [DataID] = {0}", dataID);
    

    改成类似

    string sql = string.Format
        ("SELECT * FROM PersonalData WHERE [DataID] = {0}", dataID);
    

    而且:您可以像构建查询一样使用 SQL 注入。

    【讨论】:

    • 是的。我只是想让它发挥作用。现在我将使用参数。但是,由于我正在检查字段中是否存在空字段、非数字字符或不存在的 ID,因此我相信我是安全的。程序有点简单:)
    【解决方案3】:

    您需要在SELECT 子句中指定一些内容。我猜:

    SELECT DataID FROM PersonalData WHERE ...
    

    【讨论】:

      【解决方案4】:

      问题在于这行代码:

      string sql = string.Format
              ("SELECT FROM PersonalData WHERE [DataID] = {0}", dataID);
      

      您需要指定要选择的内容。示例:SELECT *SELECT [MyColumn]SELECT TOP 1 * 等。根据您的要求,您正在寻找类似的东西:

      string sql = string.Format
       ("SELECT COUNT(*) AS UserCount FROM PersonalData WHERE [DataID] = {0}", dataID);
      

      其他信息:

      如果在网络上使用此方法,例如从查询字符串中提取一个 ID,那么您将面临 SQL 注入攻击。稍微修改您的代码将解决问题:

      string sql = "SELECT FROM PersonalData WHERE [DataID] = @DataID";
      
      using (OleDbCommand command = new OleDbCommand(sql, connection))
              {
                  command.Parameters.AddWithValue("@DataID", dataID);
              }
      

      【讨论】:

      • @HelpNeeder,太好了!不幸的是,很多人不是。出于存档目的,我尝试在回答中冗长。此外,经过深思熟虑的答案通常会获得更多支持:)
      猜你喜欢
      • 2010-10-22
      • 2010-11-24
      • 2021-12-11
      • 2016-09-04
      • 1970-01-01
      • 1970-01-01
      • 2020-04-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多