2010-03-31 148 views
0
string ConnectionString = WebConfigurationManager.ConnectionStrings["dbnameConnectionString"].ConnectionString; 
SqlConnection myConnection = new SqlConnection(ConnectionString); 

myConnection.Open(); 
try 
{ 
    string qry = "UPDATE customers SET [email protected] WHERE cid=1"; 
    SqlCommand insertQuery = new SqlCommand(qry, myConnection); 
    insertQuery.Parameters.Add(new SqlParameter("@firstname", txtFirstname.Text)); 
    insertQuery.ExecuteNonQuery(); 

    myConnection.Close(); 
} 
catch (Exception ee) 
{ 

} 

有什麼建議嗎?asp.net無法更新數據庫

+0

您是否獲得一個例外? – 2010-03-31 21:13:02

+0

否.............. – tom 2010-03-31 21:19:46

+0

所以你的db中沒有cid = 1? – 2010-03-31 21:21:28

回答

0

你的代碼的一些清理你可能想試試(你應該處置IDisposable的對象):

string ConnectionString = // ... 
using (var connection = new SqlConnection(ConnectionString)) 
using (var command = connection.CreateCommand()) 
{ 
    connection.Open(); 
    command.CommandText = "UPDATE customers SET firstname = @firstname WHERE cid=1"; 
    command.Parameters.AddWithValue("@firstname", txtFirstname.Text); 
    var rowsUpdated = command.ExecuteNonQuery(); 
} 
+0

仍然無法正常工作,但謝謝。 – tom 2010-03-31 21:19:23

+0

爲了讓我們能夠爲您提供幫助,您將不得不比「不行」更具體。 – 2010-03-31 21:20:16

+0

執行此語句後'rowsUpdated'的值是什麼? – 2010-03-31 21:22:04