C# 如何使用SQL表数据填充字典

C# 如何使用SQL表数据填充字典,c#,sql,oracle,dictionary,C#,Sql,Oracle,Dictionary,在我的C#项目中,我有一个字典,它的值当前是静态的 var dict = new Dictionary<string, string> { ["0000"] = "UK", ["1111"] = "JAPAN", ["2222"] = "CHINA", ["3333"] = "SRI LANKA", ["4444"] = "AUSI", ["5555"] = "USA", }; 如何将此表的值设置到字典中?提前感谢。使用oracle

在我的C#项目中,我有一个字典,它的值当前是静态的

var dict = new Dictionary<string, string>
{
    ["0000"] = "UK",
    ["1111"] = "JAPAN",
    ["2222"] = "CHINA",
    ["3333"] = "SRI LANKA",
    ["4444"] = "AUSI",
    ["5555"] = "USA",
};

如何将此表的值设置到字典中?提前感谢。

使用oracle dataadapter将数据放入数据表(请参阅)。然后使用下面的代码。您不需要像我那样将数据放入datatable。只是将数据添加到表中,以测试创建字典的代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            DataTable  dt = new DataTable();
            dt.Columns.Add("ID", typeof(int));
            dt.Columns.Add("CID", typeof(string));
            dt.Columns.Add("CountryName", typeof(string));

            dt.Rows.Add(new object[] {1, "0000", "UK"});
            dt.Rows.Add(new object[] {2, "1111", "JAPAN"});
            dt.Rows.Add(new object[] {3, "2222", "CHINA"});
            dt.Rows.Add(new object[] {4, "3333", "SRI LANKA"});
            dt.Rows.Add(new object[] {5, "4444", "AUSI"});
            dt.Rows.Add(new object[] {6, "5555", "USA"});

            Dictionary<string, string> dict = dt.AsEnumerable()
                .GroupBy(x => x.Field<string>("CID"), y => y.Field<string>("CountryName"))
                .ToDictionary(x => x.Key, y => y.FirstOrDefault());
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用系统数据;
命名空间控制台应用程序1
{
班级计划
{
静态void Main(字符串[]参数)
{
DataTable dt=新的DataTable();
添加(“ID”,typeof(int));
添加(“CID”,类型(字符串));
添加(“CountryName”,typeof(string));
Add(新对象[]{1,“0000”,“UK”});
Add(新对象[]{2,“1111”,“JAPAN”});
添加(新对象[]{3,“2222”,“中国”});
添加(新对象[]{4,“3333”,“斯里兰卡”});
Add(新对象[]{5,“4444”,“AUSI”});
添加(新对象[]{6,“5555”,“USA”});
Dictionary dict=dt.AsEnumerable()
.GroupBy(x=>x.Field(“CID”),y=>y.Field(“CountryName”))
.ToDictionary(x=>x.Key,y=>y.FirstOrDefault());
}
}
}

如果您使用的是Oracle 12.2,则可以使用以下命令执行返回json字符串的查询:

如果您的数据为json格式,则可以使用以下方法将其转换为字典:

string json=@“{”0000“:”英国“,”1111“:”日本“}”;
var dic=JsonConvert.DeserializeObject(json);

提前感谢。我会试着提前告诉你谢谢。我会尽力让你知道的,先生
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            DataTable  dt = new DataTable();
            dt.Columns.Add("ID", typeof(int));
            dt.Columns.Add("CID", typeof(string));
            dt.Columns.Add("CountryName", typeof(string));

            dt.Rows.Add(new object[] {1, "0000", "UK"});
            dt.Rows.Add(new object[] {2, "1111", "JAPAN"});
            dt.Rows.Add(new object[] {3, "2222", "CHINA"});
            dt.Rows.Add(new object[] {4, "3333", "SRI LANKA"});
            dt.Rows.Add(new object[] {5, "4444", "AUSI"});
            dt.Rows.Add(new object[] {6, "5555", "USA"});

            Dictionary<string, string> dict = dt.AsEnumerable()
                .GroupBy(x => x.Field<string>("CID"), y => y.Field<string>("CountryName"))
                .ToDictionary(x => x.Key, y => y.FirstOrDefault());
        }
    }
}
select JSON_ARRAY (cid, countryname) from country
string json = @"{""0000"":""UK"",""1111"":""Japan""}";

var dic = JsonConvert.DeserializeObject<Dictionary<string, string>>(json);