|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
Using Try Catch Finally Statements for connections
I'm trying to make a program that uses a sql server database. I wanted to use the Try-Catch-Finally statement to check if the connection is connected, or if the username/password is right, etc. How do I do this? Thanx!
|
|
#2
|
|||
|
|||
|
try
//write here the code for the connection catch ( SQLEXception e ) console.writeline(e.stackTrace) end try |
|
#3
|
|||
|
|||
|
The preferred method is (assuming you're using C#) to use the using keyword. This defines a scope at the end of which the SqlConnection object is disposed.
Code:
using ( SqlConnection conn = new SqlConnection( ... ) ) {
try {
conn.Open();
} catch ( SqlExecption sqlEx ) {
Console.WriteLine( sqlEx.Message );
}
}
|
![]() |
| Viewing: Dev Shed Forums > Programming Languages - More > .Net Development > Using Try Catch Finally Statements for connections |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|