c# - 错误 : The type 'System.Data.OleDb.OleDbDataReader' has no constructors defined

标签 c# asp.net

我正在尝试使用 MS 访问数据库更改密码选项....

请大家帮帮我....

这里是代码: 默认.aspx.cs

protected void Button1_Click(object sender, EventArgs e)
{
    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);

        OleDbDataReader reader = new OleDbDataReader();



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

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



        reader = cmd.ExecuteReader();
        reader.Read();

        if (reader["user_id"].ToString() != String.Empty && reader["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("@newPasss", txtnewpass.Text);
                cmd.Parameters.AddWithValue("@userod", txtuserid.Text);
                cmd.Parameters.AddWithValue("@passwd", txtoldpass.Text);

                int count = cmd.ExecuteNonQuery();

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

也请检查......

Compilation Error Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

Compiler Error Message: CS0143: The type 'System.Data.OleDb.OleDbDataReader' has no constructors defined

来源错误:

Line 36:             OleDbCommand cmd = new OleDbCommand(q, myCon);
Line 37: 
Line 38:             OleDbDataReader reader = new OleDbDataReader();
Line 39:             
Line 40:    

最佳答案

如错误信息所述; OleDbDataReader没有构造函数。

来自 documentation of OleDbDataReader ;

To create an OleDbDataReader, you must call the ExecuteReader method of the OleDbCommand object, instead of directly using a constructor.

您可以使用 ExecuteReader返回 OleDbDataReader

的方法
OleDbDataReader dr = cmd.ExecuteReader();

并且您需要在调用ExecuteReader 方法之前添加参数值。

也可以使用 using statement处理您的 OleDbConnectionOleDbCommandOleDbDataReader

using(OleDbConnection myCon = new OleDbConnection(conString))
using(OleDbCommand cmd = myCon.CreateCommand())
{
    //Define your sql query and add your parameter values.

    using(OleDbDataReader dr = cmd.ExecuteReader())
    {
       //
    }  
}

作为史蒂夫 mentioned , OleDbDataReader.Read method返回 boolean 值(truefalse)并读取您的 OleDbDataReader 结果逐行。您可能需要考虑像在 while 语句 中一样使用此方法的结果。例如;

while(reader.Read())
{
    //Reads your results until the last row..
}

最后,我强烈怀疑您将密码存储为纯文本。 不要那样做!使用SHA-512 hash .

关于c# - 错误 : The type 'System.Data.OleDb.OleDbDataReader' has no constructors defined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24648081/

相关文章:

c# - 为什么没有捕获的 lambda 从 C# 5 中的静态方法更改为 C# 6 中的实例方法?

c# - 以可等待的方式等待 INotifyTaskCompletion 属性的结果?

javascript - 使用 javascript asp.net 在 gridview 的同一行中选中复选框时如何验证 gridview 中的文本框

c# - 如何在 Web API 中将 HttpResponse 设置为 HttpResponseMessage

c# - 将 List 转换为 JSON 以填充表

c# - LINQ to SQL 业务对象创建最佳实践

c# - 使用 Team Foundation Server DLL 作为类库中的引用

asp.net - 在ASP.NET中,用于在服务器端执行代码的HTML指令符号<%#或<%=等的名称是什么?

javascript - 如何轻松地从 MVC 中的复选框附加数据?

c# - 这个 `if` 语句是否会产生空引用异常?