Last active
December 18, 2015 07:59
-
-
Save jbubriski/5750708 to your computer and use it in GitHub Desktop.
Executing queries with SqlConnection and SqlCommand.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public void NonQuery() | |
{ | |
var connectionString = ConfigurationManager.ConnectionStrings["Default"].ConnectionString; | |
var sqlCommandText = "UPDATE People SET Name = 'John'"; | |
using (var sqlConnection = new SqlConnection(connectionString)) | |
using (var sqlCommand = new SqlCommand(sqlCommandText, sqlConnection)) | |
{ | |
sqlCommand.Connection.Open(); | |
sqlCommand.ExecuteNonQuery(); | |
sqlCommand.Connection.Close(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public void Query() | |
{ | |
var connectionString = ConfigurationManager.ConnectionStrings["Default"].ConnectionString; | |
var sqlCommandText = "SELECT FirstName FROM People WHERE LastName = @LastName"; | |
using (var sqlConnection = new SqlConnection(connectionString)) | |
using (var sqlCommand = new SqlCommand(sqlCommandText, sqlConnection)) | |
{ | |
sqlCommand.Parameters.AddWithValue("LastName", "Bubriski"); | |
sqlCommand.Connection.Open(); | |
using(var sqlReader = sqlCommand.ExecuteReader()) | |
{ | |
while(sqlReader.Read()) | |
{ | |
// Do something with the data | |
var firstName = sqlReader["FirstName"]; | |
} | |
} | |
sqlCommand.Connection.Close(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public DataSet QueryDataSet() | |
{ | |
var dataSet = new DataSet(); | |
var connectionString = ConfigurationManager.ConnectionStrings["Default"].ConnectionString; | |
var sqlCommandText = "SELECT FirstName FROM People WHERE LastName = @LastName"; | |
using (var sqlConnection = new SqlConnection(connectionString)) | |
using (var sqlCommand = new SqlCommand(sqlCommandText, sqlConnection)) | |
{ | |
sqlCommand.Parameters.AddWithValue("LastName", "Bubriski"); | |
sqlCommand.Connection.Open(); | |
using (var sqlDataAdapter = new SqlDataAdapter(sqlCommand)) | |
{ | |
sqlDataAdapter.Fill(dataSet); | |
} | |
sqlCommand.Connection.Close(); | |
} | |
return dataSet; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public int Scalar() | |
{ | |
var peopleCount = 0; | |
var connectionString = ConfigurationManager.ConnectionStrings["Default"].ConnectionString; | |
var sqlCommandText = "SELECT COUNT(*) FROM People"; | |
using (var sqlConnection = new SqlConnection(connectionString)) | |
using (var sqlCommand = new SqlCommand(sqlCommandText, sqlConnection)) | |
{ | |
sqlCommand.Connection.Open(); | |
peopleCount = sqlCommand.ExecuteScalar(); | |
sqlCommand.Connection.Close(); | |
} | |
return peopleCount; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment