更新数据库中的信息

时间:2015-04-17 09:19:30

标签: 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();

3 个答案:

答案 0 :(得分:2)

您的问题是您正在打开连接(这是正确的),但之后您又要打开它。

这就是没有发生更新的原因。您也无需在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
    }
}

让我知道结果。

答案 1 :(得分:0)

为什么不在异步处理设置为false的情况下尝试此操作。注意表中的所有行都将更新,因为您的查询没有任何限制,即没有Where子句。

 try
{
    SqlConnection con = new SqlConnection("Data Source=STUDENT-PC;Initial Catalog=BuyBid;Integrated Security=True;Asynchronous Processing=False;");
    //creates a new sqlcommand to update the buyer's information to the Database.
    SqlCommand cmd = new SqlCommand("UPDATE BUYER SET Name = '" + txtNameUpdate.Text + "' ,Surname = '" + txtSurnameUpdate.Text + "', Email = '" + txtemailUpdate.Text + "', CellNumber = '" + txtCellUpdate.Text + "'", con);
    con.Open();

    int rows = cmd.ExecuteNonQuery();
    con.Close();
}
catch (Exception Ex)
{
    MessageBox.Show(Ex.Message + Environment.NewLine + Ex.TargetSite, "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
 }

答案 2 :(得分:0)

我使用过try catch或你的代码:

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 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();
    }
    }
    catch (Exception)
    {
    }
    finally
    {
        con.Close();
    }
}

使用它跟踪,检查是否con.open();是真的,如果有任何陷阱发生。