using System;
using System.Windows;
using System.Windows.Controls;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;
using MySql.Data;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using MySql.Data.MySqlClient;
namespace conntoSQLServer
{
public partial class MainWindow : Window
{
string connectionString;
MySqlDataAdapter adapter;
DataTable phonesTable;
public MainWindow()
{
InitializeComponent();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
connectionString = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
string sql = "SELECT id, ask, offer, status, name, company, phone, ctime, email, cdate FROM orders";
/* string sql = "SELECT o.id, o.ask, o.offer, o.status, o.name, o.company, o.phone, o.ctime, o.email, o.cdate, c.name as city, cl.name as class, rc.name as rclass FROM orders as o INNER JOIN cities as c ON (o.city = c.id) INNER JOIN classes AS cl ON (o.class=cl.id) INNER JOIN rclasses as rc ON (o.rclass=rc.id)"; */
// string sql2 = "UPDATE orders SET ask = @ask, offer = offer";
phonesTable = new DataTable();
MySqlConnection connection = null;
try
{
connection = new MySqlConnection(connectionString);
MySqlCommand command = new MySqlCommand(sql, connection);
adapter = new MySqlDataAdapter(command);
adapter.Fill(phonesTable);
phonesGrid.ItemsSource = phonesTable.DefaultView;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
if (connection != null)
connection.Close();
}
}
private void UpdateDB()
{
try
{
MySqlCommandBuilder comandbuilder = new MySqlCommandBuilder(adapter);
adapter.Update(phonesTable);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Information);
}
}
private void updateButton_Click(object sender, RoutedEventArgs e)
{
UpdateDB();
}
private void deleteButton_Click(object sender, RoutedEventArgs e)
{
if (phonesGrid.SelectedItems != null)
{
for (int i = 0; i < phonesGrid.SelectedItems.Count; i++)
{
DataRowView datarowView = phonesGrid.SelectedItems[i] as DataRowView;
if (datarowView != null)
{
DataRow dataRow = (DataRow)datarowView.Row;
dataRow.Delete();
}
}
}
UpdateDB();
}
}
}
<DataGrid AutoGenerateColumns="False" x:Name="phonesGrid">
<DataGrid.Columns>
<DataGridTextColumn Header="Спрос" Binding="{Binding ask}" FontFamily="Arial" Width="120"/>
<DataGridTextColumn Header="Предложение" Binding="{Binding offer}" Width="140"/>
<DataGridTextColumn Header="Статус" Binding="{Binding status}" Width="100"/>
<DataGridTextColumn Header="Имя-Отчество" Binding="{Binding name}" Width="140"/>
<DataGridTextColumn Header="Компания" Binding="{Binding company}" Width="120"/>
<DataGridTextColumn Header="Город" Binding="{Binding city}" Width="100"/>
<DataGridTextColumn Header="Род дей-сти" Binding="{Binding class}" Width="120"/>
<DataGridComboBoxColumn Header="Р" ItemsSource="{Binding rclass}" SelectedItemBinding="{Binding ask}" Width="50"/>
<DataGridTextColumn Header="Телефон" Binding="{Binding phone}" Width="120"/>
<DataGridTextColumn Header="Время" Binding="{Binding ctime}" Width="110"/>
<DataGridTextColumn Header="Емейл" Binding="{Binding email}" Width="140"/>
</DataGrid.Columns>
</DataGrid>
<StackPanel HorizontalAlignment="Center" Grid.Row="1" Orientation="Horizontal">
<Button x:Name="updateButton" Content="Обновить" Click="updateButton_Click" />
<Button x:Name="deleteButton" Content="Удалить" Click="deleteButton_Click" />
</StackPanel>