如何在数据库中插入循环值 [英] how to insert values of loop in database

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

问题描述

我正在将asp.net与c#一起使用.
我很困惑.请参见下面的代码.

i am using asp.net with c#.
i have a confusion.see the below code.

while (itr.MoveNext())   //this loop will rotate 26 time coz the value of itr is 26
            {
                Hashtable quote = ((Hashtable)(itr.Current));
                Response.Write(("Quote Symbol:" + quote["symbol"]));
                Response.Write(("Title:" + quote["title"]));                //1
                Response.Write(("Time:" + quote["time"]));

                if ((fxfeed.getInterval() == 1))
                {
                    if (fxfeed.getPrice().Equals("bid,ask"))
                    {
                        Response.Write(("Bid:" + quote["bid"]));
                        Response.Write(("Ask:" + quote["ask"]));
                    }
                    else
                    {
                        Response.Write(("Price:" + quote["price"]));
                    }
                }
                else
                {
                    Response.Write(("Open:" + quote["open"]));        //2
                    Response.Write(("High:" + quote["high"]));        //3
                    Response.Write(("Low:" + quote["low"]));        //4
                    Response.Write(("Close:" + quote["close"]));        //5
                }


正如我评论的那样,while循环将旋转26次.因此,每条由数字注释(//1,//2等)表示的时间行都将被打印在屏幕上.这意味着在while循环107完成时将打印(用注释数字表示)值.我有一个数据库,并希望将107个cols中的这107个值存储在数据库表中.所以有人可以告诉我将sql命令放在哪里插入数据,以及我需要在该命令中写些什么.请帮助我.


as i commented that the while loop will rotate 26 times. so each time lines which are represented by number comments(//1,//2 etc) these will be printed on screen. it means the at the completion of the while loop 107 values will be printed(which are represented by comments numbers). i have a database and want to store those 107 values in 107 cols in a database table. so can somebody please tell me where to put the sql command to insert the data and what do i need to write in that command. please help me.

推荐答案

在将循环插入数据库之前,您需要捕获循环中的所有数字.您不想在循环内进行数据库更新,因为我认为这可能会导致数据库开销.您可能可以做的是插入一个逻辑,该逻辑将在循环运行时获取所有数字并将其存储在集合中.然后,将集合的值作为参数放在SqlDataCommand上,然后进行数据库更新.

[更新到OP的后续问题]

这是您可能想做的一种方法.基本上,在循环内部,您可以将所有参数添加到集合中,例如字典.
You need to capture all the numbers on the loop before inserting it on the database. You don''t want to do the database update inside the loop since this, I think, might cause an overhead on your database. What you can probably do is insert a logic that will get all the numbers while loop is running and store it on a collection. And then, put the values of the collection as parameters on an SqlDataCommand, and then do the database update.

[Update to OP''s follow up question]

Here is one way you might want to do. Basically, inside the loop, you add all the parameters in a collection, like a dictionary, for example.
Dictionary<string, string> param = new Dictionary<string, string>();
while(itr.MoveNext())
{
   param.Add("@parameterName", "value"); // take note that you might execute this method more than once per execution of a loop, depending on what values you need to insert to your database.
   //other logic
}



收集所有必需的参数值之后,现在将它们添加到SqlCommand的参数上.请参见以下示例.



After gathering all the required parameter values, you now add them on the parameters of your SqlCommand. See the following example.

string query = "INSERT INTO YourTable (col1, col2, col3, etc....) VALUES(@val1, @val2, @val3, etc...)";
          SqlConnection conn = new SqlConnection("Your connection string");
          SqlCommand comm = new SqlCommand(query, conn);
          foreach (KeyValuePair<string, string> keyVal in param)
          {
              comm.Parameters.AddWithValue(keyVal.Key, keyVal.Value);
          }
          comm.ExecuteNonQuery();


当然,这对您不起作用.您必须对其进行定制以适合您的情况.但我希望您现在就明白.


Of course this will not work for you. You have to tailor it to suit your scenario. But I hope you get the idea now.


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

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