using Haftmittel.Properties; using Microsoft.Data.SqlClient; using System; using System.Collections.ObjectModel; using System.Data.Odbc; using System.Globalization; using System.IO; using System.Linq; using System.Threading; using System.Windows; using System.Windows.Controls; using System.Windows.Input; using System.Windows.Markup; namespace Haftmittel { /// /// Interaction logic for MainWindow.xaml /// public partial class MainWindow : Window { private int tow = 0; private int ope = 0; private string connstr = string.Empty; public MainWindow() { try { string[] cmd = Environment.GetCommandLineArgs(); if (cmd.Count() < 2) { MessageBox.Show("Za mało parametrów"); this.Close(); return; } if (OdczytajPlik(cmd[1])) { if (string.IsNullOrWhiteSpace(connstr)) { connstr = Settings.Default.ConnectionString; } else { connstr = PrzerobConnectionString(connstr); } DataContext = new ClassHaftmittel(tow, ope, connstr); } else { this.Close(); } } catch (Exception ex) { MessageBox.Show(ex.Message); } Thread.CurrentThread.CurrentUICulture = new CultureInfo(Settings.Default.Language); this.Language = XmlLanguage.GetLanguage(Thread.CurrentThread.CurrentUICulture.Name); try { InitializeComponent(); } catch (Exception ex) { MessageBox.Show(ex.Message); } } /// /// odczytuje plik CSV wysłany z XLa /// /// private bool OdczytajPlik(string nazwa) { try { StreamReader reader = File.OpenText(nazwa); string line; var sep = CultureInfo.CurrentCulture.TextInfo.ListSeparator; while ((line = reader.ReadLine()) != null) { string[] items = line.Split(sep.ToCharArray()); // pierwszy parametr to GID towaru, drugi operatora tow = Convert.ToInt32(items[1]); ope = Convert.ToInt32(items[2]); for (int licz = 3; licz < items.Length - 1; licz++) { if (!string.IsNullOrWhiteSpace(items[licz])) { connstr += @items[licz] + ";"; } } connstr = connstr.Replace("\"", ""); } return true; } catch (Exception) { MessageBox.Show(string.Format("Problemy z otwarciem lub zawartością pliku {0}", nazwa)); return false; } } private void Image_MouseDown_1(object sender, MouseButtonEventArgs e) { // (DataContext as ClassHaftmittel).SDS_Obrazek1Change(); if ((sender as Image).Opacity == 1) { (sender as Image).Opacity = 0.2; } else { (sender as Image).Opacity = 1; } } private void Button_Click(object sender, RoutedEventArgs e) { this.Close(); } private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e) { if (DataContext != null) { if ((DataContext as ClassHaftmittel).IsChanged) { if (MessageBox.Show("Niezapisane dane. Zakończyć?", "Koniec", MessageBoxButton.YesNo) == MessageBoxResult.Yes) { e.Cancel = false; } else { e.Cancel = true; } } } } private void ComboBox_KeyDown(object sender, KeyEventArgs e) { ObservableCollection lc = ((sender as ComboBox).ItemsSource as ObservableCollection); ClassSkladnik c = lc.Where(x => x.Kod.ToUpper().StartsWith(e.Key.ToString().Last().ToString())).FirstOrDefault(); if (c != null) { (sender as ComboBox).SelectedItem = c; } } private string PrzerobConnectionString(string connstr) { if (!string.IsNullOrWhiteSpace(connstr)) { var odbc = new OdbcConnectionStringBuilder(connstr); var sql = new SqlConnectionStringBuilder() { DataSource = odbc["server"].ToString(), InitialCatalog = odbc["database"].ToString(), UserID = odbc["uid"].ToString(), Password = odbc["pwd"].ToString(), PersistSecurityInfo = (odbc["trusted_connection"].ToString() == "true") }; return sql.ConnectionString; } else { throw new Exception("Problem z dostępem do bazy"); } } } }