Sql Server 2014 Express Edition database backup from c# - Sql Error - c#

I am using Sql Server 2014 Express Edition. I am using Visual Studio 2015.
I have created a c# Winform app that uses a database. In the app, I have a button that is suppose to backup the database. On the button click event, the sql command
"backup database testdb to disk = #fullfilename";
is executed using sqlcommand and ExecuteNonQuery.
When I test this out, I get an error:
Failed to backup databaseSystem.Data.SqlClient.SqlException
(0x80131904) Operating system error 3(The system cannot find the path
specified).
I am testing the backup trying to get it to save to a mapped drive. I have looked on google and tried the approached recommended. I have change the login service for Sql Server to a domain user. I have mapped the drive using that same domain user's credentials. I have verified that the user has all permissions to the share,etc. No joy. Same problem.
Would love to hear some pointers on how to get this to work. Note: it works just fine if you use a local, non mapped drive.
Note: It must use mapped drives or UNC's. Its a requirement.
thanks.

Backup to UNC is supported and should work without problems.
My troubleshooting suggestions:
Use UNC instead of mapped drive and make sure the destination folder exists prior to starting the backup
Copy the SQL command into SSMS and manually start the backup
If backup via SSMS works and your app still fails, startup SQL Profiler and look at the SQL command your app sends
Good luck!

Related

Database backup of azure database deployed in local sql server management studio

Can anyone help me in doing this task? I am using my sql azure database in my local machine's sql server management studio 2008 r2. What my issue is, I am trying to take backup of a database from my c# console application using the following methods:
using smo: showing error at "sqlBackup(server)" method.
The error details like -
System.IndexOutOfRangeException: Index was outside the bounds of the array.
at Microsoft.SqlServer.Management.Smo.SqlPropertyMetadataProvider.PropertyNam
eToIDLookupWithException(String propertyName, PropertyAccessPurpose pap)
at Microsoft.SqlServer.Management.Smo.SqlSmoObject.GetDbComparer(Boolean inSe
rver)
at Microsoft.SqlServer.Management.Smo.SqlSmoObject.InitializeStringComparer()
at Microsoft.SqlServer.Management.Smo.AbstractCollectionBase.get_StringCompar
er()
at Microsoft.SqlServer.Management.Smo.SimpleObjectCollectionBase.InitInnerCol
lection()
at Microsoft.SqlServer.Management.Smo.SmoCollectionBase.InitializeChildCollec
tion(Boolean refresh)
at Microsoft.SqlServer.Management.Smo.SmoCollectionBase.GetEnumerator()
using "backup database" command, this showing command not supported in this version of sql server. After searching in internet, i found that this command wont support for azure databases in ssms.
Can anyone please provide me solution to solve this.
You can not backup the Database in Azure Sql Service. When try to Backup Shows this Error
Statement 'BACKUP DATABASE' is not supported in this version of SQL Server.
So Backup Azure SQL Service by
Right Click on Db --> Tasks --> Export data Tier Application --> Set Location
Creates a bacpac file after export, then it can be import via this
Note: Be aware there are export and extract. The extract option only copies the schema, so if you need the data as wel make sure to use the export variant.
For security reasons, SQL server instance doesn't allow database migrations between two different target servers such as local and azure or vice versa.
The below tool can help run the scripts on one server to another. Keep in mind that when you run them it restores the schema and the data fully. You can alter the settings in Advanced option.
https://sqlazuremw.codeplex.com/

Visual Studio 2012 move local .sdf file to server computer

here is the problem I am facing now. I have created an application that uses local database (this was created by Add -> New Item -> Local Database. Afterwards I have added tables under this .sdf database.
Then I have connected to this database using Add -> New Item -> ADO.NET Entity Data Model.
Everything works like a charm, unless I was asked to move this database to a place, where multiple people could access this database and work with it.
Therefore, as I have no previous experience with databases, I have treated this .sdf file as any other file (let's say Excel workbook) and I thought that I could simply take already existing database, copy it on server computer (e.g. \Server001\Database\Database1.sdf) and simply change connection string under app.config.
However the problem is that this does not work. As I didn't know how to change connection string, I created new application, where I have tried to connect to this database located on a server computer; however I received the following error:
SQL Server Compact does not support opening database files on a network share.
I already have fully functioning program, but I have no idea how to make it work with multiple users. I have tied to google for solution, but all I could find is how to create local database, not how to make it accessible by placing it in server computer.
Could you guys please help me? If you need more details, please let me know!
P.S. This is WPF application, .NET 4.5, created using Visual Studio 2012 Professional.
Thank you!
The error message pretty much sums up the problem: SQL Server Compact does not support opening database files on a network share.
SQL Server Compact (aka "local database") is to be consumed by a local application; even if it was a web app serving many requests, the application itself is local.
If you want to have multiple remote connections (i.e. centralized DB, distributed app), you should look at using an instance of SQL Server (any SKU would probably work, even SQL Server Express). Those will use MDF files instead of SDF files, so you might want to refer to Convert .sdf database to .mdf database. You'll probably also need to set up a user identity for your connection string, so check out this link on CREATE USER and Difference between a User and a Login in SQL Server to understand how that can be configured.

Connection String in C# windows application

I am developing C# windows application first time.
Que 1 - ) I have developed my windows application which uses SQL server 2008 using below connection string..
Data Source=myMachineName;Initial Catalog=myDataBase;User Id=myUsername;Password=myPassword;
But When I created setup of my application and installed it to another computer then it is throwing error of could not establish connection with SQL server..
I guess this error is because of my connection string as it is specific to my computer and installing it to another computer will not work..
Hence How can I make my connection string which will work with any computer..
Que 2 - Do I need to attach schema file with my setup as How my database will be created into the other computers??
This might be silly qustion but as I am doing this first time , i dont know about this all.
Regards,
Mahesh
You should place your web config in app.config file which is in xml format.
And read it from your code.You can easily change app.config file through notepad according to machine.
Yes must have your schema script in setup if you want to create it from setup.Otherwise you create database manual on client machine.
If you've used SQL Server on your development machine, it has to be installed on every computer that will be using your app. There are several options to resolve this issue:
If your app doesn't need access to a central sql server (for example your app just keeps track of users DVDs), use SQL Server Compact Edition instead and embed it to your app
If your app needs some kind of a centralized data, get a dedicated SQL server first, install there everything you need and in your app change your current myServerAddress to the address of the server. Note that the server needs to be accessible from remote locations (some restrict access to localhost!).
Que 2 - Do I need to attach schema file with my setup as How my
database will be created into the other computers??
Again, multiple options. For instance, you can export your database to a .sql file (Right click on the db in your DB explorer and select option Publish to provider) and then import it using a sql manager on other computers. Or maybe you can create a .cs installation script, that will do the same job using C# code.
Edit based on OP's comments:
Example links for solutions:
question1: Embedding SQLServer CE in an installer
question2: http://support.microsoft.com/kb/307283
Okay that's fine but will my current Connection string work in that case as the DataSource
is my specific machine name
Of course not, you'd have to change it to point to the SQL Server CE database file. Probably the best way is to not hardcode it, but use a relative path to the database. It looks like this for instance:
Data Source=|DataDirectory|\MyDb.sdf, where |DataDirectory| points to the App_Data folder of your application.
Also plese see the error screen map i have attach with the question..
Your error is quite clear - there was a problem establishing a connection to your database. It either doesn't exist or isn't accessible from your current machine.

Keep database file in different partition

I have created my first project in C# which uses sql server database (file name: printing.mdf)
I have created a database on Sql Server Express and developed an application to manipulate that database.
After deploying (installing sql server on the client's pc, then attaching database manually) everything works fine.
Real problem is that I want
My application should install sql server automatically during installation.
Database (printing.mfd) file should also be attached in installation process.
MOST IMPORTANT: I want to save mdf file on D: drive, so that when system drive (C:) is formatted for any reason, my database should not lost.
For last problem I have tried this connection string
Data Source=.\SQLEXPRESS;AttachDbFileName=d:\printingData\printing.mdf;Integrated Security=True;User Instance=True
I also copied printing.mdf to d:\printingData
This worked on my pc but not on clients pc
Any suggestions specially related to 3rd one.
You can't install SQL Server automatically. Even if this were possible, it would be highly UN-recommended as lots of system tests/configurations are done during the install.
You can attach database files to an existing SQL Server instance at install-time using a custom installation package. NSIS is the best freeware I have come across for installation and it is very well documented and supported (by the users etc.). The functionality you want for database attachment is one of the plugins found here on the NSIS Wiki pages
Of course the D: partition you may have on your PC may not exist on another user own machine. Also, there is not way of automatically creating such a partition on a user’s machine. To do, or attempt to do this would be very intrusive.
In my opinion, I would create some documentation specifying the recommended configuration for your software. You could also provide the installation instructions documenting how to partition the hard drive. But you will find that users (casual users esp.) will not want to be doing such things!
Edit: To answer your comment. For installation from the command prompt in see the following MSDN article - good luck, and be carefull attempting to automate this, it is NOT a good idea...
To attach a database using TSQL use the following queries:
-- For an .mdf with no log file - rebuild it.
CREATE DATABASE [{0}] ON ( FILENAME = N'{1}' ) ATTACH_REBUILD_LOG;);
-- For a standard attach.
CREATE DATABASE [{0}] ON ( FILENAME = N'{1}' ) FOR ATTACH;
Note you can use a system stored proceedure to attach databases but this is being phased out by Microsoft.
Hope this helps.

C# application accessing remote sql server 2008 instance ... not working

I have a console app that needs to connect to a remote sql server 2008 instance....
this particular line throws an error even though i have access to that database...
connection.open() is the line that is throwing the error...
Make sure that Sql server 2008 instance available at your system where you are running your application.
Atleast client version of Sql server 2008 should be installed on development machine.
Check connection string and server system also allow network connection.
Make sure the connection string in you console app uses the same credentials and settings that you use to successfully connect in management studio.
Are you using Windows authentication in one place and a SQL login in another?
Have you been able to connect to this remote server before? If you have been able to connect previously, but cannot do so now, that may be an indication that a backup copy of the database, from another server, has been restored to that server.
When you write that you "have access" to that database, how do you connect? Close your copy of SQL Server Management Studio and re-open it; when you get the Connect dialog, be sure to enter the user credentials that your console app uses to connect. Do you see your database? Are you able to execute stored procedures, run SELECT statements, or otherwise do what you would expect?
If you do not see the database--or if you cannot do the activities you expect, you have probably restored a database from another server. This is very common--a database developer is working on the next revision of the database on his development machine; when it comes time to deploy he backs up the database, copies it to the server, and restores the database. Now he can't connect--and can't understand why.
The reason is that the Login and User are presented in the SSMS UI as strings--but in fact they are integer IDs. You will have to do a bit of scripting to delete the invalid User record from the database in order to assign it to the new server's Login.
If this sounds like your problem, respond--I'll check back in a bit, and follow up with more help.
JM

Categories

Resources