using System.Data.SqlClient; // create a new SqlConnection object SqlConnection connection = new SqlConnection("Data Source=MyServer;Initial Catalog=MyDatabase;Integrated Security=True"); // open the connection connection.Open();
using System.Data.SqlClient; // create a new SqlConnection object SqlConnection connection = new SqlConnection("Data Source=MyServer;Initial Catalog=MyDatabase;Integrated Security=True"); // create a SqlCommand object SqlCommand command = new SqlCommand("SELECT * FROM MyTable", connection); // execute the query and get the results SqlDataReader reader = command.ExecuteReader(); while (reader.Read()) { // do something with the data } // close the reader reader.Close();
using System.Data.SqlClient; // create a new SqlConnection object SqlConnection connection = new SqlConnection("Data Source=MyServer;Initial Catalog=MyDatabase;Integrated Security=True"); // create a SqlCommand object with parameters SqlCommand command = new SqlCommand("SELECT * FROM MyTable WHERE Column1 = @Param1", connection); command.Parameters.AddWithValue("@Param1", "Value1"); // execute the query and get the results SqlDataReader reader = command.ExecuteReader(); while (reader.Read()) { // do something with the data } // close the reader reader.Close();Overall, the `SqlConnection` class is a crucial component for interacting with databases in C#.