Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Create Public Connection String

Status
Not open for further replies.

dvannoy

MIS
May 4, 2001
2,765
US
I'm trying to create a module so I can make the connection string public and avialble from any form. how do I do this in vb.net? in vb6 I would simply call strcn as my active connection.

any help would be appreciated

Imports System.Data.OleDb
Imports System.Data


Module ConSQL

Public Sub ConnectDB()
Dim cn As OleDb.OleDbConnection
Dim strcn As String = _
"Provider=sqloledb;" & _
"Data Source=server;" & _
"Initial Catalog=DB;" & _
"User Id=uid;" & _
"Password=pwd"
cn = New OleDb.OleDbConnection(strcn)
cn.Open()

End Sub


End Module

 

Did you try:
Code:
Imports System.Data.OleDb
Imports System.Data

Module ConSQL

Public Const strcn As String = _
      "Provider=sqloledb;" & _
      "Data Source=server;" & _
      "Initial Catalog=DB;" & _
      "User Id=uid;" & _
      "Password=pwd"

    Public Sub ConnectDB()
        Dim cn As OleDb.OleDbConnection
        cn = New OleDb.OleDbConnection(strcn)
        cn.Open()

    End Sub
End Module

Have fun.

---- Andy
 
I'm getting an error when I'm trying to call strcn. is this the way I should be calling it?

see below

Dim cmd As OleDbCommand

With cmd
.Connection = strcn
.CommandType = CommandType.StoredProcedure

 

Dim cmd As OleDbCommand

With cmd
.Connection = strcn
.CommandType = CommandType.StoredProcedur

The .Connection property of the OleDbCommand object takes an OleDbConnection object, not a string. Here's how I would do this:

Code:
Imports System.Data.OleDb
Imports System.Data

Module ConSQL

Public Const strcn As String = _
      "Provider=sqloledb;" & _
      "Data Source=server;" & _
      "Initial Catalog=DB;" & _
      "User Id=uid;" & _
      "Password=pwd"

    Public [red]Function[/red] ConnectDB()[red] As OleDbConnection[/red]
        Dim cn As OleDb.OleDbConnection
        cn = New OleDb.OleDbConnection(strcn)
        cn.Open()
        [red]Return cn[/red]
    End [red]Function[/red]
End Module

Then in your form code:

Code:
        Dim cmd As OleDbCommand

        With cmd
            .Connection = [red]Module1.ConnectDB[/red]
            .CommandType = CommandType.StoredProcedure

I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson

Arrrr, mateys! Ye needs ta be preparin' yerselves fer Talk Like a Pirate Day!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top