简体   繁体   English

使用foreach循环插入多个记录C#

[英]inserting multiple record using foreach loop c#

I am creating a web app in which i need to insert multiple records in my database, this is how it is looking 我正在创建一个Web应用程序,其中需要在数据库中插入多个记录,这就是它的外观

List<string> td = tdate.Split(',').ToList();
//List<string> ps = particular.Split(',').ToList();
int i = 0;
foreach (string t in td)
foreach (string p in ps)
     {
       SqlCommand cmd = new SqlCommand("insert into finalinstructoreexpense(sonvinid,particulars,amount,totalamt,date,utno,paymentid,paymode,issuedate,sondate,trainer,type,bank_id) values(@sonvinid,@particulars,@amount,@totalamt,@date,@utno,@paymentid,@paymode,@issuedate,@sondate,@trainer,@type,@bank_id)", con);
       con.Open();
       cmd.Parameters.Add("@sonvinid", SqlDbType.Int).Value =Convert.ToInt32(id);
       cmd.Parameters.Add("@particulars", SqlDbType.NVarChar).Value = p;
       cmd.Parameters.Add("@amount",SqlDbType.Float).Value=adjamt.Split(',')[i];
       cmd.Parameters.Add("@totalamt", SqlDbType.NVarChar).Value = total;
       cmd.Parameters.Add("@date", SqlDbType.DateTime).Value = date.Split(',')[i];
       cmd.Parameters.Add("@utno", SqlDbType.NVarChar).Value = utrno;
       cmd.Parameters.Add("@paymentid",SqlDbType.NVarChar).Value=paymentid;
       cmd.Parameters.Add("@paymode", SqlDbType.NVarChar).Value = modeofpayment;
       cmd.Parameters.Add("@issuedate", SqlDbType.DateTime).Value = transferdate;
       cmd.Parameters.Add("@sondate", SqlDbType.DateTime).Value = t;
       cmd.Parameters.Add("@trainer", SqlDbType.NVarChar).Value = trainer;
       cmd.Parameters.Add("@type", SqlDbType.NVarChar).Value = typeofadj;
       cmd.Parameters.Add("@bank_id", SqlDbType.Int).Value = Convert.ToInt32(bnkid);
       cmd.ExecuteNonQuery();
       message = "Adjusted Amount Inserted Successfully";
       con.Close();
     }

this problem is i have 4 arrays in the form of string ie tdate,particular,adjamt,date and the parameters look like 这个问题是我有4字符串形式的数组, ie tdate,particular,adjamt,date和参数看起来像

tdate(01-01-2013,03-01-2013)
particular(105,100)
adjamt(500,650)
date(2015,2016)

and this is how i want to enter the records in the database 这就是我想在数据库中输入记录的方式

(01-01-2013,105,500,2015)
(03-01-2013,100,650,2016)

but this is how it is actually inserting in my databas 但这是它实际上插入我的数据库的方式

(01-01-2013,105,500,2015)
(03-01-2013,105,500,2015)

it means only tdate is working properly but the values of other parameters are not changing and inserting the first value only, what i need to do, if i want to enter the records propely 这意味着只有tdate可以正常工作,但是其他参数的值没有更改,仅插入第一个值,如果我想正确输入记录,我需要做的事情

You are initialising i to zero: 您正在将i初始化为零:

int i = 0;

but not incrementing it in your loop. 但不要在循环中增加它。 So these lines: 所以这些行:

   cmd.Parameters.Add("@amount",SqlDbType.Float).Value=adjamt.Split(',')[i];
   cmd.Parameters.Add("@date", SqlDbType.DateTime).Value = date.Split(',')[i];

are always returning the first element of the array. 总是返回数组的第一个元素。

Add: 加:

i = i++;

to the end of your loop. 到循环结束

You really should split adjamt and date into lists before looping and use those lists inside the loop. 您确实应该在循环之前将adjamtdate分成列表,并在循环内使用这些列表。

You are also looping over both td and ps . 您还将遍历tdps This will add twice as many rows into your table as you want. 这将在您的表中添加两倍的行。 Remove the inner loop and replace: 拆下内环并更换:

   cmd.Parameters.Add("@particulars", SqlDbType.NVarChar).Value = p;

by 通过

   cmd.Parameters.Add("@particulars", SqlDbType.NVarChar).Value = ps[i];

where ps is created outside your loop: 在循环外创建ps位置:

var ps = particular.Split(',').ToList();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM