Created
December 3, 2016 07:01
-
-
Save smetronic/16e76458817d8d5623748a53db1f8cbe to your computer and use it in GitHub Desktop.
Retrieving data from database using SqlDataReader
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
var result = ""; | |
using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["MyConnection"].ConnectionString)) | |
{ | |
connection.Open(); | |
using (SqlCommand cmd = new SqlCommand(@"SELECT * FROM Table WHERE Id = @Input", connection)) | |
{ | |
cmd.Parameters.Add("Input", SqlDbType.Int).Value = 1; | |
using (SqlDataReader reader = cmd.ExecuteReader()) | |
{ | |
while (reader.Read()) | |
{ | |
result = reader["FirstName"].ToString(); | |
} | |
} | |
} | |
} | |
return result; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment