【发布时间】:2019-11-09 15:17:15
【问题描述】:
我正在尝试将两个导入的 CSV 文件合并并连接到一个数据表中。 C# 对我来说是新的,但我以前在 PS 和 KSH 中做过很多次类似的事情。
数据加载正常,并像两个单独的表一样输出到控制台,但我不知道如何使用主键(wID,UserID)作为连接条件来连接每个 ShowTable() 的结果。
我尝试return Table_SalesUserData; and return Table_WifiUserData; 给出[ 'Form1.CreateTableWifi()' returns void, a return keyword must not be followed by an object expression ] 错误。
我也尝试使用:DataSet setSalesWifi = new DataSet(); 但在Button1_Click() 中会引发[ System.ArgumentNullException: ''column' argument cannot be null. Parameter name: column' ] 错误。
我认为这是因为每个的 .Columns 超出了范围。
表的创建如下所示。它们由 GenericParser 加载(代码工作正常,很长,所以没有发布)它们是 InsertTableWifi(string srcFilePathWifi) 和 InsertTableSales(string srcFilePathSales)。
我想我可以在合并数据工作时输出它,只是不知道如何将它们加入到新的 DataTable 或 DataSet 中。
有没有一种简单的方法可以将数据与我拥有的数据合并?
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Text.RegularExpressions;
using GenericParsing;
namespace GenericParserv1._1._6
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
DataTable Table_SalesUserData = new DataTable("SalesUserData ");
DataTable Table_WifiUserData = new DataTable("WifiUserData ");
DataSet setSalesWifi = new DataSet();
public void CreateTableSales()
{
DataColumn[] colsSales ={
new DataColumn("UserID",typeof(String)),
new DataColumn("Alias",typeof(String)),
new DataColumn("UserType",typeof(String)),
new DataColumn("AccountStatus",typeof(String)),
new DataColumn("ChgPasswdNxtLogin",typeof(String)),
};
Table_SalesUserData.Columns.AddRange(colsSales);
Table_SalesUserData.PrimaryKey = new DataColumn[] { Table_SalesUserData.Columns["UserID"] };
setSalesWifi.Tables.Add(Table_SalesUserData);
//return Table_SalesUserData ;
}
public void CreateTableWifi()
{
DataColumn[] colsWifi ={
new DataColumn("wID",typeof(String)),
new DataColumn("username",typeof(String)),
new DataColumn("fname",typeof(String)),
new DataColumn("lastname",typeof(String)),
new DataColumn("email",typeof(String)),
new DataColumn("empid",typeof(String)),
new DataColumn("lastlogintime",typeof(String))
};
Table_WifiUserData.Columns.AddRange(colsWifi);
Table_WifiUserData.PrimaryKey = new DataColumn[] { Table_WifiUserData.Columns["wID"] };
setSalesWifi.Tables.Add(Table_WifiUserData);
//return Table_WifiUserData ;
}
public void CreateTableReport()
{
DataColumn[] colsMerge ={
new DataColumn("wID",typeof(String)),
new DataColumn("username",typeof(String)),
new DataColumn("fname",typeof(String)),
new DataColumn("lastname",typeof(String)),
new DataColumn("email",typeof(String)),
new DataColumn("empid",typeof(String)),
new DataColumn("lastlogintime",typeof(String)),
new DataColumn("UserID",typeof(String)),
new DataColumn("Alias",typeof(String)),
new DataColumn("UserType",typeof(String)),
new DataColumn("AccountStatus",typeof(String)),
new DataColumn("ChgPasswdNxtLogin",typeof(String)),
};
Table_MergeUserData.Columns.AddRange(colsMerge);
Table_MergeUserData.PrimaryKey = new DataColumn[] { Table_MergeUserData.Columns["wID"] };
}
private static void ShowTable(DataTable table)
{
foreach (DataColumn col in table.Columns)
{
Console.Write("{0,-14}", col.ColumnName);
}
Console.WriteLine();
foreach (DataRow row in table.Rows)
{
foreach (DataColumn col in table.Columns)
{
if (col.DataType.Equals(typeof(DateTime)))
Console.Write("{0,-14:d}", row[col]);
else if (col.DataType.Equals(typeof(Decimal)))
Console.Write("{0,-14:C}", row[col]);
else
Console.Write("{0,-14}", row[col]);
}
Console.WriteLine();
}
Console.WriteLine();
}
private void Button1_Click(object sender, EventArgs e)
{
string srcFilePathWifi = @"c:\WifiUserData.csv";
string srcFilePathSales = @"c:\SalesUserData.csv";
CreateTableSales();
InsertTableSales(srcFilePathSales);
CreateTableWifi();
InsertTableWifi(srcFilePathWifi);
ShowTable(Table_WifiUserData);
ShowTable(Table_SalesUserData);
// Set the relations between the tables and create the related constraint.
setSalesWifi.Relations.Add("Table_WifiUserData", Table_SalesUserData.Columns["wID"], Table_SalesUserData.Columns["UserID"], true);
}
}
}
WifiUserData.csv:
wID username fname lastname email empid lastlogintime
msmith marysmith mary smith marysmith@company.com10001 24/01/2019 14:00
jbloggs joebloggs joe bloggs joebloggs@company.com10002 10/01/2019 9:00
pgolightly petergolightlypeter golightly petergolightly@company.com10003 20/01/2019 17:00
rrabbit rogerrabbit roger rabbit rogerrabbit@company.com10004 1/02/2019 14:00
SalesUserData.csv:
UserID Alias UserType AccountStatus ChgPasswdNxtLogin
msmith mary smith Manager No No
jbloggs joe bloggs Standard No No
pgolightly peter golightlyJunior No No
rrabbit roger rabbit Standard No No
我的目标:
UserID Alias UserType AccountStatus ChgPasswdNxtLogin username fname lastname email empid lastlogintime
msmith mary smith Manager No No marysmith mary smith marysmith@company.com 10001 24/01/2019 14:00
jbloggs joe bloggs Standard No No joebloggs joe bloggs joebloggs@company.com 10002 10/01/2019 9:00
pgolightly peter golightly Junior No No petergolightly peter golightly petergolightly@company.com 10003 20/01/2019 17:00
rrabbit roger rabbit Standard No No rogerrabbit roger rabbit rogerrabbit@company.com 10004 1/02/2019 14:00
【问题讨论】:
标签: c# csv join merge datatable