将arraylist插入数据库C#webservice

时间:2016-12-15 06:29:54

标签: c# arrays web-services

我正在创建一个Web应用程序,我需要在其中插入一些值。这是我的网络服务:

[WebMethod]
[ScriptMethod(UseHttpGet = true)]
public void saverecd(string id, string particular,string amt,string tdate, string total, string date, string utrno, string modeofpayment, string transferdate,string trainer, string typeofadj)
{
    sqlq = "";
    sqlq = "insert into finalinstructoreexpense(sonvinid,particulars,amount,totalamt,date,utno,paymentid,paymode,issuedate,sondate,trainer,type,bank_id) values('" + id + "','" + particular + "','" + amt + "','" + total + "',convert(datetime,'" + date + "',105),'" + utrno + "','" + paymentid + "','" + modeofpayment + "',convert(datetime,'" + transferdate + "',105),convert(datetime,'" + tdate + "',105),'" + trainer + "','" + typeofadj + "',null)";
    con.Open();
    SqlCommand comm1 = new SqlCommand(sqlq, con);
    comm1.ExecuteNonQuery();
    message = "Adjusted Amount Inserted Successfully";
    con.Close();
    Context.Response.Write(message);
}

这里我特别注意13111300002,13111300001,我想将这些值分别存储在我的数据库中:

1531    20667   13111300002,13111300001 200 200 2013-12-15 00:00:00.000 test    1312150001  Online  2013-01-01 00:00:00.000 2013-11-13 00:00:00.000 Ibrahim shaikh  split   NULL

这是我的数据存储在数据库中的方式:

1531    20667   13111300002 200 200 2013-12-15 00:00:00.000 test    1312150001  Online  2013-01-01 00:00:00.000 2013-11-13 00:00:00.000 Ibrahim shaikh  split   NULL
1531    20667   13111300001 200 200 2013-12-15 00:00:00.000 test    1312150001  Online  2013-01-01 00:00:00.000 2013-11-13 00:00:00.000 Ibrahim shaikh  split   NULL

我想存储我的数据。

我需要做什么?

对不起解释

2 个答案:

答案 0 :(得分:0)

您可以使用逗号分隔符拆分特定变量,并进行多次数据库调用以按照您希望的方式插入数据。

<div class="cover">
  <div class="jumbotron">
    <h1>hello you</h1>
  </div>
</div>

答案 1 :(得分:0)

首先,您需要将Query更改为参数化查询,因为它容易SQL Injection。然后你可以拆分,附带的字符串并运行一个循环来获取数据库中的值。

public void saverecd(string id, string particular,string amt,string tdate, string total, string date, string utrno, string modeofpayment, string transferdate,string trainer, string typeofadj)
{
    List<string> sp = particular.split(',');
    int i = 0;
    foreach(string s in sp)
    {
        using (SqlConnection connection = new SqlConnection(/* connection info */))
        {
            sqlq = "insert into finalinstructoreexpense(sonvinid,particulars,amount,totalamt,date,utno,paymentid,paymode,issuedate,sondate,trainer,type,bank_id) values(@id,@s,@amt,@total,@dt,@utrno,@paymentid,@modeofpayment,@transferdate,@tdate,@trainer,@typeofadj,null)";
            connection.Open();
            using (SqlCommand comm1 = new SqlCommand(sql, connection))
            {
                comm1.Parameters.Add("@id",SqlDbType.Int).value=id;
                comm1.Parameters.Add("@s",SqlDbType.Varchar, 50).value =s;
                comm1.Parameters.Add("@amt",SqlDbType.Varchar, 50).value =amt.Split(',')[i];;
                comm1.Parameters.Add("@trainer",SqlDbType.Varchar, 50).value =trainer;
                comm1.Parameters.Add("@dt", SqlDbType.Date).Value = date;
                //You can add all your Parameters here
                // I have added 3 types of as Int, Varchar and Date to give the idea
                comm1.ExecuteNonQuery();
                message = "Adjusted Amount Inserted Successfully";
                Context.Response.Write(message);
                i++;
            }
        }
    }
}