ASP.NET Connection to MS SQL Server 2005 Express

Discussion in 'C#' started by cancer10, Dec 12, 2008.

  1. #1
    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
     
    cancer10, Dec 12, 2008 IP
  2. vihutuo

    vihutuo Well-Known Member

    Messages:
    1,511
    Likes Received:
    34
    Best Answers:
    0
    Trophy Points:
    180
    #2
    Here is the code to connect to a SQL Express database. The connection string should be placed in the web.config file. The database should be in the App_data folder of your application.

    <connectionStrings>
    <add name="ASPNETDBConnectionString" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|MyDatabaseFileName.MDF;Integrated Security=True;Connect Timeout=30;User Instance=True"
    providerName="System.Data.SqlClient" />

    </connectionStrings>

    If you are coming from PHP, C# would be recommended because of the similar syntax
     
    vihutuo, Dec 12, 2008 IP
  3. justcoolsam

    justcoolsam Well-Known Member

    Messages:
    614
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    128
    #3
    Hi Dear,
    The suggestion given by vihutuo is true and the recommendation for language is also true.
    but i give you a example. and also show you that where and how you have to write a code.

    The following code you have to write in webconfig between <configuration> tag like this.

    <configuration>
    <appSettings>
    <add key="CONNECTIONSTRINGNAME"
    value="server=SERVERNAME;database=DATABASENAME;uid=USERID;password=PASSWORD;
    integrated Security=True;Connect Timeout=30;User Instance=True"
    providerName="System.Data.SqlClient"/>
    </appSettings>
    </configuration>

    After that you can use them on any page by write simply on statement as follow:

    cn = new SqlConnection(ConfigurationManager.AppSettings["CONNECTIONSTRINGNAME"]);

    * here cn is a object of SqlConnection class. by using them you can open and close the connection.
     
    justcoolsam, Dec 13, 2008 IP
  4. cancer10

    cancer10 Guest

    Messages:
    364
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    ok thanx,

    but after making a connection, how do I insert a record and select record from the db?

    Plz guide?

    Thanx
     
    cancer10, Dec 13, 2008 IP
  5. vihutuo

    vihutuo Well-Known Member

    Messages:
    1,511
    Likes Received:
    34
    Best Answers:
    0
    Trophy Points:
    180
    #5
    If you are using Visual Studio, drag and drop the SQLDatasourceControl and then configure the control. Then you can drop any of the databound controls like gridview, formview, etc to dispay or insert records
     
    vihutuo, Dec 13, 2008 IP
  6. bibinsmk

    bibinsmk Active Member

    Messages:
    205
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    53
    #6
    bibinsmk, Dec 15, 2008 IP
  7. hajan

    hajan Peon

    Messages:
    22
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Database operations are simple in ASP.NET but... what would you say if you have some Complex operations that cannot be done by using the SqlSources.. or ... will someone use more than 5 SqlDataSources on a page? Come on... if these Data Sources weren't able in ASP.NET, than it would be much harder than PHP.

    So... for you my friend, it's good to start using the SqlDataSources and other objects but I recommend u to start learning doing that with coding it... it would be the best way for u to understand and to be able to go above the frames of the framework
     
    hajan, Dec 16, 2008 IP