Skip to content

Instantly share code, notes, and snippets.

@PashaPash
Created February 6, 2016 19:10
Show Gist options
  • Save PashaPash/d4f8c05a43f48cb01afb to your computer and use it in GitHub Desktop.
Save PashaPash/d4f8c05a43f48cb01afb to your computer and use it in GitHub Desktop.
private void btnSave_Click(object sender, EventArgs e)
{
string connStr = @"Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\LitMapPoltavaData.mdf;Integrated Security=True";
SqlConnection conn = new SqlConnection(connStr);
try
{
//пробуем подключится
conn.Open();
// должно быть 3
int before = (int)(new SqlCommand("SELECT COUNT(*) FROM Writers", conn).ExecuteScalar());
SqlCommand cmd = new SqlCommand("Insert Into Writers (Id, Surname, Name, Lived, Birthplace, ShortInfo)" +
"Values(@Id, @Surname, @Name, @Lived, @Birthplace, @ShortInfo)", conn);
// Добавить параметры
cmd.Parameters.AddWithValue("@Id", tag);
cmd.Parameters.AddWithValue("@Surname", tbSurname.Text);
cmd.Parameters.AddWithValue("@Name", tbName.Text);
cmd.Parameters.AddWithValue("@Lived", tbLived.Text);
cmd.Parameters.AddWithValue("@Birthplace", tbBirthplace.Text);
cmd.Parameters.AddWithValue("@ShortInfo", tbShortInfo.Text);
// должно быть 1
int rowsInserted = cmd.ExecuteNonQuery();
// должно быть 3 + 1 -> 4
int after = (int)(new SqlCommand("SELECT COUNT(*) FROM Writers", conn).ExecuteScalar());
// должно быть 1
int withTag = (int)(new SqlCommand("SELECT COUNT(*) FROM Writers WHERE tag = " + tag, conn).ExecuteScalar());
}
finally
{
conn.Close();
conn.Dispose();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment