如何在web服务asp.net中执行insert语句

时间:2013-07-15 01:38:26

标签: asp.net sql

我想从android接收数据到我的网络服务,我的网络服务将把数据插入我的数据库。我将如何处理将我的Web服务中的数据插入数据库。

这是我的代码开头:

 [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 个答案:

答案 0 :(得分: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();
    }

像这样添加值和参数:

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

尝试以上方法。 在这里阅读更多: http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.executenonquery.aspx