Hard code connection string in DropDownList : SqlConnection « ADO.net Database « ASP.NET Tutorial






<%@ Page Language="C#" %>
<%@ import Namespace="System.Data.SqlClient" %>
<script runat="server">
void cmdConnect_OnClick(Object sender, EventArgs e) 
{
    String connectString = ConnectionsList.SelectedItem.Text;
      
    SqlConnection sqlConn = null;

    try
    {      
      sqlConn = new SqlConnection(connectString);
      sqlConn.Open();
        
      lblConnectInfo.Text = "Connection successful!";
    }
    catch
    {
      lblConnectInfo.Text = "Connection failed!";
    }
    finally
    {
      if (sqlConn != null)
        sqlConn.Close();
    }        
}
</script>
<html>
  <head>
    <title>Default</title>
  </head>
  <body>
    <form id="form1" method="post" runat="server">
      <b>Note: None of these actually work for this demo.</b><br/>
      <asp:DropDownList id="ConnectionsList" runat="server">
        <asp:listitem value="logonpassword">User ID=user;Password=pass;Initial Catalog=Northwind;Data Source=(local)</asp:listitem>
        <asp:listitem value="IntegratedSecurity">Integrated Security=yes;Initial Catalog=Northwind;Data Source=(local)</asp:listitem>
      </asp:DropDownList>
      <asp:Button id="cmdConnect" onclick="cmdConnect_OnClick" runat="server" Text="Connect"></asp:Button><br />
      <asp:Label id="lblConnectInfo" runat="server"></asp:Label>
    </form>
  </body>
</html>








18.2.SqlConnection
18.2.1.Retrieving Provider Statistics about the database commands executed with the connection
18.2.2.Displaying all provider statistics.
18.2.3.Improving Performance with Connection Pooling
18.2.4.Define data layer in a separate class
18.2.5.Hard code connection string in DropDownList