string connectionString = "Data Source=myServerAddress;Initial Catalog=myDataBase;User ID=myUsername;Password=myPassword;"; string query = "SELECT * FROM Customers"; SqlDataAdapter dataAdapter = new SqlDataAdapter(query, connectionString); DataTable dataTable = new DataTable(); dataAdapter.Fill(dataTable);
string connectionString = "Data Source=myServerAddress;Initial Catalog=myDataBase;User ID=myUsername;Password=myPassword;"; string query = "SELECT * FROM Customers WHERE Country=@Country"; SqlDataAdapter dataAdapter = new SqlDataAdapter(query, connectionString); dataAdapter.SelectCommand.Parameters.AddWithValue("@Country", "USA"); DataTable dataTable = new DataTable(); dataAdapter.Fill(dataTable);In this example, we add a parameter to our SQL query using the AddWithValue method of the SelectCommand property of the SqlDataAdapter. This allows us to filter the results of our query based on a variable value, in this case the country parameter set to "USA". The package library for System.Data.SqlClient is included in the .NET Framework Class Library.