c# - 更新数据库中的信息

标签 c# sql asp.net sql-server

我想使用 C# 更新数据库 im 上的用户信息。 这是我的代码,它不起作用,也没有给我任何错误。

protected void Page_Load(object sender, EventArgs e)
{
    lbldisplay.Text = "<b><font color=BLUE>" + "WELLCOME:: " + "</font>" + "<b><font color=white>" + Session["Name"] + "</font>";
    SqlConnection con = new SqlConnection("Data Source=STUDENT-PC;Initial Catalog=webservice_BuyBid;Integrated Security=True");
}
protected void btnLogOut_Click(object sender, EventArgs e)
{
    //this will redirect the page to the home.aspx page.
    Response.Redirect("Home.aspx");
}

protected void btnUpdate_Click(object sender, EventArgs e)
{
    //creating a new connection to the database
    SqlConnection con = new SqlConnection("Data Source=STUDENT-PC;Initial Catalog=BuyBid;Integrated Security=True;Asynchronous Processing=True;");
    con.Open();
    //creates a new sqlcommand to update the buyer's information to the Database.
    SqlCommand cmd = new SqlCommand("UPDATE BUYER SET Name = @Name,Surname = @Surname,Email =@Email,CellNumber =@CellNumber", con);
    con.Open();

    cmd.Parameters.AddWithValue("@Name", txtNameUpdate.Text);
    cmd.Parameters.AddWithValue("@Surname", txtSurnameUpdate.Text);
    cmd.Parameters.AddWithValue("@Email", txtemailUpdate.Text);
    cmd.Parameters.AddWithValue("@CellNumber", txtCellUpdate.Text);

   int rows = cmd.ExecuteNonQuery();
    con.Close();

最佳答案

您的问题是您正在打开一个连接(这是正确的),但随后您又打开了它。

这就是更新没有发生的原因。此外,您无需在 finally 语句中关闭连接。出于此目的,它不是必需的。

同时你正在执行更新语句,所以请确保你给它一个条件来更新特定的记录。

我已经适本地编辑了您的代码,这应该可以解决您的问题: (久经考验)


    protected void btnUpdate_Click(object sender, EventArgs e)
    {
        try
        {
            //creating a new connection to the database
            SqlConnection con = new SqlConnection("Data Source=STUDENT-  PC;Initial Catalog=BuyBid;Integrated Security=True;Asynchronous  Processing=True;");
            con.Open();
            //creates a new sqlcommand to update the buyers information to the   Database.
            SqlCommand cmd = new SqlCommand("UPDATE BUYER SET Name = @Name,Surname  = @Surname,Email =@Email,CellNumber =@CellNumber WHERE Email =@Email", con);
        //con.Open();

            cmd.Parameters.AddWithValue("@Name", txtNameUpdate.Text);
            cmd.Parameters.AddWithValue("@Surname", txtSurnameUpdate.Text);
            cmd.Parameters.AddWithValue("@Email", txtemailUpdate.Text);
            cmd.Parameters.AddWithValue("@CellNumber", txtCellUpdate.Text);
            cmd.BeginExecuteNonQuery();
        }

    catch (Exception e)
    {
        Console.WriteLine("Exception occured: " + e.StackTrace);
    }
   finally
   {
       // close connection if it is still open

       // editing from phone so just writting the comments here
    }
}

让我知道结果。

关于c# - 更新数据库中的信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29695153/

相关文章:

c# - 在 C#6.0 中编码但针对 .NET 2.0 进行编译的缺点

sql - 键不在表中

sql - 在 SQL Server 中插入时忽略重复键

c# - 在浏览器中加载虚拟目录名称时,AcquireRequestState 中的 Session 为空,但在加载 Default.aspx 时不为空

c# - 在页面执行长时间运行的任务时写入多行文本

c# - 我应该将应用程序范围的对象传递给需要它的对象,还是应该使用单例?

c# - 如何打破 Parallel.ForEachAsync 循环,而不是取消它?

c# - 如何在仍然使用 ASP.NET MVC 的同时为网站开发快速原型(prototype)?

php - MySQL:如果另一个表包含某个值,则在对字段求和时忽略行

asp.net - 无法访问 Docker 内的 Asp.Net Core