コードの本体は SqlConnection クラスです。
<%@ PAGE LANGUAGE="C#" %> <%@ Import Namespace="System.Data.SqlClient" %> <html> <head> <title>connect db</title> <script runat="server"> String ok_str = "Data Source=.\\SQLExpress;Integrated Security=sspi;"; String ng_str = "Data Source=.\\SQLExpres;Integrated Security=sspi;Connection Timeout=1;"; void connect_test(Object sender, CommandEventArgs e) { String connection_string = ""; switch(e.CommandName) { case "ok_test": connection_string = ok_str; break; default: connection_string = ng_str; break; } using (SqlConnection con = new SqlConnection(connection_string)) { try { con.Open(); con.Close(); lbl_result.Text = "connect success"; } catch(SqlException sqle) { lbl_result.Text = sqle.Message; } } } </script> </head> <body> <form runat="server"> <asp:Button id="btn_connectdb1" Text="Connect DB" CommandName="ok_test" OnCommand="connect_test" runat="server" /> <asp:Button id="btn_connectdb2" Text="Fail DB" CommandName="ng_test" OnCommand="connect_test" runat="server" /><br> <asp:Label id="lbl_result" Text="" runat="server" /> </form> </body> </html>