使用存储过程备份数据库时C#Linq-to-sql错误 [英] C# Linq-to-sql error when using stored procedure to backup database

查看:82
本文介绍了使用存储过程备份数据库时C#Linq-to-sql错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

0 down vote最喜欢





我正在使用SQL Server 2008 mdf数据库上的C#linq-to-sql应用程序。我的数据库不在SQL服务器内,但在我的项目文件夹中。我的连接字符串是

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

<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" />





我正在使用Windows身份验证从C#访问SQL Server,并且.mdf文件被复制到输出每次运行应用程序时都有文件夹。



我有一个备份数据库的存储过程。在SQL Server中执行该过程成功完成,但是当我尝试从C#调用此过程时,出现以下错误数据库'optica.mdf'不存在。请确保正确输入名称.BACKUP DATABASE异常终止。 。虽然数据库名称optica.mdf甚至不是程序的参数。



这是我的存储过程





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

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.

推荐答案

BACKUP DATABASE 适用于 所在的数据库 在SQL服务器内,而不是在你的项目文件夹中:SQL服务器实例不太可能在生产中访问你的项目文件夹。



由于文件在您的计算机中,为什么不以旧的传统方式备份它? File.Copy应该这样做......
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...


这篇关于使用存储过程备份数据库时C#Linq-to-sql错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆