Преобразование таблиц в классы. Работа с LINQ в C#.
1. В обозревателе проекта нажимаем ПКМ - Добавить - Создать элемент
2. Выбираем в открывшемся окне Классы LINQ to SQL
3. Откроется конструктор Object Relational Designer.
4. Для создания объектов создадим сначала подключение к базе
5. Далее перетаскиваем названия таблиц из списка в окно конструктора
6. Пример запроса
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
using System.Windows; | |
using System.Windows.Controls; | |
using System.Windows.Data; | |
using System.Windows.Documents; | |
using System.Windows.Input; | |
using System.Windows.Media; | |
using System.Windows.Media.Imaging; | |
using System.Windows.Shapes; | |
namespace Furniture | |
{ | |
/// <summary> | |
/// Логика взаимодействия для LoginWindow.xaml | |
/// </summary> | |
public partial class LoginWindow : Window | |
{ | |
public LoginWindow() | |
{ | |
InitializeComponent(); | |
} | |
private void Button_Click(object sender, RoutedEventArgs e) | |
{ | |
DataClasses1DataContext db = new DataClasses1DataContext(); | |
List<Пользователь> result = (from a in db.Пользователь where ((a.Логин == LoginField.Text)&&(a.Пароль == PasswordField.Password)) select a).ToList(); | |
if (result.Count == 0) { MessageBox.Show("Такого пользователя не существует"); } else MessageBox.Show(result[0].Роль); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
using System.Windows; | |
using System.Windows.Controls; | |
using System.Windows.Data; | |
using System.Windows.Documents; | |
using System.Windows.Input; | |
using System.Windows.Media; | |
using System.Windows.Media.Imaging; | |
using System.Windows.Shapes; | |
namespace Furniture | |
{ | |
/// <summary> | |
/// Логика взаимодействия для RegistrationWindow.xaml | |
/// </summary> | |
public partial class RegistrationWindow : Window | |
{ | |
public RegistrationWindow() | |
{ | |
InitializeComponent(); | |
} | |
private void Button_Click(object sender, RoutedEventArgs e) | |
{ | |
DataClasses1DataContext db = new DataClasses1DataContext(); | |
if ((LoginField.Text.Trim().Length < 1) || ((NameField.Text.Trim().Length < 1)) || ((PasswordField.Password.Trim().Length < 1))) { | |
MessageBox.Show("Заполните все поля"); | |
return; | |
} | |
List<Пользователь> result = (from a in db.Пользователь where (a.Логин == LoginField.Text) select a).ToList(); | |
if (result.Count != 0) | |
{ | |
MessageBox.Show("Пользователь с таким логином уже существует"); | |
return; | |
} | |
Пользователь user = new Пользователь | |
{ | |
Логин = LoginField.Text, | |
Наименование = NameField.Text, | |
Пароль = PasswordField.Password, | |
Роль = "Заказчик" | |
}; | |
db.Пользователь.InsertOnSubmit(user); | |
try | |
{ | |
db.SubmitChanges(); | |
} | |
catch (Exception err) { | |
MessageBox.Show("Произошла ошибка сервера"); | |
return; | |
} | |
MessageBox.Show("Вы успешно зарегистрировались"); | |
} | |
} | |
} |
Комментарии
Отправить комментарий