using System.Data.SqlClient; class Program { static void Main(string[] args) { string connectionString = "Data Source=server_name;Initial Catalog=database_name;Integrated Security=True"; string query = "SELECT * FROM customers"; using (SqlConnection connection = new SqlConnection(connectionString)) { SqlCommand command = new SqlCommand(query, connection); connection.Open(); SqlDataReader reader = command.ExecuteReader(); while (reader.Read()) { Console.WriteLine(reader["customer_name"].ToString()); } reader.Close(); } } }
using System.Data; using System.Data.SqlClient; class Program { static void Main(string[] args) { string connectionString = "Data Source=server_name;Initial Catalog=database_name;Integrated Security=True"; string storedProcedure = "get_customers"; using (SqlConnection connection = new SqlConnection(connectionString)) { SqlCommand command = new SqlCommand(storedProcedure, connection); command.CommandType = CommandType.StoredProcedure; connection.Open(); SqlDataReader reader = command.ExecuteReader(); while (reader.Read()) { Console.WriteLine(reader["customer_name"].ToString()); } reader.Close(); } } }In this example, we create a SqlConnection object and a SqlCommand object with the name of the stored procedure. We set the CommandType of the SqlCommand object to CommandType.StoredProcedure and execute it using the ExecuteReader method to get a SqlDataReader object. We then iterate through the SqlDataReader object to get the data from the stored procedure and write it to the console. The package library for SqlDataReader is System.Data.SqlClient.