ASP.NET with ADO.NET question

Get help on programming - C++, Java, Delphi, etc.
Post Reply
SilverBack
Registered User
Posts: 1387
Joined: 26 Jan 2006, 02:00
Location: JHB
Contact:

ASP.NET with ADO.NET question

Post by SilverBack »

Hi all,

Im going through the Programming ASP.NET (o'Reilly 2005) book and got to the ADO.NET section. Im a bit stumped here cos I dont agree with the way they are connecting to the database object in the book.

In classic ASP I had one database include file and just ASP included the file when I needed to use the database on a ASP page. In the book, it seems that they setup a database connection object on each ASPX page as its needed. I totally disagree with this method because if something changes, it would have to be updated on every single page, instead of being able to make an update in one central place like with a db include file in classic ASP.

What is the best way to use ADO.NET? Is there a way to make use of it like in classic ASP with a database include file (everything sitting in one central place)? Any help appreciated as I am just learning and dont want to pick up bad habits or incorrect methods. Thanks :wink:
Deja Moo: The feeling that you've heard this bull before.
Hire A Programmer -|- Just Source Code Blog
User avatar
Ron2K
Forum Technical Administrator
Posts: 9050
Joined: 04 Jul 2006, 16:45
Location: Upper Hutt, New Zealand
Contact:

Post by Ron2K »

For the purposes of this post, I will assume that your backends are coded in C# and not the pile of spaghetti that is Visual Basic. :wink:

Are you trying to centralize connection strings? If so, and if you're using .NET 2.0, you can stick them all in the web.config file and access them from there.

You'd stick them in like this:

Code: Select all

<configuration>
  <appSettings>
    <add key="connString" value="Data Source=10.0.0.1; Initial Catalog=DatabaseName; User ID=SilverBack; Password=warble;" />
    <!-- other connection strings go here -->
  </appSettings>

  <!-- rest of web.config goes here -->
</configuration>
Then, to use that value in your code:

Code: Select all

SqlConnection sqlConn = new SqlConnection(ConfigurationManager.AppSettings.Get("connString"));
If you want to add a central class that all of your ASPX pages can access, VS2005 can do this as well - it will create the .cs file in the App_Code subdirectory.

Hope this helps.
Kia kaha, Kia māia, Kia manawanui.
SilverBack
Registered User
Posts: 1387
Joined: 26 Jan 2006, 02:00
Location: JHB
Contact:

Post by SilverBack »

Sweet, thanks for the info! It did help! :)
Deja Moo: The feeling that you've heard this bull before.
Hire A Programmer -|- Just Source Code Blog
Post Reply