Windows services 将数据从windows服务写入SQL Server 2012
是否可以从RFID阅读器获取数据。从Windows服务开始,然后将数据直接写入sql server 2012 如果这是可能的,我怎么能做到这一点。在服务或Sql Server中编写代码Windows services 将数据从windows服务写入SQL Server 2012,windows-services,sql-server-2012,rfid,Windows Services,Sql Server 2012,Rfid,是否可以从RFID阅读器获取数据。从Windows服务开始,然后将数据直接写入sql server 2012 如果这是可能的,我怎么能做到这一点。在服务或Sql Server中编写代码 先谢谢大家我们先谈谈结构。在服务的OnStart方法中,我建议创建一个管理数据库交互的对象。OnStop方法需要一种方法,在服务停止时通知此线程停止运行。要做到这一点,我使用了一个。这是基本结构 using System.ServiceProcess; using System.Threading; publi
先谢谢大家我们先谈谈结构。在服务的
OnStart
方法中,我建议创建一个管理数据库交互的对象。OnStop
方法需要一种方法,在服务停止时通知此线程停止运行。要做到这一点,我使用了一个。这是基本结构
using System.ServiceProcess;
using System.Threading;
public partial class Service1 : ServiceBase
{
private ManualResetEvent _shutdownEvent;
private Thread _thread;
public Service1()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
/* This WaitHandle will allow us to shutdown the thread when
the OnStop method is called. */
_shutdownEvent = new ManualResetEvent(false);
/* Create the thread. Note that it will do its work in the
appropriately named DoWork method below. */
_thread = new Thread(DoWork);
/* Start the thread. */
_thread.Start();
}
protected override void OnStop()
{
/* Set the WaitHandle, which signals the thread to stop. */
_shutdownEvent.Set();
/* Wait for the thread to exit. */
_thread.Join();
}
/* This is the method that will be invoked when the thread is started
in the OnStart method. */
private void DoWork()
{
/* To keep the thread running, use a while loop, checking the
status of the ManualResetEvent each time through the loop.
When the OnStop method is called, the ManualResetEvent will be
triggered, thereby exiting the loop. The thread will end as
soon as this method exits. */
while (!_shutdownEvent.WaitOne(0))
{
/* Sleep for one second. We'll modify this later, but for
now, it will keep the processor from being used at 100%. */
Thread.Sleep(1000);
}
}
}
现在让我们关注DoWork
方法。我假设您已经创建了SQL Server Express 2012数据库,并且所需的表已经存在。有很多方法可以通过编程来完成这两件事,但为了简单起见,我不打算在这里担心这一点。此外,我还将使用SQL Server Express LocalDB 2014实例使用的语法。这就是我正在使用的语法,但它应该与SQL Server Express 2012语法非常接近(如果不完全相同的话)。连接字符串可能需要更改,您可以查看以查找示例
您要做的第一件事是连接到数据库。然后,在while循环中,您需要向数据库添加数据。因为我不知道你们的RFID数据收集是如何工作的,所以这是你们必须要做的事情,但这应该给你们一个大概的想法
using System.Data.SqlClient;
private void DoWork()
{
/* Define the connection string. */
string connectionString = @"Data Source=(LocalDB)\MSSQLLocalDB;Initial Catalog=master;Integrated Security=True";
/* Define the command text used for inserting the data into the
database. Note that this will be very specific to your database
schema. I'm assuming you have a table named 'RFID' with a single
column named 'Data' of type INTEGER. */
string commandText = @"INSERT INTO RFID (Data) VALUES (@data)";
/* Create the connection object. The using statement will ensure that
the object is disposed when we exit. */
using (var connection = new SqlConnection(connectionString))
{
/* Open the connection. */
connection.Open();
/* Enter the while loop to keep the thread running. */
int count = 0;
while (!_shutdownEvent.WaitOne(0))
{
/* TODO: Someway, somehow, you'll have to acquire the RFID
data. In this example, I'll just increment a counter. */
int data = count++;
/* Create the command for inserting the data into the
database. */
using (var command = new SqlCommand(commandText, connection))
{
/* Add the parameter. */
command.Parameters.AddWithValue("@data", data);
/* Execute the command. */
command.ExecuteNonQuery();
}
}
}
}
请注意,这是一个非常粗略的例子。您可能希望通过捕获并记录可能发生的任何异常来防弹DoWork
方法。显然,如何获取RFID数据以及如何将其插入数据库将取决于您的具体要求
HTH非常感谢您。我现在就试试。你在哪里启动tcp连接。我的意思是打开端口从RFID阅读器获取数据。(顺便说一句,RFID是SAAT-520系列读卡器)。|谢谢您可以在进入线程循环之前,在
OnStart
方法中或在DoWork
方法的开头执行此操作。