Go Back   Velocity Reviews > Newsgroups > ASP Net
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

Reply

ASP Net - what is the best way to connect to a sql database?

 
Thread Tools Search this Thread
Old 02-07-2004, 03:51 PM   #1
Default what is the best way to connect to a sql database?


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
  Reply With Quote
Old 02-07-2004, 05:29 PM   #2
Chip
 
Posts: n/a
Default Re: what is the best way to connect to a sql database?
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
  Reply With Quote
Old 02-08-2004, 04:40 AM   #3
Martha[MSFT]
 
Posts: n/a
Default Re: what is the best way to connect to a sql database?
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=usern ame;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]
  Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Prerequisites 70-745 (Business Intelligence) Valmont MCITP 3 06-24-2008 03:03 PM
SQL Server 2008 delayed into Q3 2008 darrilgibson@cox.net MCITP 0 01-27-2008 10:26 PM
MCITP SQL Server 2005 or SQL Server 2008 Darrilgibson@gmail.com MCITP 0 12-19-2007 01:56 PM
how to write codes to connect access database using html codes hiralcp Software 0 12-18-2007 08:49 PM
Trying to connect to a mySQL database and get... Plagued Software 0 06-07-2007 10:14 PM




SEO by vBSEO 3.3.2 ©2009, Crawlability, Inc.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46