简体   繁体   English

使用 C# 在变量中循环和存储来自 SQL Server 的数据

[英]Looping and storing data from SQL Server in variables using C#

I have a table similar to following in SQL我有一个类似于 SQL 中的表

Code  Month Budget Expense 
A10   9     100    10
A10   10    100    40
A10   11    100    40 
A10   12    100    10

I would like to iterate through all the rows and store each set with the following variables:我想遍历所有行并使用以下变量存储每个集合:

variable_budget=100;
variable_month1 =10;
variable_month2 =40;
variable_month3 =40;
variable_month4 =10;

Please note that the month is not fixed but it can be 3 or 4 not more.请注意,月份不是固定的,但可以是 3 或 4,而不是更多。

Please help me on a best strategy to achieve it.请帮助我制定实现它的最佳策略。

Thank you谢谢

You have to use three basic classes to connect with SQL and receive data: SqlConnection , SqlCommand and SqlDataReader .您必须使用三个基本类来连接 SQL 并接收数据: SqlConnectionSqlCommandSqlDataReader Note that they are IDisposable , so using should be used :) Another way would be to use some ORM, like Entity Framework.请注意,它们是IDisposable ,因此应该使用using :) 另一种方法是使用一些 ORM,例如实体框架。 Anyway, the first approach would be:无论如何,第一种方法是:

using (SqlConnection conn = new SqlConnection(MainData.ConnStr))
{
    conn.Open();
    using (SqlCommand com = new SqlCommand("select * from MY_TABLE", conn))
    using (SqlDataReader reader = com.ExecuteReader())
    {
        // check if query returned any rows, if so, loop through them
        if (reader.HasRows)
            while (reader.Read())
            {
                 //here you can do operations on table rows, like assigning to variables etc.
            }

    }
}

You try this,你试试这个,

 static void Main(string[] args)
                {
                    List<FrmDBSet> response = new List<FrmDBSet>();
                    response.Add(new FrmDBSet() {Code ="A10", Month=9,Budget=100,Expense=10 });
                    response.Add(new FrmDBSet() { Code = "A10", Month = 10, Budget = 100, Expense = 40 });
                    response.Add(new FrmDBSet() { Code = "A10", Month = 11, Budget = 100, Expense = 40 });
                    response.Add(new FrmDBSet() { Code = "A10", Month = 12, Budget = 100, Expense = 10 });

                    var res = response.GroupBy(s => s.Budget).ToDictionary(s => s.Key, s => s.ToList()).ToList();
                    FinalCls finalvalue = new FinalCls();

                    foreach (var item in res)
                    {
                        finalvalue.Budget = item.Key;              
                        int index = 1;
                        foreach (FrmDBSet value in item.Value)
                        {
                            string propertyname = $"variable_month{index}";
                            PropertyInfo property = finalvalue.GetType().GetProperty(propertyname);
                            if(property!=null)
                            property.SetValue(finalvalue, value.Expense);
                            index++;
                        }
                    }

                    Console.ReadKey();
                }

public class FinalCls
    {
        public int Budget { get; set; }
        public int variable_month1 { get; set; }
        public int variable_month2 { get; set; }
        public int variable_month3 { get; set; }
        public int variable_month4 { get; set; }
    }


    public class FrmDBSet
    {
        public string Code { get; set; }
        public int Month { get; set; }
        public int Budget { get; set; }
        public int Expense { get; set; }
    }

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

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