CSharp Setup Sqlconnection (C#) – ADO.Net Database

<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<script Language="c#" runat="server">
  void Page_Load()
  {
//    string strConnection = "user id=sa;password=;";
  //  strConnection += "initial catalog=northwind;data source=EWANB;";
    //strConnection += "Connect Timeout=30";
    string strConnection = "Data Source=whsql-v08.prod.mesa1.secureserver.net;Initial Catalog=DB_49907;User ID=w3mentoruser;Password='password';";    data_src.Text = strConnection;
    SqlConnection objConnection = new SqlConnection(strConnection);
    try
    {
      objConnection.Open();
      con_open.Text="Connection opened successfully.<br />";
      objConnection.Close();
      con_close.Text="Connection closed.<br />";
    }
    catch (Exception e)
    {
      con_open.Text="Connection failed to open.<br />";
      con_close.Text=e.ToString();
    }
  }
</script>
<html>
  <body>
  <h4>Testing the data connection
  <asp:label id="data_src" runat="server"/></h4>
  <asp:label id="con_open" runat="server"/><br />
  <asp:label id="con_close" runat="server"/><br />
  </body>
</html>

Enjoyed this post? Share it!

 

Leave a comment

Your email address will not be published.