connect Access via c# - c#

i need to open a connection to a remote access db.
in the local environment to the remote acess db is working great .
when i run this application from production server (other server) it's fail with message
"
It is already opened exclusively by another user, or you need permission to view its data.
"
my code :
conString =
#"Provider=Microsoft.JET.OLEDB.4.0;"
+ #"data source=" \\150.248.248.38\d$\TestApp\vending.mdb;Jet OLEDB:Database Password=1234;";
OleDbConnection connAccess = new OleDbConnection(conString);
try
{
connAccess.Open();
objDiningRoom.Connection = connAccess;
....
}
catch (Exception ex)
{
}
finally
{
connAccess.Close();
connAccess.Dispose();
}
*Its not open in other place
thanks

This looks like it is a permissions problem.
Make sure you give the IUSR account (or whatever account ASP.NET runs as) read/write permissions to your database.

you can try :here
copied from there :
This commonly occurs when your database file is opened exclusively
by another application (usually MS
Access). Close all applications that
use this database and try again.
This error may occur if the account being used by Internet Information
Server (IIS), (usually IUSR), does
not have the correct Windows NT
permissions for a file-based database
or for the folder containing the
file.
Check the permissions on the file and the folder. Make sure that you
have the ability to create and/or
destroy any temporary files.
Temporary files are usually created
in the same folder as the database,
but the file may also be created in
other folders such as /Winnt. If
you use a network path to the
database (UNC or mapped drive),
check the permissions on the share,
the file, and the folder.
Check to make sure that the file and the data source name (DSN) are
not marked as Exclusive.
Simplify. Use a System DSN that uses a local drive letter. Move the
database to the local drive if
necessary to test.
The "other user" might be Visual InterDev. Close any Visual InterDev
projects that contain a data
connection to the database.
This error may also occur when accessing a local Microsoft Access
database linked to a table where the
table is in an Access database on a
network server. In this situation,
please refer to the following article
in the Microsoft Knowledge Base for a
workaround: Q189408 PRB: ASP Fails to
Access Network Files Under IIS 4.0

Related

Can't open databse requested by login, login failed, login failed for user

I have developed c# WPF application, I am using MDF file for database,
my connection string looks like :
Data
Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\appDataBase.mdf;Integrated
Security=True;Connect Timeout=30
here I am replacing |DataDirectory| with user's appdata folder.
the database file is in user's appdata folder, I created a setup which
copies .mdf file to _users appdata folder when I install it in
client's computer, it works perfectly
but when I uninstalls and
reinstall it, I am not able to connect that file, getting the error:
Can't open database
"C:/users/myusername/appdata/roaming/myapplication/database/appDataBase.mdf"
requested by login, login failed, login failed for user..
File is there, but somehow it is used by SQL server and can't
open it. also while starting application every time I am creating
backup of that file, that one also failing with error
System.IO.IOException: The process cannot access the file
'C:\Users\myusername\AppData\Roaming\myapplication\Database\appDataBase.mdf'
because it is being used by another process.
Can anybody give me exact solution, because i have to use localdb only on my client computer , i don't want them let to do any extra configuration for installation of my app, i.am still facing iasue after i changed my code to close db connection on main window closing event.
This type of things happens because one of your object which is using that database is still running in the background so please dispose all the database related instance also dispose and close all the database related object in catch().
Try catch will help you to detect exact line number where you are getting error and also show due to which object you are getting error.
If you want to check why this is happening then open task manager and look out for services there you definitely find service which is holding object for database.

c# SQL) unable to open physical file(MS localdb .mdf) after attaching the .mdf file which was created in another computer(server)

I'm developing Windows Desktop appication(C# WPF) with Microsoft localdb(.mdf) and I want that users of my software can carry their localdb (.mdf) file when they move to other places(computers). The localdb (.mdf) file is created on the first computer.
To test my application, I copied my localdb file from computer A to computer B and attached with below code successfully.
string attach_greendbnameQuery = string.Format(#"EXEC sp_attach_db #dbname = N'greendb_{0}', #filename1 = N'{1}\greendb_{0}.mdf'", textBoxGreenLogin.Text, Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData).ToString());
SqlCommand attach_greendbnamecomm = new SqlCommand(attach_greendbnameQuery, check_datadbinmasterConn);
attach_greendbnamecomm.ExecuteNonQuery();
And I could read and write data into the moved localdb file.
However, when I run backup command on this database, exception occurs like,
'Unable to open physical file, Operating system error 32, process cannot open the file because the file is in use by another process'
If I enter Server Management Studio- Security-KayLee-PC\KayLee- User Mapping,
the users of all other localdb are 'dbo' but only the user of manually moved database is KayLee-PC\KayLee and only 'public' is checked and all other database roles are not checked including db_owner. (I always start(login) Windows(O/S) with KayLee-PC\Kaylee account)
I tried to make all roles checked to database roles even to server roles but failed.
Even, I tried to drop the user KayLee-PC\KayLee through below code but the exception message show as,
'User 'KayLee-PC\KayLee' does not exist in current database'
If I click the database in Server Management Studio, the current database status is not changed to clicked database and subtree nodes(like Table, Security and so on) are not displayed with message 'cannot access to the database' eventhough if I click other databases, the current database status is changed to clicked database.
Also, I tried to change the owner of the localdb(database) through below code but it seems the execution failed with result '-1' with 'sa' and when trying with 'KayLee-PC\KayLee' which I always login to Windows O/S, error message is like 'KayLee-PC\KayLee is already an owner(exist) of this database'
SqlConnection dbownerchange_Conn = new SqlConnection();
dbownerchange_Conn.ConnectionString = dbownerchange_ConnectionString;
dbownerchange_Conn.Open();
SqlCommand dbownerchange_comm = new SqlCommand();
dbownerchange_comm.Connection = dbownerchange_Conn;
dbownerchange_comm.CommandText = "EXEC sp_changedbowner 'sa'";
dbownerchange_comm.ExecuteNonQuery();
dbownerchange_Conn.Close();
Simply, If we need to move(copy) Microsoft localdb file to another place(computer), how can I do this successfully?
Must we detach before move the localdb file? If so, I'm worried there're always people who don't follow guideline by running detach function.
I've tried several scenarios to understand how SQL server is working. The conclusion is we need to detach first and re-attach to same or different machine(computer). Then, I could have been successful to move to another computer.
However, a single process of backup and restoring localdb to another machine(computer) doesn't work. Furthermore, if we detach localdb(database file .mdf) first, SQL server no more recognizes the localdb database and we cannot run backup command for the localdb.
Conclusively and simply, if we want to move localdb (microsoft database .mdf file) to other local server(computer, machine), we're needed to just detach the localdb from master database in computer A and copy the files and re-attach to another master database of computer B.
Hope this helps someone else..

Connect to remote Access 2003 database which has linked tables

I've got this scenario: I developed a C# application which connect to a mdb file (Access 2003) through OleDBConnection (engine = Microsoft.Jet.OLEDB.4.0). This mdb file has some linked tables in another mdb file in the same directory. Everything works perfectly in the local environment, the query retrieves data from linked table even if I connect only the main mdb file. A simplified schema could be:
my application in the local machine --> C:\mydir\main.mdb --> C:\mydir\linked_tables.mdb
But this application should work in some host computers of a local network. And the mdb files are stored in a server in a shared directory. Well, when the application is running in a host I set the path as follow:
\\myserver\mydir\main.mdb
The connection works. The problem happens when I launch a query which tries to get data from linked tables. It tries to find the linked table in C:\mydir\linked_tables.mdb, but this path is on the server, not in the host.
Is there a way to tell him: if the path of the main mdb file is \\myserver\mydir\main.mdb you must (automatically) get the linked tables on \\myserver\mydir\linked_tables.mdb?
Thank you
I solved with another connection. I mean: for each mdb file I use a different OleDBConnection.
OleDbConnection MainConn = new OleDbConnection("\\myserver\mydir\main.mdb");
OleDbConnection LinkedTablesConn = new OleDbConnection("\\myserver\mydir\linked_tables.mdb");
Not so elegant, but it works. If I have to do some join of tables located in different databases, I manage them with some generic collection objects.

What exactly is this error?

I was trying out making a website with asp.net to see how data binding works but when I launch the application, it throws this error.
Failed to generate a user instance of SQL Server due to failure in
retrieving the user's local application data path. Please make sure
the user has a local user profile on the computer. The connection will
be closed.
I checked the internet and I found a lot of possible solutions but none of them worked for me.
I created a custom database in the App_Data folder of the project and the connection string is
Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True
It cannot locate the data file at the specified path, or the user does not have sufficient privileges to access the data file.
If the database is set up for Windows Authentication Mode, you should try changing your connection string to something like this:
Server=<COMPUTER NAME>\SQLEXPRESS;Database=<DATABASE NAME>;Trusted_Connection=True;
OR
Server=<COMPUTER NAME>\SQLEXPRESS;AttachDbFilename=<DIRECTORY PATH>\<FILE NAME>.mdf;Trusted_Connection=True;

Having trouble connecting C# executable to database file on remote computer

Using VC# I've created a staff management app that, upon its first run, is expected to query the user for the path to a (.mdf) database which will reside on a remote computer. A resulting path may be something like
string dbPath = #"P:\remoteComputer\public\StaffTool\ExamplePersonnelDatabase.mdf";
Then I'm placing this string into a connection string template as so:
string dbConnectTemplate = #"Data Source=.\SQLEXPRESS;AttachDbFilename={0};Integrated Security=True;Connect Timeout=30;User Instance=True";
string dbConnectionString = String.Format(dbConnectionTemplate,dbPath);
Then I'm attempting to connect to the database LINQ to SQL style
ManagementDBDataContext db = new ManagementDBDataContext(
dbConnectionString);
At this point, an error pop's up stating that
The file "P:\remoteComputer\public\StaffTool\ExamplePersonnelDatabase.mdf" is on a network path that is not supported for database files.
An attempt to attach an auto-named database for file P:\remoteComputer\public\StaffTool\ExamplePersonnelDatabase.mdf failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share.
As I am relatively new to databases, I completely do not understand this message. And obviously, the file is not on a UNC share.
Any ideas?
Jim Lamb recommended that I connect to an instance of SQL server running remotely. Considering that I'm using LINQ to SQL, what refactoring do I have to do to make this happen? Other ideas still welcome - especially "push this button and everything will work" solutions.
Another clue: A coworker said that there used to be some way to work through Control Panel->Administrative Tools->Data Sources(ODBC) so that a remote database could be viewed from my computer as if it was local. The coworker didn't know any more details besides this.
You are attempting to connect to a database file on another machine over a network connection, which isn't supported by SQL Express. You'll need to make a local copy and attach to that, or connect to an instance of SQL that's running on the same machine as the MDF file.

Categories