Hi Cj,
As for "using", you can also use a "try...catch...finally" block to replace
it. For example:
===================
Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load
Dim conn As SqlConnection = Nothing
Try
conn = New
SqlConnection(ConfigurationManager.ConnectionStrings("myConnectionString").C
onnectionString)
'......
Catch ex As Exception
Finally
conn.Close()
End Try
End Sub
==================
the "finally" block will help ensure the connection get closed even there
occurs exception(the same as using).
Also, here I use "ConfigurationManager.ConnectionStrings" collection to
retrieve connectionstring info(instead of hard code it in each function).
And the actual connecction string is defined in app.config as below:
=-====================
<?xml version='1.0' encoding='utf-8'?>
<configuration>
<connectionStrings>
<clear />
<add name="myConnectionString"
connectionString="[Connection String content here]" />
</connectionStrings>
.........................
</configuration>
=====================
#Connection Strings and Configuration Files (ADO.NET)
http://msdn.microsoft.com/en-us/library/ms254494.aspx
#Getting connection string from app.config in .NET 2.0
http://shico.blogspot.com/2007/04/getting-connection-string-from.html
In addition, if you want to further centralize your code so as to avoid
duplicated connection creating code, you can encapsulate them in a utility
class. e.g.
==============
public class DBUtil
Public Shared Function GetConnection(ByVal connName As String) As
SqlConnection
Dim conn As New
SqlConnection(ConfigurationManager.ConnectionStrings(connName).ConnectionStr
ing)
Return conn
End Function
end class
=============
Then, you can call this utility function in each place you need to create
connection.
Sincerely,
Steven Cheng
Microsoft MSDN Online Support Lead
Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.
--------------------
Date: Tue, 27 Jan 2009 16:03:52 -0500
From: cj2 <
[email protected]>
User-Agent: Thunderbird 2.0.0.19 (Windows/20081209)
MIME-Version: 1.0
Subject: Re: sharing a db connection among functions in web service
Yes, perhaps you can point me to a good article on creating setting
files. In the past I've made xml files that I created on c:\ for
instance. They work but surely there is a better way of saving settings.