0

我正在 asp.net、c#、MS-access 数据库中设计一个更改密码屏幕,我有 4 个字段 userid、oldpassword、newpassword 确认密码

现在我没有得到结果计数返回 0 我已经更新了我的代码

我的代码如下

      try

      {

      OleDbConnection myCon = new  OleDbConnection(ConfigurationManager.ConnectionStrings["vhgroupconnection"]
       .ConnectionString);
        myCon.Open();

        string userid = txtuserid.Text;
        string oldpass = txtoldpass.Text;
        string newPass = txtnewpass.Text;
        string conPass = txtconfirmpass.Text;

        string q = "select user_id,passwd from register where user_id = @userid and       passwd = @oldpass";

        OleDbCommand cmd = new OleDbCommand(q, myCon);



        cmd.Parameters.AddWithValue("@userid", txtuserid.Text);

        cmd.Parameters.AddWithValue("@oldpass", txtoldpass.Text);

      OleDbDataReader re = cmd.ExecuteReader();
        re.Read();

        if (re["user_id"].ToString() != String.Empty && re["passwd"].ToString() != String.Empty)
        {
            if (newPass.Trim() != conPass.Trim())
            {
                lblmsg.Text = "New Password and old password does not match";

            }
            else
            {
               q = "UPDATE register SET  passwd = @newPass WHERE user_id =@userid";
                cmd = new OleDbCommand(q, myCon);
                cmd.Parameters.AddWithValue("@userid", txtuserid.Text);
                cmd.Parameters.AddWithValue("@newPasss", txtnewpass.Text);



                int count = cmd.ExecuteNonQuery();

                if (count > 0)
                {
                    lblmsg.Text = "Password changed successfully";
                }
                else
                {
                    lblmsg.Text = "password not changed";
                }
            }
        }
    }
    catch(Exception ex)
    {
        throw ex;
    }

请帮我解决错误

4

1 回答 1

2

您收到错误,No constructor is defined因为您无法直接实例化此对象。如MSDN所述:

要创建 OleDbDataReader,您必须调用 OleDbCommand 对象的 ExecuteReader 方法,而不是直接使用构造函数。

本质上,在创建连接并指定查询后,您将执行以下操作:

OleDbDataReader re = cmd.ExecuteReader();
于 2013-04-17T17:43:42.450 回答