2014-07-09 96 views
2

我试图改变与MS Access数据库密码选项....错误:类型“System.Data.OleDb.OleDbDataReader”有没有构造函数定义

请帮我人....

这里的代码: default.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 [email protected]"; 
       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; 
    } 
} 

还要检查PLS .....

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:  

回答

0

由于MSDN中明确规定,To create an OleDbDataReader, you must call the ExecuteReader method of the OleDbCommand object, instead of directly using a constructor.

不能使用new,这是你在做什么,这就是为什么你的错误实例化。删除有问题的行并将其更改为这个摆脱错误的:

OleDbDataReader reader = cmd.ExecuteReader(); 

另外,记得使用using块,以确保资源得到妥善处置。

using(OleDbConnection myCon = new OleDbConnection(ConfigurationManager.ConnectionStrings["vhgroupconnection"].ConnectionString)) 
{ 
OleDbCommand cmd = new OleDbCommand(q, myCon); 

//Add parameters etc 

OleDbDataReader reader = cmd.ExecuteReader(); 

//Rest of the processing 
} 
1

如错误消息所示; 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来处理您的OleDbConnection,OleDbCommandOleDbDataReader like;

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()) 
    { 
     // 
    } 
} 

而作为史蒂夫mentionedOleDbDataReader.Read method回报boolean值(falsetrue),它读取您的OleDbDataReader结果行通过行。您可能需要考虑使用此方法的结果,如,而语句。例如;

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

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

+0

+1但是也有一个非常错误的代码的第二部分没有检查的结果reader.Read() – Steve

+0

@Steve是的。更新了我的答案。谢谢。 –

0

问题:您尝试通过调用new OleDbDataReader()而应该创建一个使用OleDbCommand.ExecuteReader()读者做出OleDbDataReader新实例。

在下面的代码中注意使用using声明(这应该确保连接关闭或关闭OleDbDataReader的情况下)。

protected void Button1_Click(object sender, EventArgs e) 
{ 
    try 
    { 
     string sConnString = ConfigurationManager.ConnectionStrings["vhgroupconnection"].ConnectionString; 
     using(OleDbConnection myCon = new OleDbConnection(sConnString)) 
     { 
      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); 

      string sUserId = string.Empty; 
      string sPass = string.Empty; 

      using(OleDbDataReader reader = cmd.ExecuteReader()) 
      { 
       if(reader.Read()) //assumption: one record returned 
       {   
        sUserId = reader["user_id"].ToString(); 
        sPass = reader["passwd"].ToString(); 

       } 
      } 

      if (sUserId != string.Empty && sPass != 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 [email protected]"; 
        cmd = new OleDbCommand(q, myCon); 
        cmd.Parameters.AddWithValue("@newPass", txtnewpass.Text); 
        cmd.Parameters.AddWithValue("@userid", txtuserid.Text);      

        int count = cmd.ExecuteNonQuery(); 

        if (count > 0)     
         lblmsg.Text = "Password changed successfully";     
        else     
         lblmsg.Text = "password not changed"; 

       } 
      } 
     } 
    } 
    catch (Exception ex) 
     { 
     throw ex; 
     } 
} 
+0

感谢民间...但我再次得到的错误为“对象引用未设置为对象的实例”。 描述:执行当前Web请求期间发生未处理的异常。请查看堆栈跟踪以获取有关该错误的更多信息以及源代码的位置。 异常详细信息:System.NullReferenceException:未将对象引用设置为对象的实例。 Source Error: Line 76:catch(Exception ex) Line 77:{ Line 78:throw ex; 79行:} 行80:} – prasath

+0

我修改了答案。你现在可以检查吗? – Hassan

相关问题