我试图制作我的应用程序,目的是在磁盘上备份数据库,并通过ftp或邮件发送。因此,我做了一个研究,最后我写了Windows服务的项目和控制台的另一个项目,即数据库备份。两者都工作得很好,而且都是在同一个Visual中编写的,但是当我试图将生成备份的代码放到Windows服务中时,它就不起作用了。我不明白为什么。我尝试了put代码,而不是示例(它创建了一个文件并在那里编写了一行代码,这个部分运行良好),我甚至尝试创建另一个方法来完成它,然后调用这个方法。
Windows服务与这里完全相同,在SpadesAdminService类中而不是
System.Diagnostics.EventLog.WriteEntry("SpadesAdminSvc",
ServiceName + "::Execute()");我编写了这段代码(每5秒在我的磁盘上创建一个空文件,应该写“文本到文件”,但是文件出现了!):
using (FileStream fs = File.OpenWrite("C:\\place\\" + DateTime.Now.ToString("ddMMyyyy_HHmmss") + ".txt"))
{
Byte[] napis = new UTF8Encoding(true).GetBytes("text to files"));
fs.Write(napis, 0, napis.Length);
} 我的补习班(独自一人也很好):
namespace makeBackUpConsole
{
class Program
{
static void Main(string[] args)
{
string dbname = "exampleToniDatabase";
SqlConnection sqlcon = new SqlConnection();
SqlCommand sqlcmd = new SqlCommand();
SqlDataAdapter da = new SqlDataAdapter();
DataTable dt = new DataTable();
sqlcon.ConnectionString = @"Server=GRAFIKA-2\SQLEXPRESS;Integrated Security=True;" + "Database=exampleToniDatabase";
string destdir = "C:\\place\\";
if (!System.IO.Directory.Exists(destdir))
{
System.IO.Directory.CreateDirectory("C:\\place\\");
}
try
{
sqlcon.Open();
sqlcmd = new SqlCommand("backup database " + dbname + " to disk='" + destdir + "\\" + DateTime.Now.ToString("ddMMyyyy_HHmmss") + ".Bak'", sqlcon);
sqlcmd.ExecuteNonQuery();
sqlcon.Close();
MessageBox.Show("Backup database successfully");
}
catch (Exception ex)
{
MessageBox.Show("Error During backup database!");
}
}
}
}我正在复制这个类,而不是我的代码来创建txt文件,而却无法工作。这是一个代码:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.ServiceProcess;
using System.IO;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Data.SqlClient;
using System.Windows.Forms;
namespace toni.exampleService.Services
{
public class exampleAdminService : exampleServiceBase
{
public exampleAdminService()
{
this.ServiceName = "exampleAdminSvc";
}
protected override void OnStart(string[] args)
{
base.OnStart(args);
}
protected override void OnStop()
{
base.OnStop();
}
protected override int Execute()
{
//using (FileStream fs = File.OpenWrite("C:\\development\\toni\\dd\\" + DateTime.Now.ToString("ddMMyyyy_HHmmss") + ".txt"))
//{
// Byte[] napis = new UTF8Encoding(true).GetBytes("Dzień i godzina: " + DateTime.Now.ToString("ddMMyyyy_HHmmss"));
// fs.Write(napis, 0, napis.Length);
//}
string dbname = "exampleToniDatabase";
SqlConnection sqlcon = new SqlConnection();
SqlCommand sqlcmd = new SqlCommand();
SqlDataAdapter da = new SqlDataAdapter();
DataTable dt = new DataTable();
sqlcon.ConnectionString = @"Server=GRAFIKA-2\SQLEXPRESS;Integrated Security=True;" + "Database=exampleToniDatabase";
string destdir = "C:\\place\\";
if (!System.IO.Directory.Exists(destdir))
{
System.IO.Directory.CreateDirectory("C:\\place\\");
}
try
{
sqlcon.Open();
sqlcmd = new SqlCommand("backup database " + dbname + " to disk='" + destdir + "\\" + DateTime.Now.ToString("ddMMyyyy_HHmmss") + ".Bak'", sqlcon);
sqlcmd.ExecuteNonQuery();
sqlcon.Close();
MessageBox.Show("Backup database successfully");
}
catch (Exception ex)
{
MessageBox.Show("Error During backup database!");
}
return 0;
}
}}
当然,所有的库都是链接的。
如有任何意见,请帮助我。
提前谢谢你,祝你今天愉快。
编辑:嘿,问题解决了。我在中创建了一个数据库帐户(而不是Windows ),并将这个帐户用户Id和密码直接输入到Windows中的C#代码中。无论如何,也许有人会使用我的代码:)谢谢你的回复。
发布于 2013-07-10 12:08:43
您是否检查过Windows服务是否具有在指定位置写入文件的权限。服务不一定以用户身份运行,所以不要假设您可以在哪里编写文件,您的服务也可以这样做。
您是否尝试过写入c:\ProgramData下面的文件夹?
如果你不知道问题出在哪里,那么你需要找出答案。尝试在服务启动时添加System.Diagnostics.Debugger.Launch();,然后跟踪调试器中的更改。
发布于 2013-07-10 12:17:50
您编写的Windows服务正在为您的Server使用“集成安全性”。
如果不想在代码中输入登录凭据,则应该将执行用户设置为本地帐户,或者授予所需用户(例如LocalSystem)对中数据库的访问权限。
通常,NetworkService以LocalSystem、LocalService或NetworkService的形式运行。只需在services.msc中更改Windows的设置
https://stackoverflow.com/questions/17570226
复制相似问题