what is the best way to connect to a sql database?

Discussion in 'ASP .Net' started by msn, Feb 7, 2004.

  1. msn

    msn Guest

    What is the best way to connect to a sql database in a asp.net page with
    Visual studio. An example would be great. Thanks

    Dave
    msn, Feb 7, 2004
    #1
    1. Advertising

  2. msn

    Chip Guest

    There are so many ways to interface to SQL, it's tough to give an example.
    Here is one, using a storedproc to update a value. No data is returned.

    Dim con As SqlConnection

    Dim cmd As SqlCommand

    con = New SqlConnection(ConnectARListOps)

    con.Open()

    cmd = New SqlCommand("UpdateDiscountGroup", con)

    cmd.CommandType = CommandType.StoredProcedure

    cmd.Parameters.Add("@orgID", SqlDbType.Int).Value =
    Request.QueryString("id")

    cmd.Parameters.Add("@groupID", SqlDbType.Int).Value = txtGroupID.Text

    cmd.Parameters.Add("@invoiceID", SqlDbType.Int).Value =
    lblInvoiceIDHidden.Text

    cmd.ExecuteNonQuery()

    con.Close()





    "msn" <> wrote in message
    news:...
    > What is the best way to connect to a sql database in a asp.net page with
    > Visual studio. An example would be great. Thanks
    >
    > Dave
    >
    >
    Chip, Feb 7, 2004
    #2
    1. Advertising

  3. msn

    Martha[MSFT] Guest

    The most common way to connect to a database is to use the "SqlConnection",
    "SqlCommand" and "SqlDataReader" classes. The SqlConnection class represents
    an open connection to an SQL database. The "SqlCommand" class represents an
    sql statement or stored procedure. The "SqlDataReader" class represents
    results from a db query.

    The following example should be a good starting point:

    string connectionString =
    "Server=ServerName;database=DataBaseName;uid=username;pwd=password";
    SqlConnection conn = new SqlConnection(connectionString);
    conn.Open();
    string commandString = "Select Title from Table;
    SqlCommand command = new SqlCommand(commandString, conn);
    SqlDataReader reader = command.ExecuteReader();
    while(reader.Read())
    {
    Response.Write(reader["Title"]);
    Response.Write("<br>");
    }
    reader.Close();
    conn.Close();

    One more thing, you might want to look at "DataSet" class if you don't want
    to keep the connection to the database open the whole time. You can retrieve
    the records to memory and work on them offline. This is advantageous if you
    are using the same set of data records from multiple pages. You don't want
    to connect to the db from every page.

    Hope this helps.


    "msn" <> wrote in message
    news:...
    > What is the best way to connect to a sql database in a asp.net page with
    > Visual studio. An example would be great. Thanks
    >
    > Dave
    >
    >
    Martha[MSFT], Feb 8, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. kennedystephen

    Best Way To Connect .aspx to XML web service...

    kennedystephen, Aug 3, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    597
    =?Utf-8?B?UGV0ZXIgQnJvbWJlcmcgW0MjIE1WUF0=?=
    Aug 3, 2006
  2. Guy Brom

    best way to connect mssql2k

    Guy Brom, Apr 14, 2004, in forum: ASP .Net Security
    Replies:
    1
    Views:
    90
    Ken Schaefer
    Apr 15, 2004
  3. planetthoughtful

    Newbie: best way to connect to MySQL?

    planetthoughtful, Mar 12, 2007, in forum: Ruby
    Replies:
    6
    Views:
    97
  4. Shane Witbeck
    Replies:
    1
    Views:
    143
    Shane Witbeck
    Aug 21, 2008
  5. Replies:
    2
    Views:
    268
Loading...

Share This Page