I have a public class within a module and there is a variable which holds my connection string like this:
Public ConnectionS As New SqlConnection("Data Source(LocalDB)\MSSQLLocalDB;AttachDbFilename=\MyDB.mdf;Integrated Security=True")
The code above works and saves my Connection string correctly. However when I save my connection string to My.Settings.ConnectionS and use the following code:
Public ConnectionS As New SqlConnection(My.Settings.ConnectionS)
It doesn't save the connection string. When I use the first code above and output the value of ConnectionS into a textbox I get the exact connection string above but when I output the value of the second code I get nothing (blank textbox). I don't understand why because I have my connection string saved to My.Settings.ConnectionS in my application settings.
If I'm understanding your question correctly, you aren't saving the connection the correct way. Instead of saving the setting, you are instantiating a new setting but never actually saving that value to the user setting. The correct way to save your connection to My.Settings would be as follows:
My.Settings.ConnectionS = New SqlConnection("Data Source(LocalDB)\MSSQLLocalDB;AttachDbFilename=\MyDB.mdf;Integrated Security=True")
My.Settings.Save()
Also, be sure that you've set up the ConnectionS setting with a type of SqlConnection.
Here is more information on saving user settings if you need it: https://msdn.microsoft.com/en-us/library/fwc80dzb.aspx
Related
I need a method that will let me keep my MSaccess database open throughout the application operation instead of opening and closing everytime i enter data.
Currently Dim provider As String Dim dataFile As String Dim connString As String Dim myConnection As OleDbConnection = New OleDbConnection Dim cmd As New OleDbCommand
is public declared in each form . And the following is declared on private class
provider = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source="
dataFile = "C:\Program Files (x86)\SACCO\SACCO_DB.accdb"
connString = provider & dataFile
myConnection.Open()
Dim str As String
str = "INSERT INTO STAFF([ID_NO],[FULL_NAME],[EMAIL],[PASSWORD]) Values(?,?,?,?)"
Dim cmd As OleDbCommand = New OleDbCommand(str, myConnection)
This makes my application slower. Kindly, may someone guide me on how i will keep the database open on startup and closed on exit.
yes, this is a long time known issue - and we see near weekly postings about how LARGE delays can occur when opening the database. So for that 20+ years on a NEAR DAILY basis, from Access Front ends to back ends, the solution has been to force + keep a persistent connection. I can make a long post about how and why this occurs, but it has to do with the windows file system - and thus a open of the file, and Access attempting to open exclusive as it attempts to flip the use of the file into high speed single user mode can cause REALLY large delays. As noted, force and to keep the connection open is the near daily advice we see here for close to 20 years.
So, ok, we now using vb.net?
Simply declare a global var in a standard code module as public. Then in your startup code, you can do 1 of two things:
Open the database object - that means you don't have to know ahead of time to "pick" a particlar table to open.
eg this:
Imports System.Data.OleDb
Module MyGlobalCode
Public gDBCon As OleDbConnection ' global connection object
Public Sub GlobalOpenDatabase()
gDBCon = New OleDbConnection(My.Settings.TESTAce)
gDBCon.Open
End Sub
Public Sub GlobalCloseDatabase()
gDBCon.Close()
End Sub
End Module
So, now in any and all code? You use the above gDBCon and you:
Don't have to create a new connection object over and over.
You eliminate this LONG time known slow opening issue.
Now, it not clear why this big delay does NOT occur on some networks and setup - but, if it is occurring, then the above will show spectacular improvements in performance. Often the issue is even virus scanning software, and sometimes it seems to do with Active Directory. And other times, it has to do with VERY slow file creating times (each time you open the database, a ".ldb" file is also created - the time to create this file can be quite long on some systems - again a forced open connection will eliminate your software speeds being limited by external forces such as AD, or virus software, or a good number of other issues that causes this issue.
I am developing a VB.NET update system for a volunteer organisation’s MS Access database. The database is protected by a password as it contains personal information. I have created the application using the VB designer. I need to be able to code the application so that, if the owner decides to change the MS Access password, they will have no need to come back to me to change the code and rebuild the solution. In other words, I do not want the password to be hard coded in the app.config file or the settings.designer.vb file. My code should not need to know the password as a simple call to one of the Fill functions can test any password entered by the user. My problem is that I have found no way to alter the connection string that is tested in the setttings.designer.vb code whenever the database is accessed. I am using Visual Studio 2017.
I have spent a long time searching the web for answers and have tried various solutions involving the configurationmanager without success. I am new to this area so I would be most grateful if anyone here can help.
Here is my latest attempt which still produces an invalid password error even though the third debug statement suggests that the connection string, including the password, has been correctly set.
Public Sub UpdateConnString(connString As String)
Dim configFileMap As New ExeConfigurationFileMap()
Dim config As Configuration = ConfigurationManager.OpenExeConfiguration(configFileMap.ExeConfigFilename)
Dim connStringName As String = "TestConnectionString"
Debug.Print("0 " + config.ConnectionStrings.ConnectionStrings(connStringName).ConnectionString)
config.ConnectionStrings.ConnectionStrings(connStringName).ConnectionString = connString
Debug.Print("1 " + config.ConnectionStrings.ConnectionStrings(connStringName).ConnectionString)
config.Save(ConfigurationSaveMode.Modified, True)
Debug.Print("2 " + config.ConnectionStrings.ConnectionStrings(connStringName).ConnectionString)
End Sub
Just because a connection string is stored in the config file, you aren't required to use it as it is. You can read in that default value and then edit it before using it, e.g.
Dim builder As New OleDbConnectionStringBuilder(My.Settings.DefaultConnectionString)
builder.DataSource = dataSource
Dim connectionString = builder.ConnectionString
You can add or modify any part of a connection string you want that way at run time.
Thank you for your response. Unfortunately, the code throws a compilation error - "DefaultConnectionString is not a member of My.Settings".
Fortunatley I have now managed to find a working solution:
'My.Settings.Item("TestConnectionString") = connectionString
I have been using the ADODB.Connection object to connect to our databases. I have begun using the SQLConnection object instead because it is more friendly with DataGridView controls.
The connection string for connection to the ADODB.Connection object is similar to this: DSN=Foo;UID=Bar;PWD=123456;App=Baz.
The connection string for the SQLConnection object is similar to this: Data Source=Foo; Initial Catalog=Bar; User ID=Baz; Password=123456
I prefer specifying the database using the user's DSN, that way each user connects to their local database that is configured in their ODBC. Using the SQLConnection object, it appears I have to specify the database name directly (Data Source, Initial Catalog).
I can manually write some code at the beginning of the app to fetch the name myself and use it throughout, but I'd like to be able to do this through the connection string if possible.
Is there a way that I can alter the connection string for the SQLConnection object to use the DSN that is configured instead?
I made a desktop application in vb.net. I give the reference path of connection string through the .udl. The problem is when I place the folder in another location the path string does not change and hence results in an error. Kindly suggest. Connection details are as follows
_connStr = System.IO.File.ReadAllText("C:\Users\avt\Desktop\New folder\a.udl")
con = New OleDbConnection(_connStr)
First of all, why are you using Text file to save your connection String ?
Go to Project Properties -> Settings and create a ConnectionString setting to save your connection string. That'll solve your problem. Using a Text file for ConnectionString is not a valid way.
I've inherited an application that uses a global database sqlconnection object in order to access the database from every form in the application. The connection is established when the application starts.
I think to have the connection open all the time it's not good practice and I would prefer to change it so I would open the database connection and close it every time I need to access the database.
So I would like to know if I am in the right here.
This is what I would use instead, any suggestion for improvement is welcome:
Public Sub UpdateDatabase(ByVal command As SqlCommand, ByRef NumError As Double, ByRef DescError As String)
Using connection As New SqlConnection(connectionString)
Try
command.ExecuteNonQuery()
command.Dispose()
NumError = 0
DescError = ""
Catch ex As Exception
NumError = Err.Number
DescError = Err.Description
End Try
End Using
End Sub
I send the SqlCommand object to the method instead of a query string because I can add parameters to an SqlCommand object.
the way you are handling the connection with a using is fine and connection will always be closed and disposed for you.
not really good the way you pass the command from the caller, no database engine isolation. For the parameters you can simply pass a list of names and values and add the parameters in your class above and not in the 100.000 places where you call this code.
don't forget to associate the newly opened connection to the command or it will not work.
some people also put another using around the command so command is disposed, inside the using for the connection...