Alex Zhukovskiy Alex Zhukovskiy - 29 days ago 7
SQL Question

Submitting Changes pure SqlCommand

I need to get 2-way binding DB to DataGrid, so I'm using this method:

private void SetTable(string tableName)
{
var dataGridView1 = new DataGridView { DataSource = GetData(tableName), Dock = DockStyle.Fill };
groupBox1.Text = tableName;
groupBox1.Controls.Clear();
groupBox1.Controls.Add(dataGridView1);
}

private static DataTable GetData(string tableName)
{
using (var connection = new SqlConnection(ConnectionString))
{
var command = new SqlCommand(string.Format("SELECT * FROM {0}", tableName), connection);

connection.Open();
var adapter = new SqlDataAdapter(command);
var result = new DataTable();
adapter.Fill(result);
return result;
}
}

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
SetTable(comboBox1.Text);
}


so I have one-way binding. So I'd like to do some changes and after send them back to db. So question is simple: should I do it manually or analogue of L2S
SubmitChanges()
exists? I should use SQL requests only. No EF, No L2S and so on.

So I'd like to get some modified/added/removed rows from
DataGrid
and update them in database.
I can do it manually, using 2 lists and after make an except query, but I'd like to get it automatically.

Answer Source

You can determine what has changed by calling GetChanges() and/or update directly by using a TableAdapter.