Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    Sep 2007
    Posts
    27
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Easiest way possible to write ASP .NET code that retrieves data from MS SQL 2K8R2 db

    Hi Everyone,

    I have been asked to write some ASP that will interface with an MS SQL 2008 R2 database; with the intention of simply reading data from several tables (using SELECT). This is meant to be an easy task, however after delving into ASP .NET today I have quickly realized that there are many different avenues that I can take.

    For starters I have two development tools available to me; Visual Studio 2013 & WebMatrix. Furthermore even within VS 2013 I have seven options for creating a new site, these include 'ASP.NET Empty Web Site', and 'ASP.NET Web Site (Razor vX)'. Once a Web Site type is chosen within VS 2013 there are several .NET Frameworks available (ranging from .NET 2.0 through to .NET 4.5).

    I did begin my day by creating an ASP.NET Web Site (Razor V3) site, using .NET Framework 4.5, however I quickly encountered a number of errors. I was able to work through one or two of them, but am now stuck with authentication related troubles. I don't want to have to 'jump through hoops' to get this working, and am not really interested in Authentication.

    The code that I am writing is for 'proof of concept' and will never be extended to include the entry of data into the SQL database.

    I need the easiest way possible of writing ASP that will allow me to connect to and retrieve data from table(s).

    Any suggestions on the best direction here, and preferably links to tutorials, will be greatly appreciated.

    Kind Regards,

    Davo

  • #2
    Senior Coder alykins's Avatar
    Join Date
    Apr 2011
    Posts
    1,776
    Thanks
    41
    Thanked 196 Times in 195 Posts
    Connecting to a database and displaying it in a web app are two completely different questions. No clue how your web app is designed but for getting the data...
    Code:
    private DataTabe populateMe()
    {
        string connStr = ""; link: SQL2k8R2 Connection Strings
        DataTable dt = new DataTable();
        SqlConnection conn = new SqlConnection(connStr);
        
        using(conn)
        {
            SqlCommand cmd = new SqlCommand("enter SQL text here", conn);
            cmd.CommandType = CommandType.Text; // if you use a stored proc, use CommandType.StoredProcedure
            using(cmd)
            {
              conn.Open();
              SqlDataReader dr = cmd.ExecuteReader();
              dt.Load(dr);
              conn.Close();
            }
    
        }
        return dt;
    }
    Last edited by alykins; 06-16-2014 at 05:11 PM.

    I code C hash-tag .Net
    Reference: W3C W3CWiki .Net Lib
    Validate: html CSS
    Debug: Chrome FireFox IE


  •  

    Posting Permissions

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