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

당신이 예외를 받고 있습니까? –

+0

아니요 .............. – tom

+0

DB에 cid = 1이 없습니까? –

답변

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

+0

우리는 당신을 도울 수 있기 위해 "일하지 않는다"것보다 좀 더 구체화되어야합니다. –

+0

이 문장을 실행 한 후'rowsUpdated'의 값은 무엇입니까? –

관련 문제