我想使用Windows服务定期备份我的数据库。但我无法运行此服务。下面是我的code.i能够成功地将此服务安装到我的本地系统但是当我试图启动它时给我错误“服务xyz.exe启动然后停止。如果其他服务使用它们,则会自动停止。”当我从services.msc手动启动服务。如果有人可以帮助我,我很无奈。我的代码是
public static void BackupDatabase(string backUpFile)
{
ConnectionStringSettingsCollection connections = ConfigurationManager.ConnectionStrings;
string MyConnectionString = connections["MDASconfig"].ConnectionString;
ServerConnection con = new ServerConnection(MyConnectionString);
Server server = new Server(con);
Backup source = new Backup();
source.Action = BackupActionType.Database;
source.Database = "MDAS";
BackupDeviceItem destination = new BackupDeviceItem(backUpFile, DeviceType.File);
source.Devices.Add(destination);
source.SqlBackup(server);
con.Disconnect();
}
我已经应用了一些解决方案,例如通过事件查看器增加大小,并通过此服务的登录属性使用相同的登录帐户启动服务.Service在Services.msc中列出但由于上述错误而无法启动。
答案 0 :(得分:0)
您需要在内存中继续运行线程,请使用以下代码:
Dim delPeriodicalCheck As New TimerCallback(AddressOf BackupDatabase)
Dim objTimer As System.Threading.Timer
Dim dueTime = 1000 '1 Sec, delay start
Dim period = 60000 '1 Min, you may change this value
Protected Overrides Sub OnStart(ByVal args() As String)
objTimer = New System.Threading.Timer(delPeriodicalCheck, Nothing, dueTime,period)
Catch ex As Exception
EventLog1.WriteEntry(ex.Message)
End Try
Private Sub BackupDatabase(ByVal stateInfo As Object)
'Your code
End Sub