2015-04-17 126 views
5

我想用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 protected],CellNumber [email protected]", 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(); 
+1

不相關,但我建議EntityFramework,而不是使用SQL API與您自己的查詢。 – mirichan

+4

你用SQL Server Profiler跟蹤了你的查詢嗎?是否有一些命令實際上被髮送到服務器?注意:你正在使用'update'而沒有'where'條件 - 所以你會將表中的所有**行更新爲相同的值。 –

+0

我認爲歐普應該首先闡述「它不起作用」的部分。它究竟如何「不起作用」? @op如果你沒有得到任何錯誤,那麼也許代碼**會**工作,而不是你想要的方式。 – Banana

回答

2

你的問題是你正在打開一個連接(這是正確的),但是你又打開了它。

這就是更新沒有發生的原因。你也不需要在最後的聲明中關閉你的連接。爲此目的,這不是必需的。

此外,您正在執行更新聲明,因此請確保您爲其提供更新特定記錄的條件。

我已經適當地修改你的代碼,這應該解決您的問題: (久經考驗的)


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 protected],CellNumber [email protected] WHERE Email [email protected]", 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 
    } 
} 

讓我知道結果的。

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); 
} 
+0

爲什麼沒有SQL參數? – Sybren

+0

因爲你並不需要它們。例如更新買方設置名稱='弗雷'其中名稱='弗雷迪' – Ewan

+0

因此,你不是被迫使用它們,你不使用它們?我認爲使用它們更好。 – Sybren

0

我已經使用嘗試捕捉或代碼:

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 protected],CellNumber [email protected]", 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();是真的,如果發生任何事情。