Introducing Radical.sh

Forget Code launches a powerful code generator for building API's

Connecting to SQL SERVER in C#

The minimum requirement to connect to SQL SERVER is Data source name and Integrated security.
The code below is the example for connecting to SQL SERVER with minimal requirements.
  1. using System.Data.SqlClient;

  1. private void btnCheckConnection_Click(object sender, EventArgs e)
  2. {
  3. SqlConnection conn = new SqlConnection();
  4. conn.ConnectionString = "data source=Data source name;integrated security=SSPI;";
  5. try
  6. {
  7. conn.Open();
  8. MessageBox.Show("Connection was successful");
  9. }
  10. catch
  11. {
  12. MessageBox.Show("Failed to connect to data source");
  13. }
  14. finally
  15. {
  16. conn.Close();
  17. }
  18. }