ASP.NET Tutorial/ADO.net Database/SqlConnectionStringBuilder
Содержание
Automatically converts any connection string into its canonical representation
<source lang="csharp">
<%@ 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">
<asp:TextBox id="txtConnectionString" Columns="60" Runat="Server" /> <asp:Button id="btnConvert" Text="Convert" OnClick="btnConvert_Click" Runat="Server" />
<asp:Label id="lblResult" Runat="server" />
</form>
</body> </html></source>
Building connection strings using ConnectionStringBuilder (C#)
<source lang="csharp">
<%@ 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">
</form>
</body> </html></source>
Building connection strings using ConnectionStringBuilder (VB)
<source lang="csharp">
<%@ 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">
</form>
</body> </html></source>
Connection tester
<source lang="csharp">
<%@ 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">
Testing the Connection
This example tests the Connection
class.
<asp:Label ID="labMsg" runat="server" />
</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 += "
" + ex.Message; } finally { if (conn != null) conn.Close(); } }
}</source>
Construct a SqlConnectionStringBuilder from user input
<source lang="csharp">
<%@ 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>
<form id="form1" runat="server">
User ID | <asp:TextBox ID="UID" runat="server" width="200px" text="Dino"></asp:TextBox> |
Password | <asp:TextBox ID="Pwd" runat="server" TextMode="Password" width="200px" /> |
Server | <asp:TextBox ID="SourceName" runat="server" text="(local)" width="200px" /> |
<asp:Button runat="server" ID="CreateButton" Text="Create" OnClick="CreateButton_Click" />
<asp:Label ID="NewConnString" runat="server" /> </form>
</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; }
}</source>