Інше
DataSet
private void button9_Click(object sender, EventArgs e)
{
string sSQL = "SELECT * FROM [dbo].[UserTable]";
string cn_txt = Properties.Settings.Default.Database1ConnectionString;
SqlConnection cn_connection = new SqlConnection(cn_txt);
if (cn_connection.State != ConnectionState.Open)
{
cn_connection.Open();
}
DataSet tables = new DataSet();
SqlDataAdapter adapter = new SqlDataAdapter(sSQL, cn_connection);
adapter.Fill(tables);
dataGridView1.DataSource = tables.Tables[0];
}
Якщо перший запит пакету не проходить, то другий не виконується
private void button8_Click(object sender, EventArgs e)
{
string cn_txt = Properties.Settings.Default.Database1ConnectionString;
SqlConnection cn_connection = new SqlConnection(cn_txt);
if (cn_connection.State != ConnectionState.Open)
{
cn_connection.Open();
}
string sSQL = "INSERT INTO [dbo].[UserTable](Name, Level, Team) VALUES('" +
textBox1.Text + "','" +
textBox2.Text + "','" +
textBox3.Text + "') ";
SqlTransaction transaction = cn_connection.BeginTransaction();
SqlCommand command = cn_connection.CreateCommand();
command.Transaction = transaction;
command.CommandText = sSQL;
command.ExecuteNonQuery();
command.CommandText = sSQL;
command.ExecuteNonQuery();
transaction.Commit();
command.Dispose();
cn_connection.Close();
}
COUNT
async private void button7_Click(object sender, EventArgs e)
{
string cn_txt = Properties.Settings.Default.Database1ConnectionString;
SqlConnection cn_connection = new SqlConnection(cn_txt);
cn_connection.Close();
if (cn_connection.State != ConnectionState.Open)
{
await cn_connection.OpenAsync();
}
string sSQL = "SELECT COUNT (Id) FROM [dbo].[UserTable];";
SqlCommand command = new SqlCommand(sSQL, cn_connection);
object count = await command.ExecuteScalarAsync();
MessageBox.Show("count = " + count.ToString());
command.Dispose();
cn_connection.Close();
}