2010-03-31 107 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