实例2 使用DataTable插入数据
使用C#以DataTable的方式插入数据如下所示,由于松果时序数据库有数据写入窗口的限制,实际使用时请修改相应的参数,并确保插入的设备已经创建。详细信息请参见用户手册。
每次插入的数据量不能超过1000条。
- void InsertForDataTable()
- {
- string connStr = "server=127.0.0.1;port=8105;username=sa;password=pinusdb";
- DataTable dt = new DataTable("tab01");
- dt.Columns.Add(new DataColumn("devid", typeof(long)));
- dt.Columns.Add(new DataColumn("tstamp", typeof(DateTime)));
- dt.Columns.Add(new DataColumn("val01", typeof(long)));
- dt.Columns.Add(new DataColumn("val02", typeof(float)));
- DataRow row1 = dt.NewRow();
- row1["devid"] = 101;
- row1["tstamp"] = DateTime.Now;
- row1["val01"] = 32;
- row1["val02"] = 27.33;
- DataRow row2 = dt.NewRow();
- row2["devid"] = 102;
- row2["tstamp"] = DateTime.Now;
- row2["val01"] = 12785;
- row2["val02"] = 758.23375;
- dt.Rows.Add(row1);
- dt.Rows.Add(row2);
- try
- {
- //实例化连接对象
- PDBConnection con = new PDBConnection(connStr);
- con.Open();
- PDBCommand cmd = con.CreateCommand();
- PDBErrorCode retCode = cmd.ExecuteInsert(dt);
- if (retCode != PDBErrorCode.PdbE_OK)
- {
- Console.WriteLine("成功条数:" + cmd.SuccessCount + " 错误信息:" + PDBErrorMsg.GetErrorMsg(retCode));
- }
- else
- Console.WriteLine("插入成功!");
- con.Close();
- }
- catch (PDBException ex)
- {
- Console.WriteLine(ex.Message);
- }
- }