ASP.NET Tutorial/ADO.net Database/SqlConnectionStringBuilder

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

Automatically converts any connection string into its canonical representation

<%@ Page Language="C#" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"><script runat="server">
    protected void btnConvert_Click(object sender, EventArgs e)
    {
        SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder("Server=localhost;UID=webuser;pwd=secret;database=Northwind");
        lblResult.Text = builder.ConnectionString;
    }
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>SQL Connection String Builder</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:TextBox
        id="txtConnectionString"
        Columns="60"
        Runat="Server" />
    <asp:Button
        id="btnConvert"
        Text="Convert"
        OnClick="btnConvert_Click"
        Runat="Server" />
    <hr />
    <asp:Label
        id="lblResult"
        Runat="server" />
    </div>
    </form>
</body>
</html>


Building connection strings using ConnectionStringBuilder (C#)

<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
    protected void Page_Load(object sender, EventArgs e)
    {
        System.Data.SqlClient.SqlConnectionStringBuilder builder = new System.Data.SqlClient.SqlConnectionStringBuilder();
        builder.DataSource = "localhost";
        builder.InitialCatalog = "Northwind1";
        builder.UserID = "sa";
        builder.Password = "password";
        builder.PersistSecurityInfo = true;
        Configuration config = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("/Yourroot");
        config.ConnectionStrings.ConnectionStrings["AppConnectionString1"].ConnectionString = builder.ConnectionString;
        config.Save();
    }
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
    </div>
    </form>
</body>
</html>


Building connection strings using ConnectionStringBuilder (VB)

<%@ Page Language="VB" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim builder As New System.Data.SqlClient.SqlConnectionStringBuilder()
        builder.DataSource = "localhost"
        builder.InitialCatalog = "Northwind1"
        builder.UserID = "sa"
        builder.Password = "password"
        builder.PersistSecurityInfo = True
        Dim config as Configuration = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("/YourRoot")
        config.ConnectionStrings.ConnectionStrings("AppConnectionString1").ConnectionString = builder.ConnectionString
        config.Save()
    End Sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
    </div>
    </form>
</body>
</html>


Connection tester

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="TestConnection" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
   <title>Testing the Connection</title>
</head>
<body>
   <form id="form1" runat="server">
      <div id="container">
         <h1>Testing the Connection</h1>
         
         This example tests the <code>Connection</code> class.
         
         
         <asp:Label ID="labMsg" runat="server" />
         
      </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.Data.OleDb;
using System.Data.SqlClient;
using System.Web.Configuration;
public partial class TestConnection : System.Web.UI.Page
{
   protected void Page_Load(object sender, EventArgs e)
   {
      SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder();
      builder.DataSource = "localhost";
      builder.InitialCatalog = "Northwind";
      builder.IntegratedSecurity = true;
      string connString = builder.ConnectionString;
      SqlConnection conn = null;
      try
      {
         conn = new SqlConnection(connString);
         conn.Open();
         labMsg.Text = "State of Connection: " + conn.State;
      }
      catch (Exception ex)
      {
         labMsg.Text = "Error occurred accessing the database";
         labMsg.Text += "<br/>" + ex.Message;
      }
      finally
      {
         if (conn != null)
            conn.Close();
      }
   }
}


Construct a SqlConnectionStringBuilder from user input

<%@ 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;
    }
}