【问题标题】:My SqlDatareader was not working我的 SqlDatareader 不工作
【发布时间】:2016-07-14 06:55:29
【问题描述】:

我有一个用于获取customer_id 的代码,我使用SqlDataReader 从SQL Server 读取customer_id。我使用断点测试女巫并逐步调试,我了解SqlDataReader 条件未编译,编译器直接跳转到connection.close 行:

   string strQuery = "select customer_id from Registration where username=@username and password=@password";
    SqlConnection connection1 = DBConnection.getConnection();
    connection1.Open();

   SqlCommand cmd = new SqlCommand();
   cmd.Connection = connection1;
   cmd.CommandText = strQuery;

   cmd.Parameters.AddWithValue("username", txt1_username.Text);
   cmd.Parameters.AddWithValue("password", txt2_password.Text);

   string customer_id = cmd.ExecuteScalar() as string;

   connection1.Close();

   if (customer_id == null)
    {
    Messages myMsg = new Messages();
    myMsg.CreateMessageAlert("The User does not Registered or your using incorect username or password");

     } 

      else {
     Session["customer_id"] = customer_id; 
        }

【问题讨论】:

  • SqlDataReader 在哪里?

标签: asp.net sql-server-2008-r2


【解决方案1】:

虽然问题不是很清楚,但你可以尝试修改代码,考虑以下几点:

  • 无需为方法中的每个 sql 查询打开/关闭数据库连接。打开一次,执行所有查询,关闭。这将使代码更清晰、更快速。
  • 当您从其他地方进行连接时,请确保在打开之前将其关闭(例如:Check if SQL Connection is Open or Closed)
  • 您运行了 2 个查询,在这两种情况下都只得到 1 个结果(select count(*)、select customer_id)。那么为什么在第一种情况下你会在另一种情况下使用ExecuteScalar() 和ExecuteReader()?
  • 另一个想法是,如果您需要返回 2 个查询的结果,则不需要 2 个SqlCommand() 等。阅读Retrieving Multiple Result Sets using NextResult
  • 最后但并非最不重要 - 您似乎需要检查用户是否已注册,如果为真,请获取他的 ID。为什么不一口气做完呢?第二个查询对这两种情况都有好处 - 如果用户不存在,查询将不会返回任何结果,如果存在 - 他的 id 将被返回。这样做,您只需要一个查询和更少的编码。

更新:

更新后的代码看起来更加清晰和直接,但您没有明白我上一条评论的重点。如果你 select count(customer_id) 你会得到一个你不需要的计数。为什么不简单地select customer_id 并检查它是否被退回?

例子:

//string strQuery = "select count(customer_id) from Registration where username=@username and password=@password";
string strQuery = "select customer_id from Registration where username=@username and password=@password";
SqlConnection connection1 = DBConnection.getConnection();
connection1.Open();

SqlCommand cmd = new SqlCommand();
cmd.Connection = connection1;
cmd.CommandText = strQuery;

cmd.Parameters.AddWithValue("username", txt1_username.Text);
cmd.Parameters.AddWithValue("password", txt2_password.Text);

//int intRowCount = (int)cmd.ExecuteScalar();
string customer_id = cmd.ExecuteScalar() as string;

//txt1_username.Text = intRowCount.ToString(); <-- What's this?
connection1.Close();

//if (intRowCount == 1)
if (customer_id == null)
{
   // user does not exist, because sql returned no rows
   ... <-- do something here
} else {
   Session["customer_id"] = customer_id; 
}

更新 #2:

解决问题

  1. 确保txt1_username.Text 和txt2_password.Text 具有预期值。可能是您在某处重置了Text,这可能是查询未返回结果的原因。尝试硬编码代码中的值,例如,

    cmd2.Parameters.AddWithValue("username", "admin");
    cmd2.Parameters.AddWithValue("password", "123");
    
  2. 在 Sql Server Management Studio(或其他工具)中复制粘贴整个 sql 并从此处运行它以确保返回的结果。

  3. 确保针对正确的数据库执行它(也许您有不同的数据库,其中的数据不同的相同表)。

【讨论】:

  • 谢谢,你让我大开眼界,我改变了查询。现在,当我有一个注册用户时,如何获取 customer_id 并将其放入会话变量中?请看我编辑的代码。
  • 我更改了代码,但我在数据库中有一个具有该用户名和密码的用户,但总是向我显示消息(用户未注册...)。我用断点运行,但我不明白为什么 customer_id 应该为 Null ?尽管具有正确用户名和密码的此类用户已存储在数据库中。
  • 我在更新后的帖子中描述的故障排除。代码现在是正确的,它在用户输入(或从文本框获取数据的方式)或数据库中看起来都是数据问题。
  • 我用硬编码测试,我没有成功,接下来我在sql server中测试查询,它返回了表中的所有customer_id,这表明查询是正确的。我在 sqlserver 中附加了除此 db 之外的所有数据库。我检查了文本框,但程序不工作。我认为问题可能来自执行查询命令'string customer_id = cmd.ExecuteScalar() as string;'不是吗?
  • 是的,我是真的。问题来自执行行代码的字符串。我将其更改为 Tostring() 并且它可以工作。
【解决方案2】:

这是因为您的用户名不再是用户名。实际上是1,因为这条线

int intRowCount = (int) cmd.ExecuteScalar();
txt1_username.Text = intRowCount.ToString(); <-- RED FLAG

所以在If里面,你实际上是在运行

SELECT customer_id FROM registration WHERE username=1 and password=my_password

评论第 15 行,你应该做的很好。

【讨论】:

    【解决方案3】:

    更新

            string strQuery = "select count(*) from Employee where  FullName=@username";
            SqlConnection connection = DBConnection.getConnection();
            connection.Open();
    
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = connection;
            cmd.CommandText = strQuery;
    
            cmd.Parameters.AddWithValue("username", txt1_username.Text);
            cmd.Parameters.AddWithValue("password", txt2_password.Text);
    
            int intRowCount = (int)cmd.ExecuteScalar();
            txt1_username.Text = intRowCount.ToString();
            if (intRowCount == 1)
            {
                string strquery = "select customer_id from Registration where  username=@username and password=@password";
                SqlCommand cmd2 = new SqlCommand();
                cmd2.Connection = connection;
                cmd2.CommandText = strquery;
                cmd2.Parameters.AddWithValue("username", txt1_username.Text);
                cmd2.Parameters.AddWithValue("password", txt2_password.Text);
                SqlDataReader reader = cmd2.ExecuteReader();
                if (reader.Read())
                {
                    string customerID = reader[0].ToString();                    
                }
            }
            connection.Close();`
    

    这是您问题的完整解决方案。 不需要每次都打开连接。只需确保连接一旦使用就会关闭。

    【讨论】:

    • 我纠正了我的错误。但是编译器再次直接跳转到 connection.close 并且 sqldatareader 没有编译。为什么 sqldatareader 条件没有编译?
    • @Ahmed 我已经更新了评论。找到具有优化代码的完整解决方案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-22
    相关资源
    最近更新 更多