Construct a SqlConnectionStringBuilder from user input : SqlConnectionStringBuilder « ADO.net Database « ASP.NET Tutorial






<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" 
    Inherits="Default"%>

<!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>Connection String Builders</title>
</head>
<body>
    <div id="pageContent">
        <form id="form1" runat="server">
            <table>
            <tr>
            <td><b>User ID</b></td>
            <td><asp:TextBox ID="UID" runat="server" width="200px" 
                text="Dino"></asp:TextBox></td>
            </tr>
            <tr>
            <td><b>Password</b></td>
            <td><asp:TextBox ID="Pwd" runat="server" TextMode="Password" width="200px" /></td>
            </tr>
            <tr>
            <td><b>Server</b></td>
            <td><asp:TextBox ID="SourceName" runat="server" text="(local)" width="200px" /></td>
            </tr>
            </table>
            <asp:Button runat="server" ID="CreateButton" Text="Create" OnClick="CreateButton_Click" />
            <hr />
            <asp:Label ID="NewConnString" runat="server" /> 
        </form>
    </div>
</body>
</html>

File: Default.aspx.cs

using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Default : System.Web.UI.Page {
    protected void CreateButton_Click(object sender, EventArgs e) {
        string serverName = SourceName.Text;
        string userid = UID.Text;
        string pswd = Pwd.Text;

        SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder();
        builder.DataSource = serverName;
        builder.UserID = userid;
        builder.Password = pswd;
        NewConnString.Text = builder.ConnectionString;
    }
}








18.4.SqlConnectionStringBuilder
18.4.1.Construct a SqlConnectionStringBuilder from user input
18.4.2.Automatically converts any connection string into its canonical representation
18.4.3.Building connection strings using ConnectionStringBuilder (C#)
18.4.4.Building connection strings using ConnectionStringBuilder (VB)
18.4.5.Connection tester