|
-
Jun 29th, 2020, 05:28 AM
#1
Thread Starter
Junior Member
How to set a global connection string for all crystal reports in my project
I have a VB.Net project with SQL Server DB. I want to add few Crystal reports to it. I'm using a connection string inside a module which i use every where in my project and its very useful since i can change the login details at a single point.
Code:
connString As String = "Data Source=192.168.1.11,1433;Network Library=DBMSSOCN;Initial Catalog=MY_DB;User Id=sa;Password = abc@1234.;" 'global connection string
Same way, is it possible to have a common global connection string somewhere embedded inside (Ex. module) my project (Not a DSN) or is possible to use the above conn string.
If it's possible, what is the correct method of doing it and i would be grateful, if someone can post a sample code.
-
Jun 29th, 2020, 05:38 AM
#2
Re: How to set a global connection string for all crystal reports in my project
I don't use CR but I would expect that it would support a connection string stored in the config file, which is where you should generally store it so you can change the connection details even after compiling.
-
Jun 29th, 2020, 08:34 AM
#3
Re: How to set a global connection string for all crystal reports in my project
Personally I use a class that I put any needed config file connection and reference them.
I don't know if this a common practice but It sums the connections into a Class that can be easily referenced.
Something like this:
Code:
#Region "connectionstrings"
Public NotInheritable Class GlobalConnections
Private Sub New()
End Sub
Shared Sub New()
Try
strconVrExternal = ConfigurationManager.ConnectionStrings("DBConnectionString").ToString()
Catch ex As Exception
' This should be handled if we are not getting a db connection and we see that the connection is set and called correctly.
End Try
End Sub
Public Shared Property strconVrExternal() As String
Get
Return m_strconVrExternal
End Get
Set(value As String)
m_strconVrExternal = value
End Set
End Property
Private Shared m_strconVrExternal As String
' For manually force a connection outside of a connection string
Public Shared Sub Setstrcon(newString As String)
strconVrExternal = newString
End Sub
End Class
#End Region
on app.config:
Code:
<connectionStrings>
<add name="DBConnectionString" connectionString="Data Source=server;Initial Catalog=VW;User Id=sa; Password=password;" providerName="System.Data.SqlClient" />
</connectionStrings>
you call it like:
Dim strConnString As String = GlobalConnections.strconVrExternal
ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·
-
Jun 29th, 2020, 08:46 AM
#4
Re: How to set a global connection string for all crystal reports in my project
 Originally Posted by sapator
Personally I use a class that I put any needed config file connection and reference them.
I don't know if this a common practice but It sums the connections into a Class that can be easily referenced.
Something like this:
Code:
#Region "connectionstrings"
Public NotInheritable Class GlobalConnections
Private Sub New()
End Sub
Shared Sub New()
Try
strconVrExternal = ConfigurationManager.ConnectionStrings("DBConnectionString").ToString()
Catch ex As Exception
' This should be handled if we are not getting a db connection and we see that the connection is set and called correctly.
End Try
End Sub
Public Shared Property strconVrExternal() As String
Get
Return m_strconVrExternal
End Get
Set(value As String)
m_strconVrExternal = value
End Set
End Property
Private Shared m_strconVrExternal As String
' For manually force a connection outside of a connection string
Public Shared Sub Setstrcon(newString As String)
strconVrExternal = newString
End Sub
End Class
#End Region
on app.config:
Code:
<connectionStrings>
<add name="DBConnectionString" connectionString="Data Source=server;Initial Catalog=VW;User Id=sa; Password=password;" providerName="System.Data.SqlClient" />
</connectionStrings>
you call it like:
Dim strConnString As String = GlobalConnections.strconVrExternal
I don't really see the point in the class. Simply add the connection string on the Settings page of the project properties. It will be added to the config file automatically and you can access it via My.Settings in code.
-
Jun 29th, 2020, 09:01 AM
#5
Re: How to set a global connection string for all crystal reports in my project
No problem.
As I've said I don't know if this a common practice .
ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·
-
Jun 29th, 2020, 11:54 AM
#6
Re: How to set a global connection string for all crystal reports in my project
 Originally Posted by dilhanmail
I have a VB.Net project with SQL Server DB. I want to add few Crystal reports to it. I'm using a connection string inside a module which i use every where in my project and its very useful since i can change the login details at a single point.
Code:
connString As String = "Data Source=192.168.1.11,1433;Network Library=DBMSSOCN;Initial Catalog=MY_DB;User Id=sa;Password = abc@1234.;" 'global connection string
Same way, is it possible to have a common global connection string somewhere embedded inside (Ex. module) my project (Not a DSN) or is possible to use the above conn string.
If it's possible, what is the correct method of doing it and i would be grateful, if someone can post a sample code.
Many of our older reports have the connection built right into the report. You lose the flexibility of an ini doing that. The newer ones we use ODBC controlled in a config file. You mentioned you didn't want to go that route though.
Please remember next time...elections matter!
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|