How can I change the values of access database cells programmatically, and not through a DataGridView? For example, there are several textboxes on the form, they are filled in by the user and after clicking on the button, the textboxes fields are saved in the access database
1 answer
I will give a simple example, click on the button - save it from the textbox :
private void button1_Click(object sender, EventArgs e) { using (OleDbConnection conn = new OleDbConnection(connectionString)) { try { conn.Open(); string query = "UPDATE [таблица] SET [имя_колонки] = ? WHERE id = ?"; var accessUpdateCommand = new OleDbCommand(query, conn); accessUpdateCommand.Parameters.AddWithValue("имя_колонки", textBox1.Text); // текст accessUpdateCommand.Parameters.AddWithValue("id", 1); // тут напишите свой ид da.UpdateCommand = accessUpdateCommand; da.UpdateCommand.ExecuteNonQuery(); conn.Close(); } catch (Exception ex) { // обработка исключения if (conn != null && conn.State == ConnectionState.Open) { conn.Close(); } } } } connectionString is a database connection string.
You can also without AddWithValue , just write directly to the line what you need instead of question marks. Pro line to make it clearer, for example:
int id = 1; string query = "UPDATE [что_обновляем] SET [имя_обновляемой_колонки] =" + textBox1.Text + " WHERE id = " + id; |
DataGridVieworTextBox, the code for working with the database is exactly the same in both cases. - Alexander Petrov