473,405 Members | 2,334 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

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

msn
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
Nov 18 '05 #1
2 2557
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" <da***@helixpoint.com> wrote in message
news:us*************@tk2msftngp13.phx.gbl...
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

Nov 18 '05 #2
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" <da***@helixpoint.com> wrote in message
news:us*************@tk2msftngp13.phx.gbl...
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

Nov 18 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: arabub | last post by:
I have an installation of Apache that apparently includes PHP (out of the box Redhat 9.0). If I make database calls to Postgresql, as what user is that call going to be made? The user that...
1
by: kaiwing18 | last post by:
Hi , I have a problem relate to java and database. Could anyone answer me ?Please see the following code. import java.sql.*; public class Result { public static void main(String args) {
2
by: matt (ziba) | last post by:
what is the best way to connect to a mySQL database... is it using an odbc link? thank you in advance
8
by: Midnight Java Junkie | last post by:
Dear Colleagues: I feel that the dumbest questions are those that are never asked. I have been given the opportunity to get into .NET. Our organization has a subscription with Microsoft that...
4
by: Ben | last post by:
Hi, I started with php and mysql and i discovered three methods for connecting to a database: $conn="DRIVER={MySQL ODBC 3.51 DRIVER};SERVER=10.0.0.181;DATABASE=reserv";...
2
by: camma0001 | last post by:
Hi. I'm in a jam... I restored a DB2 database from one win 2003 DB2 8 server system to another DB2 8 server system now I can't connect. I'm trying to connect my tomcat app server to the remote...
2
by: JJ | last post by:
I wanted to run mySQL server as a back end to my web site(s), running on win2k3 with asp2.0 . Unfortunately this doesn't appear to be possible as there is no 'connector' available to connect the...
3
by: Siong.Ong | last post by:
Dear all, my PHP aims to update a MySQL database by selecting record one by one and modify then save. Here are my PHP, but I found that it doesnt work as it supposed to be, for example, when...
0
by: M.-A. Lemburg | last post by:
On 2008-05-17 20:54, Stef Mientki wrote: If you need a common interface on all the platforms, then I'd suggest you have a look at our mxODBC: http://www.egenix.com/products/python/mxODBC/ ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.