using System.Data.SqlClient; SqlConnection conn = new SqlConnection("Data Source=ServerName; Initial Catalog=DatabaseName; User ID=UserName; Password=Password"); conn.Open(); SqlCommand cmd = new SqlCommand(); cmd.Connection = conn; cmd.CommandText = "SELECT * FROM Employee"; SqlDataReader reader = cmd.ExecuteReader(); while (reader.Read()) { Console.WriteLine(reader["EmpName"].ToString()); } reader.Close(); conn.Close();
using System.Data.SqlClient; SqlConnection conn = new SqlConnection("Data Source=ServerName; Initial Catalog=DatabaseName; User ID=UserName; Password=Password"); conn.Open(); SqlCommand cmd = new SqlCommand("SELECT * FROM Employee WHERE EmpID=@ID", conn); cmd.Parameters.AddWithValue("@ID", empID); SqlDataReader reader = cmd.ExecuteReader(); while (reader.Read()) { Console.WriteLine(reader["EmpName"].ToString()); } reader.Close(); conn.Close();This code example demonstrates how to use SelectCommand with parameters to execute a SELECT query with a WHERE clause on a SQL database. The empID parameter is passed to the query using the AddWithValue method. The package library for C# SelectCommand is System.Data.SqlClient.