Issue with Microsoft Access - c#

We have 2 legacy systems - One in C++ and other in C# that connect to the same access database. The access database is not password protected and we use the following connection string to connect to the databases
Provider=Microsoft.Jet.OLEDB.4.0; Persist Security Info=False ;Data Source= AlarmHistory.mdb
C# application polls a table in the database every 10 seconds. Once in while , the C# application crashes with the error message -
System.Data.OleDb.OleDbException: Cannot start your application. The workgroup information file is missing or opened exclusively by another user.
The log analysis revealed that both the applications are not accessing the database simultaneously ... Any idea on what could cause such a situation... Please Help

They might not be accessing the database at exactly the same time, but that error message indicates that they (or something else) is accessing the lock file at the same time. Is the poll opening and closing the connection each time? You could move the open and close to outside the timer so that the connection is opened and remains open for the life of the application.

Related

SQL Server login fails during a C# application setup

I created a VS setup project in which I added a custom action to create an SQL Server database. I used the code from here https://daoudisamir.com/install-sql-server-database-with-visual-studio-setup/ that is itself a c# adaptation of the Microsoft Docs code here: https://learn.microsoft.com/en-us/previous-versions/visualstudio/visual-studio-2010/49b92ztk(v=vs.100). I made a little adaptation - the entire connection string is in the code:
string connStr = "Data Source = (localdb)\\MSSQLLocalDB; Initial Catalog = master; Integrated Security=True";
This way, I'm not using any dialogue during the installation and it starts the custom action without any input from the user.
But when trying to the installation file, I get each time a "Failed to connect to server (localdb)\MSSQLLocalDB" error. Some of the times, the inner exception message that I have in the log file said "Login failed for user 'NT AUTHORITY\SYSTEM'", sometimes just "A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found".
I know for a fact it's not the code in the custom action - I copied it to a form and added a button for the db creation via the application itself - no problem, no connection problem, db created right away.
I must add, that previously, I had a prerequisite for installing SQL Server Local DB during the application setup. That ended up miserably - it blocked me from having ANY access to the server - I had to uninstall everything VS and reinstall again before it went back to normal.
It seems to me that for some reason, instead of using the default windows user that is running the setup file, for some reason it switches to a different user. Is running the code via an msi file causing this? How can I solve it? It's been 2 full days, I read many threads and didn't find the answer.
Thank you for any help.
Edit: The LocalDB documentation states specifically that
An instance of LocalDB owned by the built-in accounts such as NT
AUTHORITY\SYSTEM can have manageability issues due to windows file
system redirection. Instead use a normal windows account as the owner.
HOWEVER, I didn't choose to run under SYSTEM. For some reason it switches to this user, while after the installation when the application is running with the same connection string, it uses the normal Windows user as expected.
Why is it switching to this user? That's what I'm trying to figure out. I have windows 10 if it matters.

VS application can't connect to database after attaching it in SSMS

I have a windows form application (developed in VS Express 2013) that connects through an instance of SQL Server 2012 to a database. Both the application and database are on my local system; I just needed a GUI to more easily interface with this very large database that stores my research data. When I initially compile and deploy the application, it works fine and has no connection problems with the database. However, if I then attach the database in SQL Server Management Studio (which I sometimes want to do) I get an error the next time I try to use the application - "Cannot open database ..... requested by the login, the login failed". I get this error even if I take the database offline and detach it before quitting SSMS. And just to be clear - I'm not making any changes to the database in SSMS, I'm just looking at the data. The connection string used by VS is Data Source (LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\CollectionMetricsDatabase.mdf;Integrated Security=True.
Any idea what the problem is?
Maybe the instance of SQL Server 2012 has a max number of concurrent connections and you exceed that with your two connections at once (SSMS connection and application's connection). Check the settings in SSMS.
Another thing to try - Based on your connection string, you are using Integrated Security, which in my experience means the application connects using Windows credentials. That can be fragile, because it means that whatever 'user' runs the application (if hosted in IIS, this will be the App Pool user) is the one whose Windows credentials are used. I would suggest creating a SQL login user (in SSMS) for the database, with db_owner access, and then changing the application's connection to be username/password based instead. You may need to enable Mixed Authentication for the SQL Server instance in SSMS if it doesn't already allow it.
You need to connect in SSMS with the localDB connection string. Attaching it will prevent it from working with localDB.
In SSMS, create a connection to (LocalDB)\v11.0. You should see your DB there already.
The problem is that you are connecting to the database file directly not through SQL Server.
Only one user can have a lock on the file at the time.
You need to recreate a connection string for your app.
That is why you were needing to detach the database.
Since this is only a personal project you could live with things as they are, at least you know the server is only running when you need it.
create connection msdn

MS Access/accdb "Cannot open database" error (C#)

I'm using an MS Access database to store data for a desktop application. This week I was asked to give access to the application to some additional users, but whenever they launch the application they are getting the error message:
Cannot open database ''. It may not be a database that your application recognizes, or the file may be corrupt.
I have gone through 5 pages of Google search results trying to find a solution and come up with nothing. Every answer I can find to this problem is either the 2GB limit (which my db is nowhere near) or corruption (which is not the case because only two new users are receiving this error and everyone else is fine).
This is my connection string:
"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=[Path];Jet OLEDB:Database Password=[Password];"
I'm pretty sure this is due to the users' machines missing something, but I cannot figure out what or where they can get it from. Please advise.
Your connection string indicates that you are using the Microsoft.ACE.OLEDB.12.0 driver.
This must be installed on the machine, as it doesn't come with windows.
You can find the driver in the Microsoft Access Database Engine 2010 Redistributable
If the error is Cannot open database . It may not be a database that your application recognizes, or the file may be corrupt, make sure that the Database file (ACCDB/MDB) is not more than 2GB because that's the limit for any Access Database File.
Reference Here

C# & .NET Checking Database Connection

I want to check availability of database connection so that I could put my application in specific modes; online mode and offline mode.
In order to do so, I try to open an OleDB connection (the database is Oracle) and if it is successful, the application shall run in online mode. However, if the database is down, opening the connection shall only be closed after specific period of time due to timeout.
Is there specific way of doing this without having to wait for the timeout? Or maybe, specify the timeout interval?
There is no other way then connecting to the database service.
However you could Mmdify the ConnectionTimeout property of your Connection instance before opening it to tweak the amount of time you want to wait.
On the other hand you could always start with the offline mode, do the connection check async and change the application behavior when the check was succeeded.

C#: How to graciously manage errors on the application DataBase?

I have a C# application that uses a localhost DB (MySQL).
Now, when I create the executable I´m assuming that the receptor computer MUST have the exact DB with the the same name and tables, also, must have running WAMP or XAMPP.
If one of this conditions is not accomplished the program will crash horribly, with the errors of Windows/C#.
I could put exceptions for every case, but I´m fearful that I would hide other errors putting exceptions for everything!
With production software, how do you manage this? With exceptions? Writing a manual for the user? etc?
During bootstrapping, I recommend check to see if a DB Connection can be created (in my case, SQL Server), given the database connection string defined in an app.config. Initially, you should do some version checking on the database. If the database can't be found, attempt to create it. if i'ts out of date, attempt to upgrade it. If this process fails, then your database engine instance isn't installed or is unresponsive. For my application case, I exit the program, as there's nothing else to do if the DB can't be accessed.
Once past this point, I generally assume that the DB connection is active.

Categories

Resources