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

Database Connection Function

Hello to all,

I need some help. I am looking for a database connection function that I can
use in my ASP.NET site. This would be in VB.NET. What I want to be able to
do is call the function and it creates the connection and I can use it in my
site. I want to pass the function some values and it uses it. Please take a
look at what I have started with and help me how to get it right.

Function OpenMsAccessOledbConn(ByVal dbPath As String, ByVal dbID As String,
ByVal dbPassword As String)
Dim dbConn As OleDbConnection
Dim dbConnStr As String = _
"Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & Path & ";" & _
"User ID=" & dbID & ";" & _
"Password=" & dbPassword
dbConn = New OleDb.OleDbConnection(dbConnStr)
dbConn.Open()
End Function

When I call the function this is what I will use:

call OpenMsAccessOledbConn("db1.mdb", "admin", "admin")

Tell me also if there is a better way for doing this.

Thanks in advance.
Mar 20 '06 #1
7 2145
It's been a while since I've looked at VB.NET but it looks to me that
all your function is doing is opening an OleDbConnection but you aren't
returning it out of your function - so you won't be able to use it to
pass SQL to Access.

It might be best to declare it as returning an OleDbConnection object
and return out dbConn

Also you might want to use
String.Format("Provider=Microsoft.Jet.OLEDB.4.0;Da ta Source={0};User
ID={1};Password={2}", Path , dbID, dbPassword) to make the
concatenation of connection strign faster and easier to rad

Mar 20 '06 #2
I don't much care for this approach.

Connections should be opened as late as possible and closed as early as
possible.

Having a function open the connection means it isn't being opened as late as
possible, and makes it that much likelier that it won't be closed.

Also, string.format() is ur best friend...
public shared function GetMsAccessOldebConnection(path as string, user as
string, password as string) as OleDbConnection
dim connectionString as string =
string.Format("Provider=Microsoft.Jet.OLEDB.4.0;Da ta Source={0};...", path);
return new OleDbConnection(connectionString);
end function
Karl

--
http://www.openmymind.net/
http://www.fuelindustries.com/
"momo" <ma***@seeourweb.com> wrote in message
news:Og***************@TK2MSFTNGP10.phx.gbl...
Hello to all,

I need some help. I am looking for a database connection function that I
can use in my ASP.NET site. This would be in VB.NET. What I want to be
able to do is call the function and it creates the connection and I can
use it in my site. I want to pass the function some values and it uses it.
Please take a look at what I have started with and help me how to get it
right.

Function OpenMsAccessOledbConn(ByVal dbPath As String, ByVal dbID As
String, ByVal dbPassword As String)
Dim dbConn As OleDbConnection
Dim dbConnStr As String = _
"Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & Path & ";" & _
"User ID=" & dbID & ";" & _
"Password=" & dbPassword
dbConn = New OleDb.OleDbConnection(dbConnStr)
dbConn.Open()
End Function

When I call the function this is what I will use:

call OpenMsAccessOledbConn("db1.mdb", "admin", "admin")

Tell me also if there is a better way for doing this.

Thanks in advance.

Mar 20 '06 #3
I agree with Karl.

This method has a vb6 flavor to it. I can't "prove it", I'm just offering
an opinion.

I would also suggest looking at the EnterpriseLibrary.

The gist of the EnterpriseLibrary is that alot of developers have the same
types of need. One of them: DataAccess.

You can get the EnterpriseLibrary for 1.1 or 2.0.

If you get the 1.1 version, you'll need to do some additions for OleDB

http://www.gotdotnet.com/Community/U...1-3781DF71DCDD

...

1. Get the 1.1 EnterpriseLibrary
2. Make the additions/slight mods to incorporate the OleDB provider
3. Learn how to effectively use the EnterpriseLibrary

the start up costs are well worth it.


"momo" <ma***@seeourweb.com> wrote in message
news:Og***************@TK2MSFTNGP10.phx.gbl...
Hello to all,

I need some help. I am looking for a database connection function that I can use in my ASP.NET site. This would be in VB.NET. What I want to be able to
do is call the function and it creates the connection and I can use it in my site. I want to pass the function some values and it uses it. Please take a look at what I have started with and help me how to get it right.

Function OpenMsAccessOledbConn(ByVal dbPath As String, ByVal dbID As String, ByVal dbPassword As String)
Dim dbConn As OleDbConnection
Dim dbConnStr As String = _
"Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & Path & ";" & _
"User ID=" & dbID & ";" & _
"Password=" & dbPassword
dbConn = New OleDb.OleDbConnection(dbConnStr)
dbConn.Open()
End Function

When I call the function this is what I will use:

call OpenMsAccessOledbConn("db1.mdb", "admin", "admin")

Tell me also if there is a better way for doing this.

Thanks in advance.

Mar 20 '06 #4
Thanks Karl for your help. I am new to ASP.NET. Can you tell me how you
would used this function in your code.
"Karl Seguin [MVP]" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME
net> wrote in message news:ut**************@TK2MSFTNGP14.phx.gbl...
I don't much care for this approach.

Connections should be opened as late as possible and closed as early as
possible.

Having a function open the connection means it isn't being opened as late
as possible, and makes it that much likelier that it won't be closed.

Also, string.format() is ur best friend...
public shared function GetMsAccessOldebConnection(path as string, user as
string, password as string) as OleDbConnection
dim connectionString as string =
string.Format("Provider=Microsoft.Jet.OLEDB.4.0;Da ta Source={0};...",
path);
return new OleDbConnection(connectionString);
end function
Karl

--
http://www.openmymind.net/
http://www.fuelindustries.com/
"momo" <ma***@seeourweb.com> wrote in message
news:Og***************@TK2MSFTNGP10.phx.gbl...
Hello to all,

I need some help. I am looking for a database connection function that I
can use in my ASP.NET site. This would be in VB.NET. What I want to be
able to do is call the function and it creates the connection and I can
use it in my site. I want to pass the function some values and it uses
it. Please take a look at what I have started with and help me how to get
it right.

Function OpenMsAccessOledbConn(ByVal dbPath As String, ByVal dbID As
String, ByVal dbPassword As String)
Dim dbConn As OleDbConnection
Dim dbConnStr As String = _
"Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & Path & ";" & _
"User ID=" & dbID & ";" & _
"Password=" & dbPassword
dbConn = New OleDb.OleDbConnection(dbConnStr)
dbConn.Open()
End Function

When I call the function this is what I will use:

call OpenMsAccessOledbConn("db1.mdb", "admin", "admin")

Tell me also if there is a better way for doing this.

Thanks in advance.


Mar 20 '06 #5
Well, you'd do something like:

dim connection as OleDbConnecton
dim command as OleDbCommand
....set up ur command
try
connection = GetMsAccessOldebConnection()
connection.Open()
command.ExecuteNonQuery()
finally
if not connection is nothing hten
connection.Dispose()
end if
if not command is nothing hten
command .Dispose()
end if
end try

--
http://www.openmymind.net/
http://www.fuelindustries.com/
"momo" <ma***@seeourweb.com> wrote in message
news:OS**************@tk2msftngp13.phx.gbl...
Thanks Karl for your help. I am new to ASP.NET. Can you tell me how you
would used this function in your code.
"Karl Seguin [MVP]" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME
net> wrote in message news:ut**************@TK2MSFTNGP14.phx.gbl...
I don't much care for this approach.

Connections should be opened as late as possible and closed as early as
possible.

Having a function open the connection means it isn't being opened as late
as possible, and makes it that much likelier that it won't be closed.

Also, string.format() is ur best friend...
public shared function GetMsAccessOldebConnection(path as string, user as
string, password as string) as OleDbConnection
dim connectionString as string =
string.Format("Provider=Microsoft.Jet.OLEDB.4.0;Da ta Source={0};...",
path);
return new OleDbConnection(connectionString);
end function
Karl

--
http://www.openmymind.net/
http://www.fuelindustries.com/
"momo" <ma***@seeourweb.com> wrote in message
news:Og***************@TK2MSFTNGP10.phx.gbl...
Hello to all,

I need some help. I am looking for a database connection function that I
can use in my ASP.NET site. This would be in VB.NET. What I want to be
able to do is call the function and it creates the connection and I can
use it in my site. I want to pass the function some values and it uses
it. Please take a look at what I have started with and help me how to
get it right.

Function OpenMsAccessOledbConn(ByVal dbPath As String, ByVal dbID As
String, ByVal dbPassword As String)
Dim dbConn As OleDbConnection
Dim dbConnStr As String = _
"Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & Path & ";" & _
"User ID=" & dbID & ";" & _
"Password=" & dbPassword
dbConn = New OleDb.OleDbConnection(dbConnStr)
dbConn.Open()
End Function

When I call the function this is what I will use:

call OpenMsAccessOledbConn("db1.mdb", "admin", "admin")

Tell me also if there is a better way for doing this.

Thanks in advance.



Mar 20 '06 #6
Okay, here is what I understand so far.

Sub GetMsAccessOledbConnection(ByVal dbPath As String, ByVal dbID As
String, ByVal dbPassword As String)
Dim dbConn As OleDbConnection
Dim dbConnStr As String.Format("Provider=Microsoft.Jet.OLEDB.4.0;Da ta
Source={0};User ID={1};Password={2}", dbPath , dbID, dbPassword)
return new OleDbConnection(dbConnStr);
End Sub

dim CONNdb as OleDbConnecton
dim CMDdb as OleDbCommand
Dim strSQL as String
strSQL="select * from table"
CMDdb=New OLEDBCommand(strSQL,CONNdb)

try
CONNdb = GetMsAccessOldebConnection("db1.mdb", "admin", "admin")
CONNdb.Open()
CMDdb.ExecuteNonQuery()
finally

Tell me what may be wrong with it

Also explain this code and please write it out for me.

if not connection is nothing hten
connection.Dispose()
end if
if not command is nothing hten
command .Dispose()
end if
end try

thanks again

"Karl Seguin [MVP]" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME
net> wrote in message news:Oz**************@tk2msftngp13.phx.gbl...
Well, you'd do something like:

dim connection as OleDbConnecton
dim command as OleDbCommand
...set up ur command
try
connection = GetMsAccessOldebConnection()
connection.Open()
command.ExecuteNonQuery()
finally
if not connection is nothing hten
connection.Dispose()
end if
if not command is nothing hten
command .Dispose()
end if
end try

--
http://www.openmymind.net/
http://www.fuelindustries.com/
"momo" <ma***@seeourweb.com> wrote in message
news:OS**************@tk2msftngp13.phx.gbl...
Thanks Karl for your help. I am new to ASP.NET. Can you tell me how you
would used this function in your code.
"Karl Seguin [MVP]" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME
net> wrote in message news:ut**************@TK2MSFTNGP14.phx.gbl...
I don't much care for this approach.

Connections should be opened as late as possible and closed as early as
possible.

Having a function open the connection means it isn't being opened as
late as possible, and makes it that much likelier that it won't be
closed.

Also, string.format() is ur best friend...
public shared function GetMsAccessOldebConnection(path as string, user
as string, password as string) as OleDbConnection
dim connectionString as string =
string.Format("Provider=Microsoft.Jet.OLEDB.4.0;Da ta Source={0};...",
path);
return new OleDbConnection(connectionString);
end function
Karl

--
http://www.openmymind.net/
http://www.fuelindustries.com/
"momo" <ma***@seeourweb.com> wrote in message
news:Og***************@TK2MSFTNGP10.phx.gbl...
Hello to all,

I need some help. I am looking for a database connection function that
I can use in my ASP.NET site. This would be in VB.NET. What I want to
be able to do is call the function and it creates the connection and I
can use it in my site. I want to pass the function some values and it
uses it. Please take a look at what I have started with and help me how
to get it right.

Function OpenMsAccessOledbConn(ByVal dbPath As String, ByVal dbID As
String, ByVal dbPassword As String)
Dim dbConn As OleDbConnection
Dim dbConnStr As String = _
"Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & Path & ";" & _
"User ID=" & dbID & ";" & _
"Password=" & dbPassword
dbConn = New OleDb.OleDbConnection(dbConnStr)
dbConn.Open()
End Function

When I call the function this is what I will use:

call OpenMsAccessOledbConn("db1.mdb", "admin", "admin")

Tell me also if there is a better way for doing this.

Thanks in advance.



Mar 20 '06 #7
Well, ExecuteNonQuery() is used for Deletes/Updates/Inserts

you'll either want to use an DataReader with ExecuteQuery() or fill a
DataSet/DataTable which'll require an Adapter..

the OleDbConnection and OledbCommand objects implement the IDisposable
interface. This interface defines the method Dispose() which should be
called to clean up any resources.

Before we call Dispose() on both the command and the connection, we make
sure they aren't null/nothing...if they are null/nothing, calling Dispose()
will only 'cuz an error.

Karl

--
http://www.openmymind.net/
http://www.fuelindustries.com/
"momo" <ma***@seeourweb.com> wrote in message
news:u5**************@TK2MSFTNGP14.phx.gbl...
Okay, here is what I understand so far.

Sub GetMsAccessOledbConnection(ByVal dbPath As String, ByVal dbID As
String, ByVal dbPassword As String)
Dim dbConn As OleDbConnection
Dim dbConnStr As String.Format("Provider=Microsoft.Jet.OLEDB.4.0;Da ta
Source={0};User ID={1};Password={2}", dbPath , dbID, dbPassword)
return new OleDbConnection(dbConnStr);
End Sub

dim CONNdb as OleDbConnecton
dim CMDdb as OleDbCommand
Dim strSQL as String
strSQL="select * from table"
CMDdb=New OLEDBCommand(strSQL,CONNdb)

try
CONNdb = GetMsAccessOldebConnection("db1.mdb", "admin", "admin")
CONNdb.Open()
CMDdb.ExecuteNonQuery()
finally

Tell me what may be wrong with it

Also explain this code and please write it out for me.

if not connection is nothing hten
connection.Dispose()
end if
if not command is nothing hten
command .Dispose()
end if
end try

thanks again

"Karl Seguin [MVP]" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME
net> wrote in message news:Oz**************@tk2msftngp13.phx.gbl...
Well, you'd do something like:

dim connection as OleDbConnecton
dim command as OleDbCommand
...set up ur command
try
connection = GetMsAccessOldebConnection()
connection.Open()
command.ExecuteNonQuery()
finally
if not connection is nothing hten
connection.Dispose()
end if
if not command is nothing hten
command .Dispose()
end if
end try

--
http://www.openmymind.net/
http://www.fuelindustries.com/
"momo" <ma***@seeourweb.com> wrote in message
news:OS**************@tk2msftngp13.phx.gbl...
Thanks Karl for your help. I am new to ASP.NET. Can you tell me how you
would used this function in your code.
"Karl Seguin [MVP]" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME
net> wrote in message news:ut**************@TK2MSFTNGP14.phx.gbl...
I don't much care for this approach.

Connections should be opened as late as possible and closed as early as
possible.

Having a function open the connection means it isn't being opened as
late as possible, and makes it that much likelier that it won't be
closed.

Also, string.format() is ur best friend...
public shared function GetMsAccessOldebConnection(path as string, user
as string, password as string) as OleDbConnection
dim connectionString as string =
string.Format("Provider=Microsoft.Jet.OLEDB.4.0;Da ta Source={0};...",
path);
return new OleDbConnection(connectionString);
end function
Karl

--
http://www.openmymind.net/
http://www.fuelindustries.com/
"momo" <ma***@seeourweb.com> wrote in message
news:Og***************@TK2MSFTNGP10.phx.gbl...
> Hello to all,
>
> I need some help. I am looking for a database connection function that
> I can use in my ASP.NET site. This would be in VB.NET. What I want to
> be able to do is call the function and it creates the connection and I
> can use it in my site. I want to pass the function some values and it
> uses it. Please take a look at what I have started with and help me
> how to get it right.
>
> Function OpenMsAccessOledbConn(ByVal dbPath As String, ByVal dbID As
> String, ByVal dbPassword As String)
> Dim dbConn As OleDbConnection
> Dim dbConnStr As String = _
> "Provider=Microsoft.Jet.OLEDB.4.0;" & _
> "Data Source=" & Path & ";" & _
> "User ID=" & dbID & ";" & _
> "Password=" & dbPassword
> dbConn = New OleDb.OleDbConnection(dbConnStr)
> dbConn.Open()
> End Function
>
> When I call the function this is what I will use:
>
> call OpenMsAccessOledbConn("db1.mdb", "admin", "admin")
>
> Tell me also if there is a better way for doing this.
>
> Thanks in advance.
>



Mar 20 '06 #8

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

Similar topics

5
by: news | last post by:
Well, I wrote my first PHP class today. Yeah! But to get it to work, in each function within the class I have to repeat the database connection lines, and that just seems redundant; there has to...
3
by: R Reyes | last post by:
Hi, I'm trying to modularize my database connections a little better and get more out of my project with less code. First check out this common dbOpen() function inside class clsDatabase. I...
2
by: Bryan | last post by:
Hello, I'm just starting to develop in asp.net and i have a question about using a database connection globally in my app. I have set up the procedures for getting all my connection string info...
1
by: dave | last post by:
I'm classic ASP developer and trying to switch to .net ...I'm newbie to ..net... In classic ASP for ADO connectivity i used to put below code in one separate file and used to include that file...
7
by: Lau Lei Cheong | last post by:
Hello, Actually I think I should have had asked it long before, but somehow I haven't. Here's the scenerio: Say we have a few pages in an ASP.NET project, each of them needs to connect to...
3
by: Bob Bedford | last post by:
hello I'm looking for some functions or objects allowing to select-insert-update-delete from any table in a mysql database without the need to create a new query every time. Example: ...
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...
9
by: Jerim79 | last post by:
I am no PHP programmer. At my current job I made it known that I was no PHP programmer during the interview. Still they have given me a script to write with the understanding that it will take me a...
0
by: windandwaves | last post by:
Hi Folk What do you think about this database class? Do you have any feedback or ideas on how it could be improved? Thank you Nicolaas class dbClass {
39
by: alex | last post by:
I've converted a latin1 database I have to utf8. The process has been: # mysqldump -u root -p --default-character-set=latin1 -c --insert-ignore --skip-set-charset mydb mydb.sql # iconv -f...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
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...

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.