DB Backup runs in Debug but not Release - c#

I have a small c# wpf app for doing some simple calculations running off a Sql Express 2008 R2 db, and in the setup section is a backup button that runs the code
using (DTZDataContext db = new DTZDataContext())
{
db.ExecuteCommand(string.Format("BACKUP DATABASE DtzDb TO DISK = '{0}'", filename));
}
The connection string I am using is
Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\App_Data\DTZ.mdf;Integrated Security=True;User Instance=True;Database=DtzDb
I have confirmed there are no other open db connections at this point in the application and the backup runs fine in debug, but once I compile for release (running from VS or as standalone) I get the error:
Operating system error 32 “32(failed to retrieve text for this error Reason 15105)” BACKUP DATABASE is terminating abnormally.
How can I fix this, preferably without having to install Sql Management Studio on each machine and attaching the DB? What is the recommended way of doing backups? Why does it work in Debug but not Release.
Many Thanks

My bad... It turns out VS had changed the DataContext connection string to DTZConnectionString1 and made another settings file. Now I am using the correct connection string it is working fine.
No idea why the incorrect one worked in debug but not release.

Related

SQL Server network interfaces error 52- Unable to locate a database at runtime installation

I am creating a POS system in Windows Forms (C#) in which I use a SQL Server database file (.mdf) to store items (completely offline). When I install the application on my computer, it works fine, but when I install it on my clients PC, an error happens:
(provider: SQL Network Interfaces, error: 52 - Unable to locate a Local Database Runtime installation"
I read somewhere that the problem is caused due to the fact that the connection string of the database of my client's PC is different. I tried to add the connection string dynamically on runtime but again it only worked on my computer.
Another reason that might be causing the problem is that I used 'server-based database' since local database option isn't available in Visual Studio 2017 for some reason.
Another solution I looked up stated that I should install SQL Server Express on my client's PC. That also failed (maybe I have to set it up in a way or something).
I also tried adding the database.mdf and database_log files in the setup folder.
Lastly I tried installing 3rd party installers (Advanced installers 15.8 and InstallShield Wizard in VS 2015) which also failed.
(I have provided the code for the connection of database taking place and the connection string)
public void ConnectToDB()
{
DBConnection = new SqlConnection(#"Data Source = (LocalDB)\MSSQLLocalDB; AttachDbFilename=C:\Users\SAIM NASSER\Desktop\app layer\data layer\Database1.mdf; Integrated Security = True");
DBConnection.Open();
ResultSet = new DataSet();
}
If I understand you correct, you want to use LocalDB
That means using Sql Server without installing a full sql server, but just the localdb part from sql server express.
For this to work you need to install the LocalDB Driver, which can be found here
https://www.microsoft.com/en-us/download/details.aspx?id=29062
You need only the ENU\x64\SqlLocalDB.MSI
This is the only thing you need to install in your clients computer, I believe it can also be installed silent, you have to research a bit for that.
And yes, you also should change the connection string on the clients computer, you need to alter it so it points to the MDF file on the clients computer, because that location will probably be different then on your computer
EDIT
To get the connection string working, you can try this
On the clients computer, create a text file and rename the extension to .udl
So for example you have a file test.udl
Now from explorer, double click it, this will open the datalink editor.
From here you can enter/choose different settings, and click on the test connection button.
Once you get a working connection, save it, and open this file with notepad.
Inside you will find the working connection string
Hope this helps

Asp.net error on trying to connect with database

I am trying to connect to a database which is an .mdb file from my web application on asp.net. This error comes up 'Microsoft.ACE.OLEDB.12.0 Data Source =C:\Users\KIKI\Desktop\ASP.net labs\Erg8\Erg8\ebookstoredb.mdb' provider is not registered on the local machine. The thing is that on my connection string I don't use version 12 but 4. I tried installing Microsoft Access Database Engine 2010 Redistributable which gives me the option to select version 12 when I set it the connection from the "Server Explorer" window but the error is still there. Any ideas? I have spent hours looking for a solution...
Here is my connection string:
OleDbConnection constring = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\Users\\KIKI\\Desktop\\ASP.net labs\\Erg8\\Erg8\\ebookstoredb.mdb");
Also, I tested the connection when I set it and a message "Connection Succeeded" came up. The problem seems to be when I run the application on the browser.

MVC 4 Entity Framework Cannot Connect to Dynamically Created Database from IIS Server

After I hosted my apps in my localhost IIS, suddenly the system gives me error:
Cannot open database "AlvinCMS" requested by the login. The login
failed. Login failed for user 'NT AUTHORITY\SYSTEM'.
The database AlvinCMS is created at runtime. The system has no problem when in debug mode.
Here is my global.asax:
public class MvcApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
//Check And Init Database
Alvin_CMS.App_Start.DatabaseConfig.Initialize();
......
This is my initialization:
public static void Initialize()
{
Alvin_CMS.Models.AlvinCMSMigrationDBContext migrationDB = new Models.AlvinCMSMigrationDBContext();
try
{
if (!migrationDB.Database.Exists())
{
migrationDB.Database.Initialize(false); //THIS CREATES DATABASE
AlvinCMSExtension.Models.AccountDBContext accountDB = new AlvinCMSExtension.Models.AccountDBContext();
accountDB.Database.Initialize(false);
SetDefaultValue(migrationDB);
}
migrationDB.Database.Initialize(false);
}
catch (Exception e)
{
migrationDB.Database.Delete();
AlvinCMSExtension.Helper.Log(e);
}
}
Here is my context:
public AlvinCMSMigrationDBContext()
: base("DefaultConnection")
{
Database.SetInitializer<AlvinCMSMigrationDBContext>(new CreateDatabaseIfNotExists<AlvinCMSMigrationDBContext>());
}
I set the apps pool to use LocalSystem and I have IntegratedSecurity=true in my connection string. How can I fix this error?
NOTE: If I run this on visual studio development server, it runs fine with no error. Also if I use localDB and run it on IIS Express, it runs fine too. The Problem only exist if I use IIS, even when I debug the program from Visual Studio Local IIS Web Server, then the error is produced
UPDATE
After 4 hours of debugging, finally I know what is wrong. The error exists because the database is not exist. I have another database error that causes the code to enter Exception, so the database is not being created.
The problematic database has this connection string:
Data Source=.\SQLEXPRESS;AttachDbFilename="C:\Users\Public\Documents\Projects\Alvin CMS Project\Alvin CMS\Alvin CMS\App_Data\db\backup\AlvinCMS_Default.mdf";Initial Catalog=AlvinCMS_Default;Integrated Security=True;MultipleActiveResultSets=True
When hosted on IIS, the program cannot attach the database. It causes this error:
{"Unable to open the physical file \"C:\Users\Public\Documents\Projects\Alvin CMS Project\Alvin CMS\Alvin CMS\App_Data\db\backup\AlvinCMS_Default.mdf\". Operating system error 5: \"5(Access is denied.)\".\r\nCannot attach the file 'C:\Users\Public\Documents\Projects\Alvin CMS Project\Alvin CMS\Alvin CMS\App_Data\db\backup\AlvinCMS_Default.mdf' as database 'AlvinCMS_Default'."}
But when I use visual development server, it works fine. So the new question arises, how can I safely attach the database without causing errors when hosted on IIS server?
You can check with following things:
Add Integrated Security=SSPI in your Web.Config
Allow NT AUTHORITY/SYSTEM to server Role as sysadmin.
Goto security > Logins > Select NT AUTHORITY\SYSTEM
Give it the necessary permissions it needs for your app.
Like stated above the updated question, actually there was never be any problem with the database connection. What actually happened was there is an access denied when I tried to access another database, which made the code entered the Exception and never touch the database creation. That is why I cannot login to access the database, because the database was never created.
Then I need to fix the another database which caused the fault. The problem was I cannot attach the database. The access was keep being denied. After I have done some research **I found that I was using LocalDB before I use SQLEXPRESS. I deleted the database from LocalDB, but apparently the localDB is still referencing the database, that is why I got my access denied because the database was still being used by LocalDB. So what I did was to run:
sqllocaldb.exe stop v11.0 from cmd (you can run it from command line in visual studio as well).
Then after that, I can finally attached my database with no problem.

Database connection works locally but fails when published to server

I'm currently working on a ASP.net website, I have everything working and wanted to deploy it to the server which is goDaddy to test the changes I made. I have had this code, the exact same project with the identical database connection string and calls on the server before. Nothing changed really I just changed one method in a class which has nothing to do with the database at all. I have the code trust set to full so the code should have permission to connect. When I set the code trust lower then full it will give me an error that I don't have the needed permissions so I'm sure this isn't where the problem lays.
Like the title says when I try the code on my local environement everything works just fine. When I try to do the same thing on the server I get a message saying:
"unable to connect to any of the specified MySQL hosts"
when you go to http://test.ceremoniecompleet.nl/admin and try to log in you will get the error. When I try to login locally it works just fine.
The connection string I'm using is as follows
con.ConnectionString = "server=188.121.44.188; database=CeremonieCompleet_DB; uid=CCU; pwd=mypassword;";
I've tried to use 188.121.44.188:3306 but this doesn't work at all. One of the godaddy support people suggested using this but this just fails on both locally and the server.
does anyone have any clue what could be going on?
the only thing that changed really is that I used visual studio 2012 before and now I'm using visual studio 2015 because my pc needed a completely fresh install.
EDIT:
Currently the mysql.data.dll that I added to the project is version 6.0.3.0 could it be that this just needs an upgrade?
The 188.121.44.188 is the godaddy server?
If it is, try changing to 'localhost'.
If it isn't, try to create a database in godaddy and set the server to 'localhost'.
To set the port, you need this string "server=<>; port=3306; ..."

How to create a SqlServer database backup with .Net?

I want to make a database backup with this C# code:
connect = new SqlConnection(con);
connect.Open();
// Execute SQL
SqlCommand command = new SqlCommand
(
#"backup database MY_database to disk='d:\SQLBackup\wcBackUp1.bak' with init, stats=10",
connect
);
command.ExecuteNonQuery();
connect.Close();
When I run it, the following error message shows up:
Cannot open backup device 'd:\SQLBackup\wcBackUp1.bak'.
Operating system error 3(The system cannot find the path specified.).
If I change the path to d:\wcBackUp1.bak it seems to be ok, is without error, but the file does not exist, it was not generated.
If I run in SQL the command I have the message that it was 100% processed, but I didn`t see the file.
Could someone help me please?
Make sure the location "d:\SQLBackup\" exist in your database server and not on your client machine.
Two things to check.
The Sql Service may not have access to the d:\sqlbackup folder. Old Sql installs used to default to install the service with full access to the machine, but newer instances tighten that up. You could try changing the path to the directory where the default backups are stored.
Secondly, if the sql server is not on the same machine that you are running this program, then you must remember that the D: will be the D: on the sql server and not your local machine
Fundamentally, the Windows account that the SQL Server service runs under must have write permissions on the specified folder.
You can check what account this is by looking in SQL Server Configuration Manager, under SQL Server Services (look at the Log On As column)
Check what permissions that account actually has on the target folder using Explorer -> right click folder -> properties -> security -> advanced -> effective permissions.
One way to check that this is the problem is to change your code to back up to your SQL instance's backup folder, where the permissions are likely to be correct. For example
C:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\Backup

Categories

Resources