Database Reference
In-Depth Information
conn.Close();
Console.WriteLine("Connection closed.");
}
Console.ReadLine();
}
}
}
3.
Run the application by pressing Ctrl+F5. If the connection is successful, you'll
see the output in Figure 12-1.
Figure 12-1.
Opening and closing a database connection
If the connection failed, you'll see an error message as in Figure 12-2. (You can get this by shutting
down the SQL Server service first, with
net stop mssql$<SQL Server instance name>
entered at a
command prompt. If you try this, remember to restart it with
net start mssql$<SQL Server instance
name>
.) Or, the easy way would be to try passing wrong SQL instance name to the connection string.
Figure 12-2.
Error if connection failed while connecting to SQL Server