Skip to content

Commit 03cdb18

Browse files
committed
Add csv to create table
1 parent ea584fd commit 03cdb18

File tree

2 files changed

+177
-0
lines changed

2 files changed

+177
-0
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<Window x:Class="DataToolChain.CsvToCreateTable"
2+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4+
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
5+
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
6+
xmlns:local="clr-namespace:DataToolChain"
7+
mc:Ignorable="d"
8+
Title="CSV To SQL Create Table" Height="650" Width="850"
9+
d:DataContext="{d:DesignInstance Type=local:CsvToCreateTableViewModel, IsDesignTimeCreatable=True}"
10+
>
11+
<Grid>
12+
<Grid.RowDefinitions>
13+
<RowDefinition Height="Auto"/>
14+
<RowDefinition Height="Auto"/>
15+
<RowDefinition Height="*"/>
16+
</Grid.RowDefinitions>
17+
18+
19+
<Grid Margin="5">
20+
<Grid.ColumnDefinitions>
21+
<ColumnDefinition Width="Auto"/>
22+
<ColumnDefinition Width="*"/>
23+
<ColumnDefinition Width="Auto"/>
24+
</Grid.ColumnDefinitions>
25+
<Label VerticalContentAlignment="Center" Margin="5">Input Files:</Label>
26+
<DataGrid Grid.Column="1" Margin="5" VerticalContentAlignment="Center" Height="200" ItemsSource="{Binding Path=FilePaths, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
27+
<StackPanel Grid.Column="2" Orientation="Horizontal">
28+
<Button Margin="5,5,5,0" Content="..." HorizontalAlignment="Stretch" VerticalAlignment="Top" Width="25" Click="ButtonBrowse_OnClick" Height="25"/>
29+
<Button Margin="5" Content="Go" HorizontalAlignment="Stretch" VerticalAlignment="Top" Height="25" Width="86" Click="ButtonGoClick"/>
30+
</StackPanel>
31+
</Grid>
32+
33+
34+
<StackPanel Background="Beige" Grid.Row="1" Margin="5">
35+
<StackPanel Orientation="Horizontal">
36+
<Label Margin="5" VerticalContentAlignment="Center">Output Table Name:</Label>
37+
<TextBox HorizontalAlignment="Left" VerticalContentAlignment="Center" Margin="5" Height="24" TextWrapping="Wrap" Text="{Binding Path=TableName, UpdateSourceTrigger=PropertyChanged}" Width="409" />
38+
</StackPanel>
39+
<StackPanel Orientation="Horizontal">
40+
<Label Margin="5">Delimiter:</Label>
41+
<RadioButton Margin="5" IsChecked="{Binding Path=UseTabDelimiter}" GroupName="delimiter" VerticalContentAlignment="Center">Tab</RadioButton>
42+
<RadioButton Margin="5" VerticalContentAlignment="Center" IsChecked="{Binding Path=UseCommaDelimiter}" GroupName="delimiter">Comma</RadioButton>
43+
</StackPanel>
44+
<StackPanel Orientation="Horizontal">
45+
<Label Margin="5">Header offset rows:</Label>
46+
<TextBox HorizontalAlignment="Left" VerticalContentAlignment="Center" Margin="5" Height="24" TextWrapping="Wrap" Text="{Binding Path=HeaderOffsetRows, UpdateSourceTrigger=PropertyChanged}" Width="65" />
47+
<Label Margin="5"></Label>
48+
</StackPanel>
49+
</StackPanel>
50+
51+
<TextBox Grid.Row="2" Margin="5" TextWrapping="Wrap" Text="{Binding Path=OutputText, Mode=TwoWay}" AcceptsReturn="True" IsUndoEnabled="False" VerticalScrollBarVisibility="Auto" BorderThickness="2" BorderBrush="#FF193EAE" />
52+
</Grid>
53+
</Window>
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
using System;
2+
using System.Collections.ObjectModel;
3+
using System.ComponentModel;
4+
using System.IO;
5+
using System.Linq;
6+
using System.Runtime.CompilerServices;
7+
using System.Windows;
8+
using DataToolChain.Ui.Extensions;
9+
using Microsoft.Win32;
10+
11+
namespace DataToolChain
12+
{
13+
public class FilePathObject
14+
{
15+
public string FilePath { get; set; }
16+
}
17+
18+
public class CsvToCreateTableViewModel : INotifyPropertyChanged
19+
{
20+
private string _outputText;
21+
private ObservableCollection<FilePathObject> _filePaths = new ObservableCollection<FilePathObject>();
22+
private string _tableName;
23+
24+
public string OutputText
25+
{
26+
get { return _outputText; }
27+
set
28+
{
29+
_outputText = value;
30+
OnPropertyChanged();
31+
}
32+
}
33+
34+
public ObservableCollection<FilePathObject> FilePaths
35+
{
36+
get { return _filePaths; }
37+
set
38+
{
39+
_filePaths = value;
40+
OnPropertyChanged();
41+
}
42+
}
43+
44+
public string TableName
45+
{
46+
get { return _tableName; }
47+
set
48+
{
49+
_tableName = value;
50+
OnPropertyChanged();
51+
}
52+
}
53+
54+
public bool UseTabDelimiter { get; set; } = true;
55+
public bool UseCommaDelimiter { get; set; }
56+
public int HeaderOffsetRows { get; set; } = 1;
57+
58+
public void Go()
59+
{
60+
var filePaths = _filePaths.Select(p => p.FilePath).ToArray();
61+
var tableName = TableName;
62+
var csvDelimiter = UseTabDelimiter ? '\t' : ',';
63+
var headerOffsetRows = HeaderOffsetRows;
64+
65+
try
66+
{
67+
OutputText = TableSqlUploadHelpers.GetTableSql(filePaths, tableName, csvDelimiter, headerOffsetRows);
68+
}
69+
catch (Exception ex)
70+
{
71+
OutputText = ex.ConcatenateInners();
72+
}
73+
}
74+
75+
76+
public event PropertyChangedEventHandler PropertyChanged;
77+
78+
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
79+
{
80+
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
81+
}
82+
}
83+
84+
/// <summary>
85+
/// Interaction logic for MainWindow.xaml
86+
/// </summary>
87+
public partial class CsvToCreateTable : Window
88+
{
89+
public CsvToCreateTableViewModel _viewModel { get; set; } = new CsvToCreateTableViewModel();
90+
91+
92+
public
93+
CsvToCreateTable()
94+
{
95+
InitializeComponent();
96+
97+
DataContext = _viewModel;
98+
}
99+
100+
private void ButtonGoClick(object sender, RoutedEventArgs e)
101+
{
102+
try
103+
{
104+
_viewModel.Go();
105+
}
106+
catch (Exception ex)
107+
{
108+
ex.DisplayInners();
109+
}
110+
}
111+
112+
private void ButtonBrowse_OnClick(object sender, RoutedEventArgs e)
113+
{
114+
var d = new OpenFileDialog();
115+
d.Multiselect = true;
116+
117+
if (d.ShowDialog() == true)
118+
{
119+
_viewModel.FilePaths.AddRange(d.FileNames.Select(p => new FilePathObject(){FilePath = p}));
120+
_viewModel.TableName = Path.GetFileNameWithoutExtension(d.FileName);
121+
}
122+
}
123+
}
124+
}

0 commit comments

Comments
 (0)