前言
ProcessDB实时/时序数据库支持C#语言开发,本文将针对C#操作ProcessDB连接数据库,添加数据库,删除数据库,添加表,添加测点,插入数据,查询数据进行介绍
示例代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.InteropServices;
using ProcessDB;
namespace Object_String
{
class Program
{
static void Main(string[] args)
{
TestString testString = new TestString();
int res = testString.StringTest();
}
}
public class TestString
{
public int StringTest()
{
int res = 0;
/* 初始化连接控制块 */
res = ProcessDBDao.getInstance().init();
if (0 != res)
{
Console.WriteLine("ProcessDBDao init failed! error code: {0}", res);
return res;
}
/* 建立连接 */
res = ProcessDBDao.getInstance().connect("117.78.33.41", 8399, "root", "shuquan230519");
//res = ProcessDBDao.getInstance().connect("192.168.0.186", 8301, "root", "root");
if (0 != res)
{
Console.WriteLine("login failed! error code: {0}", res);
return res;
}
/* 删除数据库 */
string databasename = "D123";
res = ProcessDBDao.getInstance().delete_database(databasename);
if (res < 0 && res != -5112)
{
Console.WriteLine("Delete {0} failed!error code: {0}", databasename, res);
return res;
}
/* 添加数据库 */
String desc = "testD100";
res = ProcessDBDao.getInstance().add_database(databasename, desc);
if (res < 0)
{
Console.WriteLine("Add {0} failed!error code: {1}", databasename, res);
return res;
}
/*添加表*/
String tablename = databasename + ".T1";
desc = "table_t1";
res = ProcessDBDao.getInstance().add_table(tablename, desc);
if (res < 0)
{
Console.WriteLine("Add {0} failed!error code: {1}", tablename, res);
return res;
}
/*添加采样点(string)*/
PointProperty pointProperty = new PointProperty();
pointProperty.point_type = (byte)POINT_TYPE.PHY_PT;//采样点
pointProperty.priority = (byte)PRIORITY_TYPE.PRI_MIDDLE; //优先级:中
pointProperty.data_type = (byte)DATA_TYPE.TAG_TYPE_STRING; //数据类型: string
pointProperty.io_type = (byte)IO_TYPE.IO_INPUT; //io类型:输入
pointProperty.space_compress = 1; //数据块压缩
pointProperty.time_unit = (byte)TIME_UNIT_TYPE.TU_SQUENCE; //时间单位:秒
// pointProperty.pad;
pointProperty.init_value = 0; //初始值
pointProperty.bottomscale = 1; //量程低限
pointProperty.topscale = 6; //量程高限
pointProperty.alarm_low = 3; //告警底限
pointProperty.alarm_high = 4; //告警高限
pointProperty.deviation = 20; //
pointProperty.dev_type = 0; //
pointProperty.time_compress = 0; //
pointProperty.alarm_switch = 0; //告警开关
pointProperty.alarm_ack_type = 0; //
//无告警
pointProperty.alarm_type = 0;
pointProperty.alarm_level = 0;
//pointProperty.alarm_time_dead_band = ;
//pointProperty.alarm_value_dead_band = ;
pointProperty.alarm_hh = 5;
pointProperty.alarm_ll = 2;
pointProperty.archived = 1;
pointProperty.alarm_group_id = 1;
int pointId = 0;
string unit = "N/A";
string expression = "";
desc = "测试string";
string pointname = tablename + ".P1";
res = ProcessDBDao.getInstance().add_point(pointname, ref pointProperty, unit, expression, desc, ref pointId);
if (res < 0)
{
Console.WriteLine("Add point: {0} failed!error code: {1}", pointname, res);
return res;
}
else
{
Console.WriteLine("Add string point succeed! pointname: {0}, dataType: {1}, unit: {2}, expression: {3}, desc: {4}, pointId: {5} succeed!", pointname, pointProperty.data_type, unit, expression, desc, pointId);
}
MESSAGE_TYPE messageType = MESSAGE_TYPE.MT_INFO;
string str = "abc1";
/* 更新实时数据(数据类型:string) */
res = ProcessDBDao.getInstance().insert_realtime_string_by_name(pointname, messageType, str);
if (res < 0)
{
Console.WriteLine("insert realTime string by name failed!", pointname, res);
return res;
}
else
{
Console.WriteLine("Insert realTime string by name succeed! pointname: {0}, messagetype: {1}", pointname, MESSAGE_TYPE.MT_INFO);
}
/* 查询实时数据(数据类型:string) */
res = ProcessDBDao.getInstance().add_name_to_query_builder(pointname);
if (0 != res)
{
Console.WriteLine("add {0} to query builder failed! error code: {1}", pointname, res);
}
StringBuilder outstr = new StringBuilder();
res = ProcessDBDao.getInstance().query_realtime_string_by_name(pointname, outstr);
if (0 != res)
{
Console.WriteLine("query realtime string by name failed, error code: {0}", res);
}
else
{
Console.WriteLine("query realtime string by name succeed, string: {0}", outstr.ToString());
}
res = ProcessDBDao.getInstance().close();
return res;
}
}
}
运行结果: