0

我想从 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();

             {

             }
         }
4

1 回答 1

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

于 2013-07-15T01:41:27.413 回答