我正在尝试创建一个简单的应用程序,将文件的标题行映射到DB中的表中。我创建了一个函数,它在逗号上拆分标题行,然后在文本框中显示它(每行一列)。然后我从数据库中提取所有列并执行相同的操作。我接下来要做的是一个automap,它只是检查文件中的列是否与db列完全匹配。如果是,则匹配它们并将它们放在中心文本框中,如下所示:Col1 : Col1
。我希望能够做的下一步是手动映射那些没有完全相同的名称。我的想法是能够选择最左侧文本框中的列(显示整行被选中)并对DB执行相同操作,一旦它们都有选择,它就会将其添加到中间匹配的文本框中。如何匹配标题中的列以将其与db?
XAML:
<Grid>
<Button x:Name="btnOpenFile" Padding="10" Click="btnOpenFile_Click" Content="Open file" Margin="30,8,594,356"/>
<TextBox Name="txtEditor" IsReadOnly="True" Margin="30,105,518,10" />
<Label Margin="30,68,649,319">Header from file</Label>
<TextBox x:Name="txtEditor_db" Margin="525,105,23,10" />
<Label Margin="525,68,72,319" Content="Column Mappings from db
"/>
<TextBox x:Name="txtEditor_db_Copy" Margin="307,167,295,93" />
</Grid>
代码背后: public partial class MainWindow:Window { List mappings = new List();
public MainWindow()
{
InitializeComponent();
}
private void btnOpenFile_Click(object sender, RoutedEventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog();
if (openFileDialog.ShowDialog() == true)
{
txtEditor.Text = string.Join(Environment.NewLine, File.ReadLines(openFileDialog.FileName).First().Replace(" ","").Split(','));
}
if(txtEditor.Text.Count() > 0)
{
using(LocalDataEntities db = new LocalDataEntities())
{
txtEditor_db.Text = string.Join(Environment.NewLine, db.Database.SqlQuery<string>("SELECT name FROM sys.columns WHERE object_id = OBJECT_ID('MyTable');").ToList());
}
}
TryAutoMapping();
RefreshMappingBox();
}
private void RefreshMappingBox()
{
foreach (HeaderToDBMapping map in mappings) {
txtEditor_db_Copy.Text += string.Format("{0} : {1}{2}", map.source, map.destination, Environment.NewLine);
}
}
private void TryAutoMapping()
{
List<string> columnsSource = txtEditor.Text.Split(new[] { "\r\n", "\r", "\n" }, StringSplitOptions.None).ToList();
List<string> columnsDB = txtEditor_db.Text.Split(new[] { "\r\n", "\r", "\n" }, StringSplitOptions.None).ToList();
foreach (string col in columnsSource)
{
foreach (string colDB in columnsDB)
{
if (col.Equals(colDB))
{
mappings.Add(new HeaderToDBMapping(col, colDB, true));
}
}
}
}
}
HeaderToDBMapping:
public class HeaderToDBMapping
{
public string source;
public string destination;
public bool autoMapped = false;
public HeaderToDBMapping(string source, string destination, bool autoMapped)
{
this.source = source;
this.destination = destination;
this.autoMapped = autoMapped;
}
}
答案 0 :(得分:1)
我不完全明白你在这里尝试做什么,但我建议你使用Snake::runThread
代替ListBox
,这样你就可以选择要手动映射的项目。