public static Dictionary<string, object> LonView()
{
string sql = "SELECT Marks,Id FROM table4";
using (SqlConnection Connection = new SqlConnection((@"DataSource")))
{
using (SqlCommand myCommand = new SqlCommand(sql, Connection))
{
Connection.Open();
using (SqlDataReader myReader = myCommand.ExecuteReader())
{
DataTable dt = new DataTable();
dt.Load(myReader);
Connection.Close();
Dictionary<string, object> lst = new Dictionary<string, object>();
var tableEnumerable = dt.AsEnumerable();
var tableArray = tableEnumerable.ToArray();
foreach (var data in tableArray)
{
lst.Add(data["id"].ToString(),data["Marks"] as string);
}
return lst;
}
}
}
}
我想将我的数据表列转换为字典并返回数组中的字典{[“A”,30],[“B”,32],[“C”,45]} form.Suggest一些想法做我的任务。
答案 0 :(得分:0)
看一下这个例子:
DataTable table = new DataTable();
table.Columns.Add("Id", typeof(string));
table.Columns.Add("Marks", typeof(int));
table.Rows.Add(new object[] { "A", 50 });
table.Rows.Add(new object[] { "B", 100 });
table.Rows.Add(new object[] { "C", 200 });
Dictionary<string, int> dict = new Dictionary<string, int>();
foreach (DataRow row in table.Rows)
{
dict.Add(row[0].ToString(), Convert.ToInt32(row[1].ToString()));
}
var arr = dict.ToArray();
这对你有帮助吗?
请确保您的查询不会返回重复的ID值(字典中的键),因为您将获得例外。
答案 1 :(得分:0)
您不必创建任何DataTable
,临时Dictionary
:简单的直接while
循环即可
string sql =
@"SELECT Marks,
Id
FROM table4";
using (SqlConnection Connection = new SqlConnection((@"DataSource"))) {
using (SqlCommand myCommand = new SqlCommand(sql, Connection)) {
Connection.Open();
// We don't know the final array's length; let's use a list as a buffer
List<string[]> buffer = new List<string[]>();
using (SqlDataReader myReader = myCommand.ExecuteReader()) {
while (myReader.Read())
buffer.Add(new string[] {
Convert.ToString(myReader["id"]),
Convert.ToString(myReader["Marks"]),
});
}
return buffer.ToArray();
}
}
修改:如果您想为某些目的保留DataTable
using (SqlConnection Connection = new SqlConnection((@"DataSource"))) {
using (SqlCommand myCommand = new SqlCommand(sql, Connection)) {
Connection.Open();
using (SqlDataReader myReader = myCommand.ExecuteReader()) {
DataTable dt = new DataTable();
dt.Load(myReader);
// You can obtain the string[][] array via Linq:
string[][] result = dt
.AsEnumerable()
.Select(record => new string[] {
Convert.ToString(record["id"]),
Convert.ToString(record["Marks"]), })
.ToArray();
...
}
...
答案 2 :(得分:0)
试试这个例子
dt.AsEnumerable()
.ToDictionary<DataRow, string, object>(row => row.Field<string>(0),
row => row.Field<object>(1));
这会对你有帮助。
答案 3 :(得分:0)
非常简单的要求:
public static Dictionary<string, object> LonView()
{
string sql = "SELECT Marks,Id FROM table4";
Dictionary<string, object> lst = null;
using (SqlConnection Connection = new SqlConnection((@"DataSource")))
{
using (SqlDataAdapter adapter = new SqlDataAdapter(sql, Connection))
{
DataTable dt = new DataTable();
adapter.Fill(dt);
Connection.Close();
lst = dt.AsEnumerable()
.GroupBy(x => x.Field<string>("Marks"), y => y.Field<object>("Id"))
.ToDictionary(x => x.Key, y => y.FirstOrDefault());
}
}
return lst;
}
如果数据库中的值是DateTime
public static Dictionary<string, DateTime> LonView()
{
string sql = "SELECT Marks,Id FROM table4";
Dictionary<string, DateTime> lst = null;
using (SqlConnection Connection = new SqlConnection((@"DataSource")))
{
using (SqlDataAdapter adapter = new SqlDataAdapter(sql, Connection))
{
DataTable dt = new DataTable();
adapter.Fill(dt);
Connection.Close();
lst = dt.AsEnumerable()
.GroupBy(x => x.Field<string>("Marks"), y => y.Field<DateTime>("Id"))
.ToDictionary(x => x.Key, y => y.FirstOrDefault());
}
}
return lst;
}