SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Guru phantom007's Avatar
    Join Date
    May 2008
    Posts
    736
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)

    Question ASP.NET Connection to MS SQL Server 2005 Express

    Hi Ppl,


    I come from a PHP programming background, I also know classic asp 3 very well. Now, I wanna learn ASP.net, Last night I was going through an ASP.net Ebook, But the connection string given in that book had many lines of code and confusing too.

    In PHP or classic asp, we had 1 or just 2 line of code for connecting to the DB (in VB and C#).

    Can someone please tell me a connection code with a minimal line of code?

    Also, Since I am from a PHP background, Plz suggest me which language should I use for ASP.net (VB or C#)?


    Thanx

  2. #2
    SitePoint Evangelist
    Join Date
    Apr 2008
    Location
    Dublin, Ireland
    Posts
    461
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    In asp.net it's common to put your connection string in the web.config. A basic connection string in the web.config might look like the following:-

    Code:
    <?xml version="1.0"?>
    <configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">
        <appSettings>
            ....<- put your custom settings here
        </appSettings>
        <connectionStrings>
            <add name="myConnectionString" connectionString="Data Source=serverName;Initial Catalog=databaseName;Persist Security Info=True;User ID=username;Password=password;" providerName="System.Data.SqlClient"/>
        </connectionStrings>
        <system.web>
           ....
        </system.web>
    This is based on asp.net 2.0 and above. You can use the Asp.net Configuration to add your connection strings to the web.config (it's the far right button on the toolbar above your solution name in Visual Studio/Visual Web Developer).

    In your code you can access this connection string easily:-

    Code:
    Dim connString as string = ConfigurationManager.ConnectionStrings("myConnectionString").ConnectionString

  3. #3
    SitePoint Mentor NightStalker-DNS's Avatar
    Join Date
    Jul 2004
    Location
    Cape Town, South Africa
    Posts
    2,873
    Mentioned
    44 Post(s)
    Tagged
    0 Thread(s)
    Yea. Use methods as pointed out by BrianOConnell. To get connection strings you can use www.connectionstrings.com for help if you are unclear on something.

    As for wat language, it comes down to preference. I prefer C# as i find it easier to read. But you might be more comfortable with VB seing as tho u come from a asp.old background

  4. #4
    Afrika
    Join Date
    Jul 2004
    Location
    Nigeria
    Posts
    1,737
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you are using visual studio, it does it all without you writing any lines of code

  5. #5
    SitePoint Wizard webcosmo's Avatar
    Join Date
    Oct 2007
    Location
    Boston, MA
    Posts
    1,480
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    You can do it different ways. Most popular is putting it on web.config file. Generally people would define a function for read-only database operations, another for update, insert.


Bookmarks

Posting Permissions

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