File: Default.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="ConnectionTester" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:RadioButton id="optSQL"
runat="server"
Text="Use SQL Authentication (with sa account)"
GroupName="Authentication"
></asp:RadioButton>
<br />
<asp:RadioButton id="optWindows"
runat="server"
Text="Use Windows Integrated Authentication"
GroupName="Authentication"
Checked="True"></asp:RadioButton>
<br />
<br />
<asp:button id="cmdConnect"
runat="server"
Text="Connect"
onclick="cmdConnect_Click"></asp:button>
<br />
<br />
<asp:label id="lblInfo" runat="server"></asp:label>
</div>
</form>
</body>
</html>
File: Default.aspx.cs
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Web.Configuration;
using System.Data.SqlClient;
public partial class ConnectionTester : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void cmdConnect_Click(object sender, EventArgs e)
{
string connectionString = "Data Source=localhost\\SQLEXPRESS;Initial Catalog=Pubs;";
if (optWindows.Checked)
{
connectionString += "Integrated Security=SSPI";
}
else
{
connectionString += "User ID=sa";
}
SqlConnection myConnection = new SqlConnection(connectionString);
try
{
myConnection.Open();
lblInfo.Text = "<b>Server Version:</b> " + myConnection.ServerVersion;
lblInfo.Text += "<br /><b>Connection Is:</b> " + myConnection.State.ToString();
}
catch (Exception err)
{
lblInfo.Text = "Error reading the database. ";
lblInfo.Text += err.Message;
}
finally
{
myConnection.Close();
lblInfo.Text += "<br /><b>Now Connection Is:</b> ";
lblInfo.Text += myConnection.State.ToString();
}
}
}
|