c# Select MSSQL code example
Example 1: c# sql select
SqlConnection conn = new SqlConnection("Data Source=;Initial Catalog=;Persist Security Info=True;User ID=;Password=");
conn.Open();
SqlCommand command = new SqlCommand("Select id from [table1] where name=@zip", conn);
command.Parameters.AddWithValue("@zip","india");
using (SqlDataReader reader = command.ExecuteReader())
{
if (reader.Read())
{
Console.WriteLine(String.Format("{0}",reader["id"]));
}
}
conn.Close();
Example 2: c# Select MSSQL
DataSet ds = new DataSet();
using (SqlConnection con = new SqlConnection(connectionstr))
{
using (SqlCommand cmd = con.CreateCommand())
{
con.Open();
cmd.CommandText = "select * from master.sys.server_principals";
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
adapter.Fill(ds);
}
}