我需要帮助来更改我的方法填充我的数据库的形式。
我使用 API 通过 OPC 协议与工业设备进行通信。
此 API 将通用时区 (UTC) 作为参数,但我在通用光栅 (UTC) 中有 +3 小时的差异。
在我的数据集中,我将此 API 用于返回三个属性的类:数据值、质量和时间戳
我需要及时转换为本地时间才能将此属性 Timestamp 保存在我的数据库中。
下面是我的班级的一个班级例子。
using System;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using OpcLabs.EasyOpc.DataAccess;
namespace LogAsStringToSql
{
class Program
{
static void Main()
{
const string connectionString =
"Data Source=.\\SQLEXPRESS;Initial Catalog=QuickOPCExamples;Integrated Security=true";
Console.WriteLine("Starting up...");
using (var connection = new SqlConnection(connectionString))
{
connection.Open();
// Create all necessary ADO.NET objects.
var adapter = new SqlDataAdapter("SELECT * FROM SimpleLog", connection);
var dataSet = new DataSet();
adapter.FillSchema(dataSet, SchemaType.Source, "SimpleLog");
adapter.InsertCommand = new SqlCommandBuilder(adapter).GetInsertCommand();
DataTable table = dataSet.Tables["SimpleLog"];
Console.WriteLine("Logging for 30 seconds...");
// Subscribe to an OPC item, using an anonymous method to process the notifications.
int[] handles = EasyDAClient.DefaultInstance.SubscribeMultipleItems(
new[]
{
new DAItemGroupArguments("", "OPCLabs.KitServer.2", "Simulation.Incrementing (1 s)", 100, null),
new DAItemGroupArguments("", "OPCLabs.KitServer.2", "Simulation.Ramp (10 s)", 1000, null),
new DAItemGroupArguments("", "OPCLabs.KitServer.2", "Simulation.Register_BSTR", 1000, null),
new DAItemGroupArguments("", "OPCLabs.KitServer.2", "Simulation.Register_BOOL", 1000, null)
},
(_, eventArgs) =>
{
Console.Write(".");
// In this example, we only log valid data. Production logger would also log errors.
if (eventArgs.Vtq != null)
{
// Fill a DataRow with the OPC data, and add it to a DataTable.
table.Rows.Clear();
DataRow row = table.NewRow();
row["ItemID"] = eventArgs.ItemDescriptor.ItemId;
row["Value"] = eventArgs.Vtq.Value; // The DataRow will make the conversion to a string.
row["Timestamp"] = (eventArgs.Vtq.Timestamp < (DateTime)SqlDateTime.MinValue)
? (DateTime)SqlDateTime.MinValue
: eventArgs.Vtq.Timestamp;
row["Quality"] = (short)eventArgs.Vtq.Quality;
table.Rows.Add(row);
// Update the underlying DataSet using an insert command.
adapter.Update(dataSet, "SimpleLog");
}
}
);
System.Threading.Thread.Sleep(60 * 1000);
Console.WriteLine();
Console.WriteLine("Shutting down...");
EasyDAClient.DefaultInstance.UnsubscribeMultipleItems(handles);
}
Console.WriteLine("Finished.");
}
}
}
我如何更改当前方法以在 DateTime 上使用 ToLocalTime () 方法来接收本地时间的时间戳?
row["Timestamp"] = (eventArgs.Vtq.Timestamp < (DateTime)SqlDateTime.MinValue)
? (DateTime)SqlDateTime.MinValue
: eventArgs.Vtq.Timestamp;
我无法理解如何改变这一点,非常欢迎任何帮助,谢谢。