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

C# 使用C在变量中循环和存储来自SQL Server的数据#,c#,sql-server,loops,C#,Sql Server,Loops,我在SQL中有一个类似于下面的表 Code Month Budget Expense A10 9 100 10 A10 10 100 40 A10 11 100 40 A10 12 100 10 我希望遍历所有行,并使用以下变量存储每个集合: variable_budget=100; variable_month1 =10; variable_month2 =40; variable_month3 =40; variabl

我在SQL中有一个类似于下面的表

Code  Month Budget Expense 
A10   9     100    10
A10   10    100    40
A10   11    100    40 
A10   12    100    10
我希望遍历所有行,并使用以下变量存储每个集合:

variable_budget=100;
variable_month1 =10;
variable_month2 =40;
variable_month3 =40;
variable_month4 =10;
请注意,月份不是固定的,但可以是3个月或4个月,不能超过3个月

请帮助我找到实现这一目标的最佳策略


谢谢

您必须使用三个基本类来连接SQL和接收数据:
SqlConnection
SqlCommand
SqlDataReader
。请注意,它们是
IDisposable
,因此应该使用
using
:)另一种方法是使用一些ORM,比如实体框架。无论如何,第一种方法是:

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.
            }

    }
}
你试试这个

 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; }
    }
static void Main(字符串[]args)
{
列表响应=新列表();
添加(新FrmDBSet(){Code=“A10”,月=9,预算=100,费用=10});
添加(新FrmDBSet(){Code=“A10”,月=10,预算=100,费用=40});
添加(新FrmDBSet(){Code=“A10”,月=11,预算=100,费用=40});
添加(新FrmDBSet(){Code=“A10”,月=12,预算=100,费用=10});
var res=response.GroupBy(s=>s.Budget).ToDictionary(s=>s.Key,s=>s.ToList()).ToList();
最终值=新的最终值();
foreach(资源中的var项目)
{
finalvalue.Budget=项目.Key;
int指数=1;
foreach(在item.value中设置FRMDB值)
{
字符串propertyname=$“变量{index}”;
PropertyInfo property=finalvalue.GetType().GetProperty(propertyname);
if(属性!=null)
property.SetValue(最终值、值、费用);
索引++;
}
}
Console.ReadKey();
}
公开课决赛
{
公共整数预算{get;set;}
公共int变量_month1{get;set;}
公共int变量_month2{get;set;}
公共int变量_month3{get;set;}
公共int变量_month4{get;set;}
}
公共类FrmDBSet
{
公共字符串代码{get;set;}
公共整数月{get;set;}
公共整数预算{get;set;}
公共int费用{get;set;}
}