我的c#代码不会更新访问数据库中的记录 [英] My c# code does not update the record in access database

查看:59
本文介绍了我的c#代码不会更新访问数据库中的记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  大家好,这是一个名为Forgot password.aspx $的页面上的代码b $ b我需要用户提供他们的电子邮件,以便他们重置密码。

问题是程序运行,但数据库中的记录没有更新,用户只是重定向到登录页面

我做错了什么?
请帮忙!


protected void save_Password_Click( object sender,EventArgs e)
{


OleDbConnection connect = new OleDbConnection();
connect.ConnectionString = @ Provider = Microsoft.ACE.OLEDB.12.0;
数据源= C:\Users\Student\Documents\websiteDatabase.accdb;


OleDbDataAdapter adapter = new OleDbDataAdapter( SELECT password FROM userInformation where emailAddress =' + p_Email.Text + ',连接);


DataTable dt = new DataTable();


adapter.Fill(dt);



如果(dt.Rows.Count.ToString()== 1
{

if (tb_newPassword.Text == tb_Confirm.Text)
{

// 打开连接
connect.Open();

OleDbCommand cmd = connect.CreateCommand();


cmd.CommandText = UPDATE userInformation SET password =' + tb_Confirm.Text + 'WHERE emailAddress =' + p_Email.Text + ';


MessageBox.Show( 密码已成功更改! );


Response.Redirect( 登录Page.aspx);

// 关闭连接
connect.Close() ;
}
else
{

lbl.Text = 密码不匹配!;

}
}
else
{

lbl.Text = 更改失败,请再试一次;

}

}





我的尝试:



我试过寻找查询数据库更新语句的其他方法,但我没有找到任何明确的解决方案

解决方案

你似乎错过了一段实际执行命令的代码。



 尝试 
{
connect.Open();
OleDbCommand cmd = connect.CreateCommand();
// 现在创建命令文本,这将是您将要执行的命令
cmd.CommandText = UPDATE userInformation SET password =' + tb_Confirm.Text + < span class =code-string> 'WHERE emailAddress =' + p_Email.Text + ';
// 您将不得不执行此命令并且您错过了此语句
cmd.ExecuteNonQuery();
// 现在显示您的消息,不要忘记关闭连接
MessageBox.Show( 密码已成功更改!);
connect.Close();
}
catch (例外情况)
{
// 选择您要对异常做什么
}


尝试在finally块中关闭连接并检查,

在打开之前检查连接状态

if(con.State!= ConnectionState.Open)

con.Open();

"Hi guys this is code on a page titled Forgot password.aspx
I need the user to provide their email, in order for them to reset their password.

The problem is the program does run, but the record in the database is not updated, and the user is just redirected to the sign in page

Am I doing something wrong?
please help!"

protected void save_Password_Click(object sender, EventArgs e)
    {
       
        
        OleDbConnection connect = new OleDbConnection();
		connect.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;
                                        Data Source=C:\Users\Student\Documents\websiteDatabase.accdb";

        
		OleDbDataAdapter adapter = new OleDbDataAdapter("SELECT password FROM userInformation where emailAddress = '" + p_Email.Text + "'", connect);

       
		DataTable dt = new DataTable();

        
		adapter.Fill(dt);


        
		if ( dt.Rows.Count.ToString() == "1")
		{
            
			if(tb_newPassword.Text == tb_Confirm.Text)
			{

                //Opens the connection
                connect.Open();

                OleDbCommand cmd = connect.CreateCommand();   
   
               
                cmd.CommandText = "UPDATE userInformation SET password ='" + tb_Confirm.Text + "' WHERE emailAddress = '" + p_Email.Text  + "'";
				
                
                MessageBox.Show("Password changed successfully!");

                
                Response.Redirect("Sign In Page.aspx");

                //Closes the connection
				connect.Close();
			}
			else
			{
                
				lbl.Text = "Passwords do not match!";

			}
		}
		else
		{
            
			lbl.Text=" Change unsuccessful, please try again";

		}
        
    }



What I have tried:

I have tried looking for other ways to query the database update statement, but I have not found any clear solutions

解决方案

You seem to be missing a block of code to actually execute the command.

try
{
connect.Open();
OleDbCommand cmd = connect.CreateCommand();
//You create your command text now, this will be the command which you will execute 
cmd.CommandText = "UPDATE userInformation SET password ='" + tb_Confirm.Text + "' WHERE emailAddress = '" + p_Email.Text  + "'";
//You will have to execute this command and you were missing this statement
cmd.ExecuteNonQuery();
//Now display your message and don't forget to close your connection 
MessageBox.Show("Password changed successfully!");
connect.Close();
}
catch(Exception ex)
{
//choose what you want to do with the exception
}


try closing connection in the finally block and check,
check the connection state before opening
if (con.State != ConnectionState.Open)
con.Open();


这篇关于我的c#代码不会更新访问数据库中的记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆