473,395 Members | 1,458 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,395 software developers and data experts.

database in asp.net

hi,
I'm absolutely newbie to asp.net and i've just started working with database. each time I want to make a database query, I have to start the connection by creating a new sqlConnection object. Is there anyway to keep the connection with the database, err, how to create the dataset or datasource for the whole project, so that I don't have to repeat the above action again and again. does it have anything to do with web.config file?

In php, it can easily be done by create a config.php storing db configuration then include it in all other files. what about in asp.net?

thanx
Nov 21 '08 #1
4 1113
r035198x
13,262 8TB
A wonderful observation that is ignored by most people who post in these forums. In fact you should never mix SQL and asp code. Better create some DB access classes and call methods from those classes. It's the basis of the MVC.

For the connections, you should have a GetConnection() method that returns a connection from some connection pool.
Nov 21 '08 #2
PRR
750 Expert 512MB
you may find these articles useful
Database P1
Database P2
Nov 21 '08 #3
so, may i use this way:
in a file db_class.cs i create a class dbconnection:

Expand|Select|Wrap|Line Numbers
  1.  
  2. class dbConnection
  3. {
  4. // returns the connection to sql database
  5. public SqlConnection getConnection() {
  6.         // connect to sql database
  7.         string sqldb = WebConfigurationManager.ConnectionStrings["testdb"].ConnectionString;
  8.         SqlConnection connectDB = new SqlConnection(sqldb);
  9.         return connectDB;
  10. }
  11.  
  12. // open the connection
  13. public void openDB() {
  14.        this.getConnection().open();      
  15. }
  16.  
  17. // close the connection
  18. public void closeDB() {
  19.        this.getConnection().close();      
  20. }
  21.  
  22. // other method...
  23. //end of dbConnection class
  24.  
  25. }

then I include this class file into other files each time i need to interact with db, and just call the method getConnection() to get connection object (used for sqlCommand object), and openDB() the activate the connection and so on...

am i going the right way? actually i don't know how to include a class file into other files (ow, my bad) (which is similar to include('filename.inc') in php)

i'd be grateful if anyone can correct me and show better ways :)
Nov 26 '08 #4
r035198x
13,262 8TB
You can just use that class in another class withing the the same Namespace. If you decide to use that class in a different Namespace then you can import it using the using keyword. To create a complete model layer you'll need to do a bit more than that. Google DAO for a start.

P.S Wash your mouth next time you say "PHP" <washes mouth>
Dec 10 '08 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Cherrish Vaidiyan | last post by:
sir, The following are the steps that i followed in setting up standby database on Red hat Linux 9. i am using Oracle 9i. i have followed the steps in this site : ...
6
by: Marvin Libson | last post by:
Hi All: I am running DB2 UDB V7.2 with FP11. Platform is Windows 2000. I have created a java UDF and trigger. When I update my database I get the following error: SQL1224N A database...
8
by: Kamlesh | last post by:
Hi, How do I know the physical database path of a database. When I goto the DB2INSTANCE users's directory (/home/db2inst1), I see following folders: /db2inst1/NODE0000/SQL00001...
1
by: pintur | last post by:
The message is: SQL1036C Errore di I/O durante l' accesso al database. SQLSTATE=58030 what is the proble? what for restore tables? thanks
3
by: josh.kuo | last post by:
Sorry about the subject, I can't think of a better one. I recently wrote some PHP classes that I think might be of interest to this group. Since I have been reaping the benefits of reading news...
8
by: morleyc | last post by:
Hi, until recently i was quite happy to add data sources from mssql database in visual studio and drag the datasets directly onto the form this creating a directly editable form which worked well....
0
by: Jack | last post by:
Training Classes for Oracle10g, 9i, 8i Certification training in Oracle10g and 9i: DBA, Developer, Discoverer. training conducted at your location worldwide. Courseware licensing also available....
0
by: Winder | last post by:
Training Classes for Oracle10g, 9i, 8i Certification training in Oracle10g and 9i: DBA, Developer, Discoverer. training conducted at your location worldwide. Courseware licensing also available....
0
by: Laurynn | last post by:
# (ebook - pdf) - programming - mysql - php database applicati # (Ebook - Pdf)Learnkey How To Design A Database - Sql And Crystal Report # (ebook-pdf) E F Codd - Extending the Database Relational...
9
by: Peter Duniho | last post by:
Is there a straightfoward API in .NET that allows for inspection of a database? That is, to look at the structure of the database, without knowing anything in advance about it? For example,...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...

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.