Connect to SQL server with integrated Authentication or SQL Authentication : SqlServer « ADO.net Database « ASP.NET Tutorial






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();
        }
    }
}








18.42.SqlServer
18.42.1.Connect to SQL server with integrated Authentication or SQL Authentication
18.42.2.Execute delete command with SqlCommand against SqlServer
18.42.3.You can connect to a Local database named MyLocalData.mdf by using the following connection string:
18.42.4.For example, the following connection string enables you to connect to a Server database named MyData:
18.42.5.You use SQLCMD by opening a command prompt and connecting to your database with the following command:
18.42.6.Read data from SQL server and fill asp:dropdownlist (C#)
18.42.7.Handle table relationship (C#)
18.42.8.Insert, update and delete (C#)