C# 从Windows服务应用程序访问数据并将数据插入SQL Server数据库?
我曾尝试使用Windows服务应用程序将数据插入我的数据库,但没有达到效果。服务运行。连接字符串是正确的,但无法插入数据 我试着在网上做我的研究。。。到目前为止,还没有找到任何伟大的尝试。有人有解决办法吗 多谢各位C# 从Windows服务应用程序访问数据并将数据插入SQL Server数据库?,c#,sql-server,windows,C#,Sql Server,Windows,我曾尝试使用Windows服务应用程序将数据插入我的数据库,但没有达到效果。服务运行。连接字符串是正确的,但无法插入数据 我试着在网上做我的研究。。。到目前为止,还没有找到任何伟大的尝试。有人有解决办法吗 多谢各位 // this is my service class public partial class sacc : ServiceBase { SqlConnection con; SqlCommand com; string query = string.Em
// this is my service class
public partial class sacc : ServiceBase
{
SqlConnection con;
SqlCommand com;
string query = string.Empty;
SqlDataReader reader;
GlobalConnector gc = new GlobalConnector();
SerialPort sp;
string temp;
public sacc()
{
InitializeComponent();
//Control.CheckForIllegalCrossThreadCalls = false;
}
public sacc(string[] args)
{
this.CanStop = true;
this.CanPauseAndContinue=true;
InitializeComponent();
string eventSourceName = "MySource";
string logName = "MyNewLog";
if (args.Count() > 0)
{
eventSourceName = args[0];
}
if (args.Count() > 1)
{
logName = args[1];
}
eventLog1 = new System.Diagnostics.EventLog();
if (!System.Diagnostics.EventLog.SourceExists(eventSourceName))
{
System.Diagnostics.EventLog.CreateEventSource(eventSourceName, logName);
}
eventLog1.Source = eventSourceName;
eventLog1.Log = logName;
}
Timer timer1 = new Timer();
protected override void OnStart(string[] args)
{
timer1.Elapsed += new ElapsedEventHandler(timer1_Elapsed);
timer1.Interval = 10000;
timer1.Enabled = true;
timer1.Start();
}
private void timer1_Elapsed(object sender, EventArgs e)
{
using (con = new SqlConnection(gc.connnectionstring))
{
con.Open();
sp = new SerialPort(AutodetectArduinoPort(), 9600);
sp.Open();
sp.Parity = Parity.None;
sp.StopBits = StopBits.One;
sp.Handshake = Handshake.None;
sp.ReadTimeout = 10000;
sp.DataReceived += new SerialDataReceivedEventHandler(datareceive);
query = "delete from temperature";
using (var com = new SqlCommand(query, con))
{
com.ExecuteNonQuery();
}
con.Close();
}
}
private void datareceive(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{
int countbt = sp.BytesToRead;
if (countbt > 3)
{
byte[] recbyte = new byte[countbt];
sp.Read(recbyte, 0, countbt - 1);
char deg = (char)176;
temp = recbyte[2].ToString() + " " + deg + " Celcius";
// label3.Text = ASCIIEncoding.ASCII.GetString(recbyte,2,1);
}
}
private string AutodetectArduinoPort()
{
ManagementScope connectionScope = new ManagementScope();
SelectQuery serialQuery = new SelectQuery("SELECT * FROM Win32_SerialPort");
ManagementObjectSearcher searcher = new ManagementObjectSearcher(connectionScope, serialQuery);
try
{
foreach (ManagementObject item in searcher.Get())
{
string desc = item["Description"].ToString();
string deviceId = item["DeviceID"].ToString();
if (desc.Contains("Arduino"))
{
return deviceId;
}
}
}
catch (ManagementException e)
{
/* Do Nothing */
}
return null;
}
这是我的projectinstaller.cs:
非常感谢。需要更多详细信息,但根据以往经验,这很可能是一个安全问题!!下面是解决这个问题的细节 您需要检查Windows服务属性,并检查其运行的凭据 SQL Server需要将该用户添加到access数据库中 e、 g若您使用SYSTEM,那个么将SYSTEM添加到SQL Server角色,并授予它访问您想要访问的数据库的权限 这应该可以解决你的问题
你需要添加更多,以便任何人都能帮助你。例如,当前发生了什么?所以您的代码不起作用,但您没有向我们展示有关此代码的任何内容。我们需要一个水晶球来猜测那里发生了什么。另外,因为我们今天早上把它留在家里,我们看不到您看到的代码。。那么,你希望有人怎么回答你这个非问题呢?很明显,你需要改变手表的名称,并确保你参考了这个东西。然后再次检查watzadoodle是否已损坏,如果已损坏,请使用FZZZBBBBBB重新运行。我相信你会根据你写问题的方式理解这一点。不过说真的,添加你的代码给我们一个机会。我愿意打赌,如果你在con.Open上设置断点;在你的计时器里,你已经过去了。。。事件处理程序,您将在几秒钟内解决您的问题。谢谢Sumeet Kumar,我想解决这个问题的工作会给我一个解决方案。但问题是如何将系统添加到SQL server角色并授予其访问权限。
private void serviceProcessInstaller1_AfterInstall(object sender, InstallEventArgs e)
{
this.serviceProcessInstaller1.Account = System.ServiceProcess.ServiceAccount.LocalSystem;
serviceProcessInstaller1.Username = null;
serviceProcessInstaller1.Password = null;
}