Databases Reference
In-Depth Information
// create connection
SqlConnection conn = new SqlConnection(connString);
try {
// open connection
conn.Open();
Console.WriteLine("Connection opened.");
}
catch (SqlException e) {
// display error
Console.WriteLine("Error: " + e);
}
finally {
// close connection
conn.Close();
Console.WriteLine("Connection closed.");
}
}
}
}
3. Run the application by pressing Ctrl+F5. If the connection is successful, you'll see
the output in Figure 10-1.
Figure 10-1. Connecting and disconnecting
If the connection failed, you'll see an error message as in Figure 10-2. (You can
get this by shutting down SSE first, with net stop mssql$sqlexpress entered at
a command prompt. If you try this, remember to restart it with net start
mssql$sqlexpress .)
Search WWH ::




Custom Search