Created
March 20, 2019 13:38
-
-
Save GeorgiyDemo/18dab00ed4b385e76941c98e8e86acfa to your computer and use it in GitHub Desktop.
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
//Подключить namespace | |
using System.Data.OleDb; | |
//Добавить поля в класс | |
public static string connectString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=../../../Exam.mdb;"; | |
private OleDbConnection myConnection; | |
//Конструктор формы | |
public MainForm() | |
{ | |
InitializeComponent(); | |
//Новое соединение | |
myConnection = new OleDbConnection(connectString); | |
myConnection.Open(); | |
} | |
//SQL-запросы и Access | |
//Один элемент | |
private void OneElementButton_Click(object sender, EventArgs e) | |
{ | |
string query = "SELECT user FROM data WHERE Код=1"; | |
OleDbCommand command = new OleDbCommand(query, myConnection); | |
textBox1.Text = command.ExecuteScalar().ToString(); | |
} | |
//Несколько элементов | |
private void MultiElementsButton_Click(object sender, EventArgs e) | |
{ | |
string query = "SELECT * FROM data WHERE user=2"; | |
OleDbCommand command = new OleDbCommand(query, myConnection); | |
OleDbDataReader reader = command.ExecuteReader(); | |
listBox1.Items.Clear(); | |
while (reader.Read()) | |
listBox1.Items.Add(reader[0].ToString() + " " + reader[1].ToString() + " " + reader[2].ToString() + " "); | |
reader.Close(); | |
} | |
//Для команд без возврата значения (INSERT/DELETE) | |
private void InsertElementButton_Click(object sender, EventArgs e) | |
{ | |
string query = "INSERT INTO data (user, active, passive) VALUES ("+UserTextBox.Text+", '"+ActiveTextBox.Text+"', '"+PassiveTextBox.Text+"')"; | |
OleDbCommand command = new OleDbCommand(query, myConnection); | |
command.ExecuteNonQuery(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment