如何一次将一个数据插入到数据库中 [英] how to insert on by one data into database not at a time

查看:53
本文介绍了如何一次将一个数据插入到数据库中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有3个数组值

a [i] = 1,2,3

b [j] = 4,5,6

c [k] = 7,8,9

i想要一个循环,它会插入它需要1,4,7 AND分别插入数据库列a,b,c再次它会回来拿走2,5,8并在第2行插入相应的列,依此类推。





评论更新:



i我正在使用



i have 3 arrays value
a[i]=1,2,3
b[j]=4,5,6
c[k]=7,8,9
i want a loop when it will insert it will take 1,4,7 AND insert into database column a,b,c respectively again it will come back and take 2,5,8 and insert the respective column in 2nd line and so on.


UPDATE from comments:

i am using

for (int i = 0; i <= Skill.Length; i++)
{
  foreach (string element in Skill)
  {

    // Cannot assign element in this loop.
    Tst.skills = element;
  }

  Tst.Level = level;
  foreach (string element in level)
  {
    // Cannot assign element in this loop.
    Tst.levels = element;
  }
  Tst.Percentage = Percentages;
  foreach (string element in Percentages)
  {
    // Cannot assign element in this loop.
    Tst.percentages = element;
  }

推荐答案

如果数组大小相同,那么你可以使用那个大小的for循环。 />


我不知道你的数据是如何创建的,但我会在我的例子中加入一张支票:



If the arrays are the same size then you can just use a for loop of that size.

I don't know how your data is created but I will include a check in my example:

if(a.length == b.length && c.length==b.length){
    for(int x = 0; x < a.length; x++){
      parameterA = a[x];
      parameterB = b[x];
      parameterC = c[x];

      //execute the query
    }
} 







更新来自更新的问题:



你在做什么,......这是错的。请允许我解释一下:






Update from updated question:

What you are doing is,... well it's wrong. Allow me to explain:

//I'll use these test arrays as an example:

string[] Skill = ["a","b","c"];
string[] level= ["1","2","3"];
string[] Percentages= ["10","20","30"]; 

for (int i = 0; i <= Skill.Length; i++)
{
  //This loop will occure three times as Skill.Length == 3

  foreach (string element in Skill)
  {
    //This loop will occur 9 time.  Skill (for loop) * Skill (foreach loop) 
    Tst.skills = element;
    //Each time this runs it will do this:
    //Tst.skills = "a";
    //Tst.skills = "b";
    //Tst.skills = "c";
  }
  // Tst.skill will always = "c";
 
  Tst.Level = level;
  foreach (string element in level)
  {
    //Same as before = this will always output Tst.levels = "3";
    Tst.levels = element;
  }
  Tst.Percentage = Percentages;
  foreach (string element in Percentages)
  {
    // Same here
    Tst.percentages = element;
  }





简化代码:





Simplify your code:

for (int i = 0; i <= Skill.Length; i++)
{
  //This loop will occure three times as Skill.Length == 3

  //this will now occur once per loop
  Tst.skills = Skills[i];

  //The index i changes each loop so Tst.skills will change each loop

  Tst.Level = level;
  Tst.levels = level[i];

  Tst.Percentage = Percentages;
  Tst.percentages = Percentages[i];

}







显然你的代码错了,但是你的缩进仍然不清楚。为什么:

Tst.Level = level;

Tst.Percentage =百分比;



这是必需的吗?



为什么要使用for循环所有?



我只是想确保我明白你要做什么^ _ ^




It is clear that your code was wrong, but your indention is still unclear. Why do this:
Tst.Level = level;
Tst.Percentage = Percentages;

Is this required?

Why use the for loops at all?

I just want to make sure I understand what you're trying to do ^_^


那怎么能我逐个发送到数据库从一个数组中取出
then how can i send one by one to database take each from one array


这篇关于如何一次将一个数据插入到数据库中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆