Answer the question
In order to leave comments, you need to log in
How to update/delete/insert data into the database?
Good afternoon everyone. Implemented a connection to the database and output to the DataGridView. In this form:
private void Form1_Load(object sender, EventArgs e)
{
// **Шаг 1: открываем соединение с базой данных DBNAME на сервере SQL с именем DBMSName**
SqlConnection cn = new SqlConnection("Data Source=DBMSName;Initial Catalog=DBNAME;Persist Security Info=True;User ID=sa;Password=123");
cn.Open();
// **Шаг 2: Создаем SqlDataAdapter при помощи команды SELECT**
string strQuery;
strQuery = "SELECT * FROM table1";
SqlDataAdapter dAdapt = new SqlDataAdapter(strQuery, cn);
// **Шаг З: Создаем и заполняем объект DataSet, а потом закрываем соединение**
DataSet ds = new DataSet(); //создание DataSet (данные БД в памяти)
DataTable dt = new DataTable(); //создание DataTable (таблица из БД в памяти)
BindingSource bs = new BindingSource(); //
try
{
dAdapt.Fill(ds, "TableName");
dt = ds.Tables["TableName"];
bs.DataSource = dt;
DataGridView1.DataSource = bs;
bindingNavigator1.BindingSource = bs;
}
catch (Exception ex)
{
//вывод ошибки
MessageBox.Show(ex.Message);
}
finally
{
//закрытие подключения
cn.Close();
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question