Results 1 to 5 of 5

Thread: Connecting ASP.Net with C# pages to SQLserver

  1. #1
    Join Date
    Dec 2010
    Posts
    65

    Connecting ASP.Net with C# pages to SQLserver

    How do I connect my asp.net pages with sql server . I am not been able to connect it . sir please send me a very detail the process of connectivity between Asp.net and Sql server.i will highly thankful to you . I have been trying to solve this problem since two weeks but all my efforts have been in vain. I need to connect my pages with the database as without it my pages would no longer be dynamic and the main motive of my project would be void .

  2. #2
    Join Date
    Nov 2008
    Posts
    1,185

    Re: Connecting ASP.Net with C# pages to SQLserver

    Please specify which version of sql server and visual studio are you using because there may be some changes in the coding for different version‘s of visual studio and sql server.For connecting to SQL Server using Windows authentication, perform the following steps: Configure a connection string. Encrypt the connection string, Configure SQL Server security. Test security access.
    Code:
    <connectionStrings>
      <add name="MyDbConn1" 
           connectionString="Server=MyServer;Database=MyDb;Trusted_Connection=Yes;"/>
      <add name="MyDbConn2" 
          connectionString="Initial Catalog=MyDb;Data Source=MyServer;Integrated Security=SSPI;"/>
    </connectionStrings>
    The above code ‘s are same. Its optional to encrypt your data . the code works fine without it also To test your asp.net pages , with the connections you made To test database access, create a test ASP.NET Web application and add the following .aspx page.
    Code:
    <%@ Page Language="C#" %>
    <%@ Import Namespace="System.Data" %>
    <%@ 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 Page_Load(object sender, EventArgs e)
      {
          using (SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["MyDbConn1"].ToString()))
          {
              SqlCommand cmd = new SqlCommand("SELECT COUNT(*) FROM authors", cn);
              cn.Open();
              SqlDataReader rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
              rdr.Read();
              Response.Write(rdr[0].ToString()); //read a value
          }
      }
    </script>
    
    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
        <title>SQL Authentication</title>
    </head>
    <body/>
    </html>

  3. #3
    Join Date
    May 2009
    Posts
    527

    Re: Connecting ASP.Net with C# pages to SQLserver

    There might be an installation problem in sql server . Because most of the time it’s not installed properly and we blame our code for not working. Check if you can see and open the link called query analyzer in sql server. If you don‘t find it there then there might be a problem in the installation. As sql installation rarely points out any problem even if it ‘s installed with errors. And if it has installed properly you can view it then open it can note down the server name. And also check for with security does it you opts for when it connects with the sql server, note it . And then check if you have mentioned the right server name, also check if you have done right setting‘s in integrated security. If the integrated security is set to true then you should be using windows authentication mode for connecting to server. And if it is set to false then you need to write your login id and password.

  4. #4
    Join Date
    Apr 2009
    Posts
    491

    Re: Connecting ASP.Net with C# pages to SQLserver

    If you are connecting it using ms access then this is the code remember that the code written for ms acess is different for that we write for sql . in sql connection we used sql.client as a namespace, while in access we use Microsoft.oledb as the namespace. The code is:

    Code:
    public void ConnectToAccess() 
    { 
    System.Data.OleDb.OleDbConnection conn = new 
    System.Data.OleDb.OleDbConnection(); 
    database. conn.ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0; Data source= @"path \mydatabase.mdb"; 
    try 
    { 
    conn.Open(); 
    
    } 
    catch (Exception Error) 
    { 
    MessageBox.Show(Error.Message); 
    } 
    finally 
    { 
    conn.Close(); 
    } 
    }

  5. #5
    Join Date
    May 2009
    Posts
    542

    Re: Connecting ASP.Net with C# pages to SQLserver

    1. First of all you have to import required namespace , in case is for sqlserver is sqlclient by using directive.

      Code:
      using System.Data.SqlClient;
    2. Create object of sqlconnection class

      Code:
      private SqlConnection  dr = New SqlConnection();
    3. then Assign the path to sqlserver server by setiing ConnectionString property to the sqlserver path.

      Code:
      dr.ConnectionString = " Data Source=.\\Servername; 
      Initial Catalog=DatabaseName; Integrated Security=True";
      For finding out what you should write in integrated security see above.
    4. Then Open connection by calling Open() method

      Code:
      connection.Open();

Similar Threads

  1. How to merge odd and even pages
    By Fausty in forum Windows Software
    Replies: 6
    Last Post: 19-07-2010, 09:26 AM
  2. Oracle Vs. SQLServer 2000
    By Aileen_L in forum Software Development
    Replies: 3
    Last Post: 25-01-2010, 10:28 AM
  3. Asp pages not able to access
    By Hecter in forum Networking & Security
    Replies: 3
    Last Post: 30-11-2009, 06:30 PM
  4. How do i add pages to existing pages in iweb
    By Samsher in forum Software Development
    Replies: 3
    Last Post: 04-05-2009, 10:18 AM
  5. how to reduce the memory useage of the sqlserver.exe process
    By PatrModer in forum Small Business Server
    Replies: 3
    Last Post: 27-02-2008, 03:49 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •