Click here to Skip to main content
15,891,372 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
0 down vote favorite


I'm working on C# linq-to-sql application over SQL Server 2008 mdf database. My database is not inside SQL server, but inside my project folder. My connection string is
C#
<add name="WindowsFormsApplication1.Properties.Settings.OpticaConnectionString"
    connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|  \Optica.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True"
    providerName="System.Data.SqlClient" />


I'm using windows authentication to access SQL Server from C#, and the .mdf file is copied to the ouput folder each time the application is run.

I have a stored procedure to back up the database. Executing the procedure within SQL Server completes successfully, but when I try to call this procedure from C# the following error occurs "Database 'optica.mdf' does not exist. Make sure that the name is entered correctly. BACKUP DATABASE is terminating abnormally.". That's though the database name "optica.mdf" is not even a parameter for the procedure.

Here is my stored procedure

SQL
USE [OPTICA.MDF]
GO

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER proc [dbo].[Sp_OpticaDatabaseBackup]
@Path varchar(1024),
as
Begin       
    DECLARE @fileName VARCHAR(256) -- file name for backup
    DECLARE @fileDate VARCHAR(20) -- used for file name 
    SELECT @fileDate = replace(CONVERT(VARCHAR(20),GETDATE(),126),':','-')
    SET @fileName = @Path + 'Pts_' + @fileDate + '.BAK' 
    BACKUP DATABASE [optica.mdf] TO DISK = @fileName
End
Any ideas? Thanks in advance.
Posted

1 solution

BACKUP DATABASE is intended for databases that are inside SQL server, not in your project folder: the SQL server instance is very unlikely to have any access to your project folder in production.

Since the file is in your computer, why not back it up the old, traditional way? File.Copy should do it...
 
Share this answer
 
Comments
mido_h_89 21-Sep-14 4:26am    
Thanks for suggestion.
Actually, I think it's not a good idea, since if some transactions are being done on the database, some inconsistencies may occur in the backup, thus I have to take the DB offline, back it up and then start it. This seems not promising.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900