I'm gonna get a backup with below method :
void BackupDatabase(string sConnect, string dbName, string backUpPath)
{
using (SqlConnection cnn = new SqlConnection(sConnect))
{
cnn.Open();
dbName = cnn.Database.ToString();
ServerConnection sc = new ServerConnection(cnn);
Server sv = new Server(sc);
// Create backup device item for the backup
BackupDeviceItem bdi = new BackupDeviceItem(backUpPath, DeviceType.File);
开发者_如何学Python
// Create the backup informaton
Microsoft.SqlServer.Management.Smo.Backup bk = new Backup();
bk.PercentComplete += new PercentCompleteEventHandler(percentComplete);
bk.Devices.Add(bdi);
bk.Action = BackupActionType.Database;
bk.PercentCompleteNotification = 1;
bk.BackupSetDescription = dbName;
bk.BackupSetName = dbName;
bk.Database = dbName;
//bk.ExpirationDate = DateTime.Now.AddDays(30);
bk.LogTruncation = BackupTruncateLogType.Truncate;
bk.FormatMedia = false;
bk.Initialize = true;
bk.Checksum = true;
bk.ContinueAfterError = true;
bk.Incremental = false;
// Run the backup
bk.SqlBackup(sv);//Exception
}
}
But an exception has occurred :
Microsoft.SqlServer.Management.Smo.FailedOperationException: Backup failed for Server '\\.\pipe\3F103E6E-3FD4-47\tsql\query'
additional info about the Exception:
{Microsoft.SqlServer.Management.Smo.FailedOperationException: Backup failed for Server '\\.\pipe\3F103E6E-3FD4-47\tsql\query'. ---> System.IO.FileNotFoundException: Could not load file or assembly 'Microsoft.SqlServer.BatchParser, Version=9.0.242.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91' or one of its dependencies. The system cannot find the file specified.
File name: 'Microsoft.SqlServer.BatchParser, Version=9.0.242.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91'
at Microsoft.SqlServer.Management.Common.ServerConnection.GetStatements(String query, ExecutionTypes executionType, Int32& statementsToReverse)
at Microsoft.SqlServer.Management.Common.ServerConnection.ExecuteNonQuery(String sqlCommand, ExecutionTypes executionType)
at Microsoft.SqlServer.Management.Common.ServerConnection.ExecuteNonQuery(StringCollection sqlCommands, ExecutionTypes executionType)
at Microsoft.SqlServer.Management.Smo.ExecutionManager.ExecuteNonQuery(StringCollection queries)
at Microsoft.SqlServer.Management.Smo.ExecutionManager.ExecuteNonQueryWithMessage(StringCollection queries, ServerMessageEventHandler dbccMessageHandler, Boolean errorsAsMessages)
at Microsoft.SqlServer.Management.Smo.BackupRestoreBase.ExecuteSql(Server server, StringCollection queries)
at Microsoft.SqlServer.Management.Smo.Backup.SqlBackup(Server srv)
=== Pre-bind state information ===
LOG: User = MDS-PC\MDS
LOG: DisplayName = Microsoft.SqlServer.BatchParser, Version=9.0.242.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91
(Fully-specified)
LOG: Appbase = file:///D:/My Works/C#/Win Form/Reza Restaurant/RezaRestaurant/bin/Release/
LOG: Initial PrivatePath = NULL
Calling assembly : Microsoft.SqlServer.ConnectionInfo, Version=9.0.242.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91.
===
LOG: This bind starts in default load context.
LOG: Using application configuration file: D:\My Works\C#\Win Form\Reza Restaurant\RezaRestaurant\bin\Release\RezaRestaurant.vshost.exe.Config
LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework64\v2.0.50727\config\machine.config.
LOG: Post-policy reference: Microsoft.SqlServer.BatchParser, Version=9.0.242.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91
LOG: Attempting download of new URL file:///D:/My Works/C#/Win Form/Reza Restaurant/RezaRestaurant/bin/Release/Microsoft.SqlServer.BatchParser.DLL.
LOG: Attempting download of new URL file:///D:/My Works/C#/Win Form/Reza Restaurant/RezaRestaurant/bin/Release/Microsoft.SqlServer.BatchParser/Microsoft.SqlServer.BatchParser.DLL.
LOG: Attempting download of new URL file:///D:/My Works/C#/Win Form/Reza Restaurant/RezaRestaurant/bin/Release/Microsoft.SqlServer.BatchParser.EXE.
LOG: Attempting download of new URL file:///D:/My Works/C#/Win Form/Reza Restaurant/RezaRestaurant/bin/Release/Microsoft.SqlServer.BatchParser/Microsoft.SqlServer.BatchParser.EXE.
--- End of inner exception stack trace ---
at Microsoft.SqlServer.Management.Smo.Backup.SqlBackup(Server srv)
at RezaRestaurant.Form_تنظیمات_نرم_افزار.BackupDatabase(String sConnect, String dbName, String backUpPath) in D:\My Works\C#\Win Form\Reza Restaurant\RezaRestaurant\Forms\تنظیمات_نرم_افزار.cs:line 260
at RezaRestaurant.Form_تنظیمات_نرم_افزار.button_تهیه_نسخه_پشتیبان_Click(Object sender, EventArgs e) in D:\My Works\C#\Win Form\Reza Restaurant\RezaRestaurant\Forms\تنظیمات_نرم_افزار.cs:line 177}
Would you please guide me ? Thanks.
The error message is pretty much conclusive:
Could not load file or assembly 'Microsoft.SqlServer.BatchParser, Version=9.0.242.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91' or one of its dependencies. The system cannot find the file specified.
Is SMO installed correctly?
Are you running on a 64 bit system?
Have you installed the 2008 version of the SMO Components?
You can down the 64 bit version of SMO from here: Microsoft SQL Server 2008 Feature Pack, October 2008
I had to install this package :
X64 Package (SQLServer2005_XMO_x64.msi) - 14675 KB
My problem was solved.
精彩评论