软糖来回答罗。
我这里有个SQL连接数据库的示例,可以参考下。
public void 数据集测试(){
//(1.连接)建立到数据库的 连接 conn
SqlConnection conn = new SqlConnection()
conn.ConnectionString = @"Data Source = (LocalDB)\MSSQLLocalDB AttachDbFilename = J:\数据库\SQLDB\NewDB.mdf Integrated Security = True Connect Timeout = 5"
//(2.打开)
conn.Open()
string 表名 = "Users"
using (conn)
{
//(3.适配)适配器 da 和 数据集 ds
da = new SqlDataAdapter()
ds = new DataSet("DataSet1")
// 将DataTable添加到DataSet
ds.Tables.Add(dt)
// 将DataSet的修改提交至“数据库”
SqlCommandBuilder mySqlCommandBuilder = new SqlCommandBuilder(da)
da.Update(ds, dt.TableName)
}
//(6.释放资源)
conn.Close()
conn.Dispose()
//注意:在修改、删除等 *** 作中表必须定义主键,select的字段也必须包含主键,
//否则会抛出异常“对于不返回任何键列信息的 SelectCommand,不支持 UpdateCommand 的动态 SQL 生成。”
}
上面的dt变量就是你的表,需要给它指定一个TableName名字。
连接字符串需要改成你数据库的,在服务器资源管理器>数据连接>属性页面>连接字符串
(鼠标选中然后按Ctrl+C复制)
如满意,请采纳,谢谢。
假设你上图表格数据为 DataTable dt,列分别为AA,BB,CC,DDvar query = from p in dt.AsEnumerable()
//分组
group p by new
{
t1 = p.Field<int>("AA").ToString().Substring(0, p.Field<int>("AA").ToString().Length - 2),
t2 = p.Field<double>("BB"),
t3 = p.Field<string>("CC")
} into g
select new
{
ColA = g.Key.t1,
ColB = g.Key.t2,
ColC = g.Key.t3,
ColD = g.Sum(c =>c.Field<int>("DD"))
}
//转datatable
DataTable dtNew = dt.Clone()
DataRow drNew
foreach (var p in query)
{
drNew = dtNew.NewRow()
drNew["AA"] = p.ColA
drNew["BB"] = p.ColB
drNew["CC"] = p.ColC
drNew["DD"] = p.ColD
dtNew.Rows.Add(drNew)
}
dtNew 为最终结果
注:转换过程中要考虑空值的情况
class Program{
static void Main(string[] args)
{ //建立课程内存表
DataTable table1 = new DataTable()
table1.Columns.Add("CourseCode", Type.GetType("System.String"))
table1.Columns.Add("CourseName", Type.GetType("System.String"))
table1.Rows.Add(new object[2] { "A", "语文" })
table1.Rows.Add(new object[2] { "B", "数学" })
table1.Rows.Add(new object[2] { "C", "英语" })
ShowTable(table1)
//建立学生内存表
DataTable table2 = new DataTable()
table2.Columns.Add("StudentCode", Type.GetType("System.String"))
table2.Columns.Add("StudentName", Type.GetType("System.String"))
table2.Rows.Add(new object[2]{"1","张三"})
table2.Rows.Add(new object[2] { "2", "李四" })
table2.Rows.Add(new object[2] { "3", "王五" })
ShowTable(table2)
//建立学生成绩表
DataTable table4 = new DataTable()
table4.Columns.Add("StudentCode", Type.GetType("System.String"))
table4.Columns.Add("CourseCode", Type.GetType("System.String"))
table4.Columns.Add("Grade", Type.GetType("System.Int32"))
table4.Rows.Add(new object[3] { "1", "A", 78 })
table4.Rows.Add(new object[3] { "1", "B", 90 })
table4.Rows.Add(new object[3] { "1", "C", 67 })
table4.Rows.Add(new object[3] { "2", "B", 78 })
table4.Rows.Add(new object[3] { "2", "C", 90 })
table4.Rows.Add(new object[3] { "3", "A", 88 })
table4.Rows.Add(new object[3] { "3", "C", 67 })
ShowTable(table4)
//最终统计表
DataTable table5 = new DataTable()
DataColumn col = table5.Columns.Add("StudentCode", Type.GetType("System.String"))
table5.Columns.Add("StudentName", Type.GetType("System.String"))
foreach (DataRow r in table1.Rows)
{
table5.Columns.Add(r["CourseCode"].ToString(), Type.GetType("System.Int32"))
}
table5.PrimaryKey =new DataColumn[1]{table5.Columns[0]}
table2.PrimaryKey = new DataColumn[1] { table2.Columns[0] }
foreach (DataRow row in table4.Rows)
{
DataRow dRow = table5.Rows.Find(row["StudentCode"])
if (dRow == null)
{
dRow = table5.NewRow()
dRow["StudentCode"] = row["StudentCode"]
DataRow fRow = table2.Rows.Find(row["StudentCode"])
dRow["StudentName"] = (fRow != null) ? fRow["StudentName"] : null
table5.Rows.Add(dRow)
}
dRow[row["CourseCode"].ToString()] = row["Grade"]
table5.AcceptChanges()
}
ShowTable(table5)
Console.ReadLine()
}
static void ShowTable(DataTable table)
{
Console.WriteLine(table.TableName)
Console.Write("\t")
foreach (DataColumn col in table.Columns)
{
Console.Write(string.Format("{0},", col.ColumnName))
}
Console.WriteLine()
foreach (DataRow row in table.Rows)
{
Console.Write("\t")
foreach (DataColumn col in table.Columns)
{
Console.Write(string.Format("{0},", row[col.ColumnName]))
}
Console.WriteLine()
}
}
}
//运行结果
CourseCode,CourseName,
A,语文,
B,数学,
C,英语,
StudentCode,StudentName,
1,张三,
2,李四,
3,王五,
StudentCode,CourseCode,Grade,
1,A,78,
1,B,90,
1,C,67,
2,B,78,
2,C,90,
3,A,88,
3,C,67,
StudentCode,StudentName,A,B,C,
1,张三,78,90,67,
2,李四,,78,90,
3,王五,88,,67,
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)