how to disconnection access in my C# program? - c#

I tried to compress my Access 2007 database like this:
System.Diagnostics.Process.Start(#"C:\Program Files\Microsoft Office\Office12\msaccess.exe",#"c:\Mydb.mdb /compact ");
In my C# program and I get this error:
You attempted to open a database that is already opened exclusively by user on machine . Try again when the database is available. (Error 3356)
I tried Conn.close() but still get the error
Thanks in advance.

Maybe connection pooling is the issue?
Not completely sure where connection pooling is implemented (I believe that this is just part of the provider e.g. SQL server and therefore might not be relevant to this issue)
But it will keep connections open between the client and server even thougth the connection object is set to "closed".
You can clear a sql server connection pool using conn.ClearPool
What are you using for the provider to connect to your database? (OLEDB?)
If so try ReleaseObjectPool()

Check your Task Manager to see if you have Access already open. If you do, close these instances and retry.

Maybe something went wrong and you just have an extra *.ldb file out there. Go to where your DB is stored:
c:\Mydb.ldb
If no one is using it delete the file.

The way compact & repair works in Access is it compacts to a new file, deletes the original file and then renames. You can use the DAO Engine to do the same thing. As far as I can tell from MS Visual C# 2010 Express, you can set a reference to DAO and use that:
var MyDBE=new DAO.DBEngine();
MyDBE.CompactDatabase("c:\\docs\\Mybe.mdb", "c:\\docs\\temp.mdb");
To ensure the database is not in use, you can check if the .ldb exists.
I am sorry, but I do not know anything about c#, so these notes are very crude.

//Edit begin
Check out these basic troubleshooting items:
http://office.microsoft.com/en-us/access-help/troubleshoot-compacting-repairing-or-recovering-an-access-file-HP005188316.aspx?queryid=728e1007c19b43bba069cb7f11364f7a&respos=0&CTT=1
See if any of the above reasons are causing the compact to fail!
//Edit end
Check the following link:
http://support.microsoft.com/kb/209207
Use /excl with the code to open it exclusively.
So code would be:
System.Diagnostics.Process.Start(#"C:\Program Files\Microsoft Office\Office12\msaccess.exe",#"c:\Mydb.mdb /excl /compact ");
By using the excl option you will be opening it exclusively!
Hope it helps!

Related

Local DB in Visual Studio 2015

After a few years, I have returned to writing in C# and I am really struggling here - I would like to have my app to have a local SQL database. I have added Service-based database and "Database1.mdf" was added to my project. I created a table and added some data just to see if it is working but I cannot connect to it. I tried numerous connection strings with no success (Server not accessible).
Do I need to run something else in the background? I thought that I might have a local database and with .NET client I can access it, and I hoped it would work whenever I bring my application (also not requiring any SQL server running). Is that wrong?
If you don't require any SQL server, take a look at SQLite. This is lite SQL database engine. Database is just one file. C# has a great library to SQLite on NuGet: https://www.nuget.org/profiles/mistachkin
SQLite is widely used, event in Android (as a native db engine).
here is what i use to connect. it appears as a Data Connection in Server Explorer.
string con2 = #"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=" + Application.StartupPath + "\\jobfile_2017.mdf;Integrated Security=True;Connect Timeout=30";
when i first started working with these, i used this source 1
it works on PC's that i have nothing installed (not even office) but as i said i'd be interested to know of any shortcomings of this method
I experiencing same problem and decided to move mdf file to static c:\db directory. Connection string was changed to incorporate new file location (AttachDbFile).
But AttachDbFile has some issues also (by some reason one table in db is inaccesible and error is access denied).
So I decided to move to Sqlite. LocalDb has many issues to work with. I read good note to resolve problem: in command line stop/remove/start name of instance. But it nuissance.
Wish you good luck to work with these db files.

Firebird 1.5 / Asp net VS 2010 (legacy database .CDB extension)

I have an old system which generated me a database in .CDB extension (i run on Firebird-1.5.6.5026-0-Win32) and i can access this database in IBExpert to query and stuff. But i need to write an application in .NET (VS 2010 4.0 framaework) so i can read this database and access some of the data to insert into a table inside SQLServer.
I tried many things, changed the server version and other things but i now all i get is ''Cannot find fbembed.dll'' exception error while trying to open the connection. My FB server doesnt have this file since he uses the 'fbclient.dll' already.
Any thoughts on how to connect my application to this .CDB database?
(this firebird version is the same that the legacy system is running, so i used the 1.7RC firebird .net provider within this server)
The connection string used is:
<add name="FirebirdConnectionString" connectionString="User=SYSDBA;Password=masterkey;
Database=localhost:C:\temp\BD\E‌​CLECTIC.CDB;DataSource=localhost;Port=3051;
Dialect=3;Charset=NONE;Role=;Connectio‌​n lifetime=15; Pooling=false;
MinPoolSize=0; MaxPoolSize=50; Packet Size=8192; ServerType=1;"
providerName="FirebirdSql.Data.FirebirdClient"/>
Unless you really want to use Firebird embedded (which you don't as you also specify localhost), you should not specify ServerType=1, but either leave it out entirely or set ServerType=0.
As to your other problem you mention in the comments, I suggest you check if this solves it and otherwise create a new question with more information.

C# creating a database programmatically with SMO

I am trying to create a database, but once created, I cannot connect to it.
The server is Microsoft SQL Server 2008 and using .Net 4.5. We're creating the database with SMO, but we're usually using Dapper to connect and query the database.
This is the code I have so far, which works :
System.Data.SqlClient.SqlConnection con = new System.Data.SqlClient.SqlConnection(connectionString);
Microsoft.SqlServer.Management.Smo.Server srv = new Microsoft.SqlServer.Management.Smo.Server(new Microsoft.SqlServer.Management.Common.ServerConnection(con));
var database = new Microsoft.SqlServer.Management.Smo.Database(srv, dbName);
database.Create(false);
database.Roles["db_datareader"].AddMember(???);
database.Roles["db_datawriter"].AddMember(???);
database.Roles["db_backupoperator"].AddMember(???);
srv.Refresh();
Noce the ??? ? I have tried
System.Environment.UserDomainName + "\\" + System.Environment.UserName
and
System.Environment.UserName
but it fails (update) with the error Add member failed for DatabaseRole 'db_datareader'. with both values.
The problem is that when I create the database, I cannot coonect to it for some reason (using Dapper), from the same program. (update) I get the error message : Cannot open database \"<database_name>\" requested by the login. The login failed.\r\nLogin failed for user '<domain>\\<username>' (where <database_name> is the database name, <domain> my logon domain, and <username> my Windows logon).
Am I missing something? Am I doing th right thing? I've tried searching the web, but it seems no one creates database this way. The methods are there, it should work, no?
** Update **
If I comment the database.Roles["..."].AddMember(...) lines, and I add a break point at srv.Refresh(), resuming the program from there solves everything.
Why a break point solves everything? I can't just break the program in production... nor break the program when creating the database everytime.
It sounds like the Dapper connection issue is a problem with SQL Server doing some of the SMO operations asynchronously. In all likelihood, the new Database is not ready for other users/connections immediately, but requires some small time for SQL Server to prepare it. In "human-time" (in SSMS, or a Breakpoint) this isn't noticeable, but "program-time" it too fast, so you probably need to give it a pause.
This may also be the problem with the Role's AddMember, but there a a number of things that could be wrong here, and we do not have enough information to tell. (specifically, does AddMember work later on? and are the strings being passed correct or not?)
This is happening because you've created the user, but no login for that user. Though I don't know the exact syntax, you're going to have to create a Login. You'll want to set its LoginType to LoginType.WindowsUser. Further, you'll likely need to set the WindowsLoginAccessType to WindowsLoginAccessType.Grant and you'll need to set the Credential by building one, probably a NetworkCredential with the user name you want.
To put a visual on this, the Login is under the Security node for the Server in Management Studio whereas the User is under the Security node for the Database. Both need to exist for access to the SQL Server.

connect to .mdf file in vs2010 , wpf application

I am using VS2010 , and I built a .mdf file using SQL server 2008
I want to use this database file in my wpf application so that I can add rows to it and delete rows from it
the problem is , I can't access this file , and all the insertion and deleting is actually hapening to the datacontexct i created .
I used myDataContexct.Submitchanges() but it didn't work either
I tried to add a connection string when I define the datacontexct that holds the url of my .mdf file and this it gave me a runtime error when trying to access this file and the error messege says :
An attempt to attach an auto-named database for file Trial.mdf failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share.
please help me because I searched alot but I couldn't find any help
If the application is not going to be installed in a manner that many clients are accessing the same server, you would want to consider using SQL Server Compact Edition.
Are you sure the connection string in your app.config refers to the local mdf file? perhaps it refers to the server instance?
What technology do you use, is it LinqToSql or Entity Framework (I think you have t call SaveChanges, not AcceptChanges)?
If you do intend to access the server instance, then the problem seems to be a security restriction.
Please add more details on statement no. 1, and I'll write further info.

SQL SERVER 2005 Automatic Schedule Backup Working not Properly

I am having a web server with install of WINDOWS 2003 Server and SQL SERVER 2005 Currently and i try to do a job schedule with the use of some control that i created. every thing is working properly but when it starts the job (in job i have written T-SQL Command for Backup Database) it will gives following error. i already check that i got the root directory path. and i have all the permission on that folder and i am as a DBOWNER of my database. but still i got error if any one or any person having idea regarding my error please help me immideately. please
****I am here gives path that is root directory of my Application ok****
MY ERROR :
Cannot open backup device 'C:\Inetpub\httpdocs\Full_BackUp_at21032009_092140AM.bak'. Device error or device off-line. See the SQL Server error log for more details. BACKUP DATABASE is terminating abnormally.
So, Please Help me if you having any ideas regarding why this error cause then if u wish to send solution directly me on this mail id : divyeshnh#gmail.com
Thank you
Sorry But I have whole rights that i wants at the root directory because i save my image files into this directory . so, you can understand that i have the rights.
Thank you for reply
i also do another thing :
i read yr answer very carefully & study all the things that u previously say. after that i try another thing that is i place that code on one button click event and fortunately i got success. so, i ask you when i try to do with schedule it will raised me an error while i click on button & take backup it will work fine. so, please give me answer about this.
Thank you for your reply
Thanks
Do the accounts used by SQL Server and Agent services have permissions to access the C:\Inetpub\httpdocs folder?
Are all the folders you your path already created? I think they have to already exist or you'll get an error.

Categories

Resources