Results 1 to 6 of 6

Thread: how to connect Asp.net with sql 05

  1. #1
    Join Date
    Dec 2010
    Posts
    88

    how to connect Asp.net with sql 05

    I’m making a website which contain Asp.net as a front-end and sql Express 05 as a back-end. It is an interactive website therefore I have decided to use sql as a back-end. Could you please guide me how to connect Asp.net with sql. I have not done any database connection till yet, so it will be very helpful for me if you give me the complete procedure of connecting Asp.net with database. Please help me out.

  2. #2
    Join Date
    Jun 2009
    Posts
    321

    Re: how to connect Asp.net with sql 05

    If you are using Asp.net as a front end then you have to write some code in your Asp file to make a connection. please add the follwoing code in you aspx.cs page.

    Step 1 : Add Namspace "using System.Data.SqlClient;"

    Step 2 : Make Sql connection.

    Write this code to create Sql connection.
    SqlConnection con = new SqlConnection("Server=You server name or comp name;Database=Yourdatabasename;Trusted_Connectopn=True");
    SqlCommand cmd = new SqlCommand("Write your sql query here eg. select * from Table name");
    con.Open();
    DataSet ds = new DataSet(cmd,con);
    SqlDataAdapter da = new SqlDataAdapter();
    da.Fill(ds);
    con.Close();

    Hope this should work. If you are unable to connect to a database then try to search on internet.

  3. #3
    Join Date
    Jun 2009
    Posts
    360

    Re: how to connect Asp.net with sql 05

    For making connection with sql express. You have to register your DB with ASP.net Membership classes by executing the command line utility in VS prompt.
    Aspnet_regsql, it will open one window, where you have to configure DB.
    Add the following lines under <system.web>
    <membership>

    <providers>
    <remove name="AspNetSqlMembershipProvider" />
    <add name="AspNetSqlMembershipProvider"
    type="System.Web.Security.SqlMembershipProvider,
    System.Web, Version=2.0.0.0, Culture=neutral,
    PublicKeyToken=b03f5f7f11d50a3a"
    connectionStringName="vsItemsConnectionString"
    enablePasswordRetrieval="false"
    enablePasswordReset="true"
    requiresQuestionAndAnswer="true"
    applicationName="/"
    requiresUniqueEmail="false"
    passwordFormat="Hashed"
    maxInvalidPasswordAttempts="5"
    minRequiredPasswordLength="7"
    minRequiredNonalphanumericCharacters="1"
    passwordAttemptWindow="10"
    passwordStrengthRegularExpression="" />
    </providers>

    </membership>

    <profile>
    <providers>
    <remove name="AspNetSqlProfileProvider" />
    <add name="AspNetSqlProfileProvider"
    connectionStringName="vsItemsConnectionString"
    applicationName="/"
    type="System.Web.Profile.SqlProfileProvider,
    System.Web, Version=2.0.0.0, Culture=neutral,
    PublicKeyToken=b03f5f7f11d50a3a" />
    </providers>
    </profile>

    <roleManager>
    <providers>
    <remove name="AspNetSqlRoleProvider" />
    <add name="AspNetSqlRoleProvider"
    connectionStringName="vsItemsConnectionString"
    applicationName="/"
    type="System.Web.Security.SqlRoleProvider,
    System.Web, Version=2.0.0.0, Culture=neutral,
    PublicKeyToken=b03f5f7f11d50a3a" />
    </providers>
    </roleManager>
    Try to include this code in your file hope it will work for you.

  4. #4
    Join Date
    May 2008
    Posts
    271

    Re: how to connect Asp.net with sql 05

    There are some code which you have to include in your file.
    If the SQL Database is on another server you can point it out with server=COMPUTERNAME\SQLExpress
    If you have another instance name of the database you can change it to..
    <
    add name="aspnet_staterKits_TimeTracker" connectionString="server=.\SQLExpress;uid=timetraxuser;pwd=timetraxpassword;database=timetrax"/>
    <
    remove name="LocalSqlServer"/>
    <
    add name="LocalSqlServer" connectionString="server=.\SQLExpress;uid=timetraxuser;pwd=timetraxpassword;database=timetrax"/>
    include the above code and try to run it. Hope it will work for you.

  5. #5
    Join Date
    May 2008
    Posts
    518

    Re: how to connect Asp.net with sql 05

    You have to add the following lines in your web.config file.
    <connectionStrings>
    <add name="vsItemsConnectionString" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\vsItems.mdf;Integrated Security=True;User Instance=True"
    providerName="System.Data.SqlClient" />
    </connectionStrings>

    Then add the following lines under <system.web>
    <membership>

    <providers>
    <remove name="AspNetSqlMembershipProvider" />
    <add name="AspNetSqlMembershipProvider"
    type="System.Web.Security.SqlMembershipProvider,
    System.Web, Version=2.0.0.0, Culture=neutral,
    PublicKeyToken=b03f5f7f11d50a3a"
    connectionStringName="vsItemsConnectionString"
    enablePasswordRetrieval="false"
    enablePasswordReset="true"
    requiresQuestionAndAnswer="true"
    applicationName="/"
    requiresUniqueEmail="false"
    passwordFormat="Hashed"
    maxInvalidPasswordAttempts="5"
    minRequiredPasswordLength="7"
    minRequiredNonalphanumericCharacters="1"
    passwordAttemptWindow="10"
    passwordStrengthRegularExpression="" />
    </providers>

    </membership>

    <profile>
    <providers>
    <remove name="AspNetSqlProfileProvider" />
    <add name="AspNetSqlProfileProvider"
    connectionStringName="vsItemsConnectionString"
    applicationName="/"
    type="System.Web.Profile.SqlProfileProvider,
    System.Web, Version=2.0.0.0, Culture=neutral,
    PublicKeyToken=b03f5f7f11d50a3a" />
    </providers>
    </profile>

    <roleManager>
    <providers>
    <remove name="AspNetSqlRoleProvider" />
    <add name="AspNetSqlRoleProvider"
    connectionStringName="vsItemsConnectionString"
    applicationName="/"
    type="System.Web.Security.SqlRoleProvider,
    System.Web, Version=2.0.0.0, Culture=neutral,
    PublicKeyToken=b03f5f7f11d50a3a" />
    </providers>
    </roleManager>

  6. #6
    Join Date
    Nov 2009
    Posts
    865

    Re: how to connect Asp.net with sql 05

    You can include the following code in your asp file.
    While creating the database connection, consider the following things in mind.

    1) Add the namespace
    - System.Data
    - System.Data.SqlClient.

    2) Create connection object using connection parameters like server name, database name , user name and password.


    3) Create sqlcommand object and pass connection object and sql query as parameters

    4) Execute the command using ExecuterReader() method of sqlcommand object.
    this will return resultset and get it into sqldatareader object.

    5) Navigate the resultset.

    6) close the connection object as well as command object

    Following is the code:

    Using System.Data;
    Using System.Data.SqlClient;



    SqlConnection con = null;
    SqlCommand cmd = null;
    SqlDataReader rdr ;
    try
    {
    con = new SqlConnection("Data Source = Server Name; Initial Catalog = Database Name; User Name = user name; password = password");

    con.Open();
    cmd = new SqlCommand(con,"sql query");
    rdr = cmd.ExecuteReader();
    while(rdr.Read())
    {
    // Operate on fetched data
    }
    }
    catch(Exception exp)
    {
    MessageBox(exp.Message);
    }
    finally
    {
    if(cmd != null)
    cmd.Close();
    if(con != null)
    con.Close();
    if(rdr != null)
    rdr.Dispose();
    }

Similar Threads

  1. Replies: 9
    Last Post: 01-10-2011, 12:14 AM
  2. Replies: 4
    Last Post: 16-06-2010, 12:54 PM
  3. Replies: 2
    Last Post: 24-03-2009, 07:19 PM
  4. Replies: 1
    Last Post: 08-08-2008, 01:55 PM
  5. Can Connect to Router but unable to connect to internet
    By Q Jones in forum Windows Vista Network
    Replies: 1
    Last Post: 14-09-2007, 03:16 PM

Tags for this Thread

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
  •  
Page generated in 1,711,714,920.26130 seconds with 17 queries