gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 13:20:05 25 4
gpt4 key购买 nike

我正在尝试使用 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com