БД
using System.Data.SqlClient;
string connectionString = @"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename='C:\Users\Andreo.Andreo-PC\documents\visual studio 2015\Projects\WindowsFormsApplication9\WindowsFormsApplication9\Database1.mdf';Integrated Security=True";
// Прочитати
private void button1_Click(object sender, EventArgs e)
{
richTextBox1.Text = "";
string sqlExpression = "SELECT * FROM workers";
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
SqlCommand command = new SqlCommand(sqlExpression, connection);
using (SqlDataReader reader = command.ExecuteReader())
{
while (reader.Read())
{
for (int i = 0; i < reader.FieldCount; i++)
{
richTextBox1.Text += reader.GetValue(i) + " ";
}
richTextBox1.Text += "\n";
}
}
}
}
// Додати
private void button2_Click(object sender, EventArgs e)
{
string sqlExpression = "INSERT INTO workers (Name) VALUES ('" + textBox1.Text + "')";
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
SqlCommand command = new SqlCommand(sqlExpression, connection);
command.ExecuteNonQuery();
command.Dispose();
connection.Close();
}
MessageBox.Show("Дані успішно записано!");
}