2013-07-15 25 views
0

我想接收從android到我的web服務的數據,我的web服務將把數據插入到我的數據庫中。我該怎麼做才能將我的Web服務中的數據插入到數據庫中。如何在web服務中插入語句asp.net

這是我的代碼開始:

[WebMethod] 
    public StudentTransactions GetStudentTransactions(string Name, string CLass, string NRIC, int StallNo, float AmountSpent) 
    { 
     SqlCommand sql1 = new SqlCommand("INSERT Into StudentTransactions (Name, CLass,NRIC,StallNo,AmountSpent) VALUES (Name,CLass,NRIC,StallNo,AmountSpent)"); 
     using (SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ncpsdbbConnectionString2"].ConnectionString)) 
     { 
      conn.Open(); 

      { 

      } 
     } 

回答

1
using (SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ncpsdbbConnectionString2"].ConnectionString)) 
    { 
     SqlCommand command = new SqlCommand("INSERT Into StudentTransactions (Name, CLass,NRIC,StallNo,AmountSpent) VALUES (@Name,@CLass,@NRIC,@StallNo,@AmountSpent)"); 
     command.Connection.Open(); 
     command.ExecuteNonQuery(); 
    } 

添加值,而params像這樣:

command.Parameters.AddWithValue("@ParamName", [Some Value]); 

試試上面。 閱讀更多: http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.executenonquery.aspx

+0

我需要添加此聲明嗎?例如:cmd.Parameters.AddWithValue(「@ Country」,userInfo.Country); –

+0

是的,你必須定義每個插入參數的參數w/values – TGH

+0

我確定後命令.ExecuteNonQuery?什麼是一些價值? –