Skip to content

Instantly share code, notes, and snippets.

@AntMooreWebDev
Created July 20, 2017 13:46
Show Gist options
  • Select an option

  • Save AntMooreWebDev/f3da874fc57973775a3e9597c969856c to your computer and use it in GitHub Desktop.

Select an option

Save AntMooreWebDev/f3da874fc57973775a3e9597c969856c to your computer and use it in GitHub Desktop.
How to execute a simple SQL query in C#
using(SqlConnection conn = new SqlConnection(connString))
using(SqlCommand command = new SqlCommand(query, conn))
using (SqlDataAdapter dataAdapter = new SqlDataAdapter(command))
dataAdapter.Fill(results);
using (SqlConnection conn = new SqlConnection(connString))
{
conn.open();
using (SqlCommand command = new SqlCommand(query, conn))
{
SqlDataReader reader = command.ExecuteReader();
while (reader.read())
{
// Do Stuff
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment