connection code in app_cofig file using windows form
-
my connection code is: String strConnection; strConnection = "server=localhost;User ID=sa;Password=sas;database=db"; SqlConnection conn = new SqlConnection(strConnection); conn.Open(); how will i write it in any application configuration file working in windows form,and call it on every page that needs to b connected?? can any body help me??
$h@ma!|@
-
my connection code is: String strConnection; strConnection = "server=localhost;User ID=sa;Password=sas;database=db"; SqlConnection conn = new SqlConnection(strConnection); conn.Open(); how will i write it in any application configuration file working in windows form,and call it on every page that needs to b connected?? can any body help me??
$h@ma!|@
i write in the folowing manner
Sonia Gupta Soniagupta1@yahoo.co.in Yahoo messengerId-soniagupta1 Love is Friendship and Friendship is Love....
-
my connection code is: String strConnection; strConnection = "server=localhost;User ID=sa;Password=sas;database=db"; SqlConnection conn = new SqlConnection(strConnection); conn.Open(); how will i write it in any application configuration file working in windows form,and call it on every page that needs to b connected?? can any body help me??
$h@ma!|@
First and foremost, this is a VB.Net question (Not ASP.Net since you are using Win Forms) second in your app.config you need a < connectionStrings > < /connectionStrings > section (like below) then add your connection string as I have. The weird thing about your post is you ask how to make it work in the app.config in Windows Forms then you ask how to call it on every "page" taht needs it, Windows Forms dont have "pages". The below example will work in Windows Forms (which is what you asked). As for the 2nd part of your question, the easiest way to call it is to write a function like below:
'This returns the connection string
Public Shared Function GetConnectionString(ByVal strConnection As String) As String
Dim strReturn As New String("")
'If you have multiple connection strings then you can pass it a connection name otherwise it defaults to your default connection
If Not String.IsNullOrEmpty(strConnection) Then
strReturn = ConfigurationManager.ConnectionStrings(strConnection).ConnectionString
Else
strReturn = ConfigurationManager.ConnectionStrings("MyConnectionName").ConnectionString
End If
Return strReturn
End FunctionThen on the form that needs a DB connection call it like this:
Dim oConnection As New SqlConnection(YourClassName.GetConnectionString("MyConnectionName"))
<connectionstrings>
<add name="MyConnectionName">
connectionString="Persist Security Info=False;Data Source=localhost;Initial Catalog=db;Integrated Security=SSPI;Trusted_Connection=TRUE;Application Name=AppName"
providerName="System.Data.SqlClient" />
</add>
</connectionstrings>If you mis-worded your question and need this for an ASP.Net application (not Windows Forms) reply and let me know and I'll help as much as I can
"Well yes, it is an Integer, but it's a metrosexual Integer. For all we know, under all that hair gel it could be a Boolean." Tom Welch