Register Blogs FAQ Members List Social Groups Calendar Search Today's Posts Mark Forums Read

Go Back   DeveloperBarn Forums > Programming & Scripting > .Net Development

Sponsored Links

Discuss "Connections" in the .Net Development forum.

.Net Development - Learn about the Microsoft.Net framework and how to create powerful web-based (ASP.net) and client-based (Windows Forms) applications utilizing various languages such as C#, VB.Net, J# and others.


Reply
 
LinkBack Thread Tools Display Modes
  #1  
Old May 23rd, 2008, 07:47 AM
Shem's Avatar
Barn Enthusiast
 
Join Date: Mar 2008
Posts: 263
Thanks: 31
Thanked 5 Times in 5 Posts
Rep Power: 1
Shem is on a distinguished road
Default Connections

Hi

I need to know how to connect to my db, and how to use it when needed
in my apps. I do know that my conn string goes in the web.config file,
though i'm not sure of how to set this up.

All help will be appreciated
Shem
Reply With Quote
Sponsored Links
  #2  
Old May 23rd, 2008, 07:56 AM
jmurrayhead's Avatar
The Barnfather
 
Join Date: Mar 2008
Location: Reston, VA, USA
Posts: 941
Thanks: 22
Thanked 93 Times in 90 Posts
Blog Entries: 5
Rep Power: 4
jmurrayhead is a jewel in the roughjmurrayhead is a jewel in the roughjmurrayhead is a jewel in the roughjmurrayhead is a jewel in the rough

Awards Showcase
Microsoft Windows Microsoft SQL Server Microsoft .Net Classic ASP 
Total Awards: 4

Default

There are several ways to handle this...

One way would be to create a class to handle all of your configuration settings and return a string from the web.config:

Code:
Public Class myConfig
    Public Shared ReadOnly Property myConnection() As String
        Get
            Return ConfigurationManager.ConnectionStrings("yourConStringNameInWebConfig").ConnectionString
        End Get
    End Property
End Class
Then anywhere you need the string:

Code:
Dim conn As SqlConnection = New SqlConnection(myConfig.myConnection)
Of course, if you're using MySQL, it will be a little different, but you'll still use the string value returned from the property to connect to the database.

The preferred way, although a little more difficult, is to create a class that inherits from "ConfigurationSection". I won't get into that, but if you're interested, download the sample from here: Wrox::ASP.NET 2.0 Website Programming: Problem - Design - Solution:Book Information and Code Download

I also highly recommend the book associated with the code.
__________________
jmurrayhead
If you agree with me... click the icon!
If my post solved your problem, click the button in the lower right-hand corner of the post.

If you like it here...throw us a few bones to help
support us.

Join our Folding team: DeveloperBarn Folding

Reply With Quote
  #3  
Old May 23rd, 2008, 08:00 AM
Shem's Avatar
Barn Enthusiast
 
Join Date: Mar 2008
Posts: 263
Thanks: 31
Thanked 5 Times in 5 Posts
Rep Power: 1
Shem is on a distinguished road
Default

Thanks for the help JMH
Reply With Quote
  #4  
Old May 23rd, 2008, 08:02 AM
richyrich's Avatar
Super Moderator
 
Join Date: Mar 2008
Location: Somewhere only we know...
Posts: 454
Thanks: 31
Thanked 42 Times in 42 Posts
Blog Entries: 1
Rep Power: 2
richyrich will become famous soon enoughrichyrich will become famous soon enough

Awards Showcase
JavaScript Classic ASP 
Total Awards: 2

Default

This is how I connect to MySQL db. This doesn't mean it's the best way!

web.config
Code:
  <connectionStrings>
    <add name="myconn" connectionString="Database=mydb;Data Source=mysql.domain.com;Port=3306;User Id=****;Password=****" />
  </connectionStrings>
Data Access Layer
Code:
Imports MySQL.Data.MySqlClient
Imports Company.App.MyBOL
 
    Public Class ConnDAL
        Public Shared ReadOnly Property connString() As String
            Get
                Return System.Configuration.ConfigurationManager.ConnectionStrings.Item("myconn").ConnectionString()
            End Get
        End Property
    End Class
 
    Public Class MyDAL
        Public Shared _error As String
        Public Shared Function Open(ByVal ref As Integer) As MyBOL
            Dim ret As New MyBOL
            Dim conn As MySqlConnection
            Dim mycommand As MySqlCommand
            conn = New MySqlConnection(ConnDAL.connString)
            mycommand = New MySqlCommand("SELECT field1,field2,field3,field4,field5 FROM tbl_name WHERE id=?id")
            mycommand.Connection = conn
            Dim rs As MySqlDataReader
            Try
                mycommand.Parameters.AddWithValue("id", ref)
                conn.Open()
                rsgr = mycommand.ExecuteReader()
                If rsgr.HasRows Then
                    rsgr.Read()
                    ret.property1 = rsgr("field1")
                    ret.property2 = rsgr("field2")
                    ret.property3 = rsgr("field3")
                    ret.property4 = rsgr("field4")
                    ret.property5 = rsgr("field5")
                Else
                    ret.page_error = "No record Found"
                End If
                rsgr.Close()
            Catch ex As Exception
                ret.page_error = ex.Message.ToString
            Finally
                mycommand.Dispose()
                conn.Close()
                conn.Dispose()
            End Try
            Return ret
        End Function
    End Class
You'll need to have the MySQL .NET connector in your bin folder.

Hope that helps.

Last edited by richyrich; May 23rd, 2008 at 08:04 AM.
Reply With Quote
  #5  
Old May 23rd, 2008, 08:09 AM
Shem's Avatar
Barn Enthusiast
 
Join Date: Mar 2008
Posts: 263
Thanks: 31
Thanked 5 Times in 5 Posts
Rep Power: 1
Shem is on a distinguished road
Default

Hey RR, where do ya keep your DAL,BOL,BLL? are they folders and your classes
kept in their respective folder? and are these folders in the App_Code folder?

Shem
Reply With Quote
  #6  
Old May 23rd, 2008, 08:12 AM
richyrich's Avatar
Super Moderator
 
Join Date: Mar 2008
Location: Somewhere only we know...
Posts: 454
Thanks: 31
Thanked 42 Times in 42 Posts
Blog Entries: 1
Rep Power: 2
richyrich will become famous soon enoughrichyrich will become famous soon enough

Awards Showcase
JavaScript Classic ASP 
Total Awards: 2

Default

The DAL, BOL and BLL are seperate files that contain the Data Access classes, Business Object classes and Business Logic classes respectively.

The files are all in the App_Code folder.
Reply With Quote
  #7  
Old May 23rd, 2008, 08:14 AM
Shem's Avatar
Barn Enthusiast
 
Join Date: Mar 2008
Posts: 263
Thanks: 31
Thanked 5 Times in 5 Posts
Rep Power: 1
Shem is on a distinguished road
Default

Quote:
Originally Posted by richyrich View Post
The DAL, BOL and BLL are seperate files that contain the Data Access classes, Business Object classes and Business Logic classes respectively.

The files are all in the App_Code folder.
thanks again

ps. can't rep you or JMH yet
Reply With Quote
  #8  
Old May 23rd, 2008, 08:15 AM
jmurrayhead's Avatar
The Barnfather
 
Join Date: Mar 2008
Location: Reston, VA, USA
Posts: 941
Thanks: 22
Thanked 93 Times in 90 Posts
Blog Entries: 5
Rep Power: 4
jmurrayhead is a jewel in the roughjmurrayhead is a jewel in the roughjmurrayhead is a jewel in the roughjmurrayhead is a jewel in the rough

Awards Showcase
Microsoft Windows Microsoft SQL Server Microsoft .Net Classic ASP 
Total Awards: 4

Default

This is a matter of preference, but I have the following folder structure:

App_Code
-DAL
-BLL
-BO

I also have a separate class file for each class for better organization.
Reply With Quote
  #9  
Old May 23rd, 2008, 08:16 AM
richyrich's Avatar
Super Moderator
 
Join Date: Mar 2008
Location: Somewhere only we know...
Posts: 454
Thanks: 31
Thanked 42 Times in 42 Posts
Blog Entries: 1
Rep Power: 2
richyrich will become famous soon enoughrichyrich will become famous soon enough

Awards Showcase
JavaScript Classic ASP 
Total Awards: 2

Default

Quote:
Originally Posted by jmurrayhead
This is a matter of preference, but I have the following folder structure:

App_Code
-DAL
-BLL
-BO

I also have a separate class file for each class for better organization.
Makes sense.

Does having seperate folders/files have any effect on performance J?
Reply With Quote
  #10  
Old May 23rd, 2008, 08:18 AM
Shem's Avatar
Barn Enthusiast
 
Join Date: Mar 2008
Posts: 263
Thanks: 31
Thanked 5 Times in 5 Posts
Rep Power: 1
Shem is on a distinguished road
Default

where can i go and read a good explanation of the DAL,BOL and BLL ?

Shem
Reply With Quote
Reply

  DeveloperBarn Forums > Programming & Scripting > .Net Development

Bookmarks

Thread Tools
Display Modes

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

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

Forum Jump


All times are GMT -4. The time now is 11:26 PM.



Content Relevant URLs by vBSEO 3.2.0