概述
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.IO;
using System.Windows.Forms;
using Common.TxtUtilities;
using Atspace.DataInjection;
using System.Text.RegularExpressions;
namespace Atspace.DataInjection.AddFunctionLibrary
{
public class DataServer
{
public DataServer(DataMainForm form)
{ this.form = form; }
public DataMainForm form;
#region 数据表成员
public System.Data.DataTable fixedDataTableForView;//
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.IO;
using System.Windows.Forms;
using Common.TxtUtilities;
using Atspace.DataInjection;
using System.Text.RegularExpressions;
namespace Atspace.DataInjection.AddFunctionLibrary
{
public class DataServer
{
public DataServer(DataMainForm form)
{ this.form = form; }
public DataMainForm form;
#region 数据表成员
public System.Data.DataTable fixedDataTableForView;//
#endregion
#region 函数功能
public DataTable GetFixedDataTable(string filePath)//
{
DataTable dt;
if (!File.Exists(filePath))
{
MessageBox.Show("指定目录下未找到源文件", "提示", MessageBoxButtons.OK);
return new DataTable();
}
else
{
Dictionary<string ,List<string>> vieDic=new Dictionary<string,List<string>>();
string sourceContent = TxtReader.GetString(filePath);
vieDic = form.AddParasData(sourceContent);
Dictionary<string, List<string>> VieDic1 = new Dictionary<string, List<string>>();
VieDic1.Add("包头1",new List<string>(){"unsigned int","0x1040"});
VieDic1.Add("包头2", new List<string>() { "unsigned int", "0xc000" });
Dictionary<string,string> lengthAndSum=form.GetDataLengthAanSum_Add();
VieDic1.Add("包长",new List<string>(){"unsigned int",lengthAndSum["包长"]});
VieDic1.Add("累加和", new List<string>() { "unsigned char", lengthAndSum["累加和"] });
if (lengthAndSum.ContainsKey("填充码"))
{
VieDic1.Add("填充码", new List<string>() { "unsigned char", lengthAndSum["填充码"] });
}
Dictionary<string, List<string>> VieDic2 = new Dictionary<string, List<string>>();
VieDic2.Add("包头1",VieDic1["包头1"]);
VieDic2.Add("包头2",VieDic1["包头2"]);
VieDic2.Add("包长", VieDic1["包长"]);
VieDic2.Add("识别码", new List<string>() { "unsigned int", form.txtMarkNum1.Text.Trim() });
foreach(var item in vieDic)
{
VieDic2.Add(item.Key,item.Value);
}
dt = new DataTable();
dt.Columns.Add("参数名");
dt.Columns.Add("参数类型");
dt.Columns.Add("参数值");
foreach (var item in VieDic2)
{
dt.Rows.Add(item.Key, item.Value[0], item.Value[1]);
public DataTable GetFixedDataTable(string filePath)//
{
DataTable dt;
if (!File.Exists(filePath))
{
MessageBox.Show("指定目录下未找到源文件", "提示", MessageBoxButtons.OK);
return new DataTable();
}
else
{
Dictionary<string ,List<string>> vieDic=new Dictionary<string,List<string>>();
string sourceContent = TxtReader.GetString(filePath);
vieDic = form.AddParasData(sourceContent);
Dictionary<string, List<string>> VieDic1 = new Dictionary<string, List<string>>();
VieDic1.Add("包头1",new List<string>(){"unsigned int","0x1040"});
VieDic1.Add("包头2", new List<string>() { "unsigned int", "0xc000" });
Dictionary<string,string> lengthAndSum=form.GetDataLengthAanSum_Add();
VieDic1.Add("包长",new List<string>(){"unsigned int",lengthAndSum["包长"]});
VieDic1.Add("累加和", new List<string>() { "unsigned char", lengthAndSum["累加和"] });
if (lengthAndSum.ContainsKey("填充码"))
{
VieDic1.Add("填充码", new List<string>() { "unsigned char", lengthAndSum["填充码"] });
}
Dictionary<string, List<string>> VieDic2 = new Dictionary<string, List<string>>();
VieDic2.Add("包头1",VieDic1["包头1"]);
VieDic2.Add("包头2",VieDic1["包头2"]);
VieDic2.Add("包长", VieDic1["包长"]);
VieDic2.Add("识别码", new List<string>() { "unsigned int", form.txtMarkNum1.Text.Trim() });
foreach(var item in vieDic)
{
VieDic2.Add(item.Key,item.Value);
}
dt = new DataTable();
dt.Columns.Add("参数名");
dt.Columns.Add("参数类型");
dt.Columns.Add("参数值");
foreach (var item in VieDic2)
{
dt.Rows.Add(item.Key, item.Value[0], item.Value[1]);
}
dt.Rows.Add("累加和", VieDic1["累加和"][0], VieDic1["累加和"][1]);
if(VieDic1.ContainsKey("填充码"))
{
dt.Rows.Add("填充码", VieDic1["填充码"][0], VieDic1["填充码"][1]);
}
return dt;
}
}
dt.Rows.Add("累加和", VieDic1["累加和"][0], VieDic1["累加和"][1]);
if(VieDic1.ContainsKey("填充码"))
{
dt.Rows.Add("填充码", VieDic1["填充码"][0], VieDic1["填充码"][1]);
}
return dt;
}
}
#endregion
public static string DataTableToString(DataTable dt)//获取表字符串
{
string dtstring = "";
for (int i = 0; i < dt.Columns.Count; i++)
{
dtstring = dtstring + dt.Columns[i].ColumnName + "t";
}
dtstring = dtstring + "rn";
for (int i = 0; i < dt.Rows.Count; i++)
{
for (int j = 0; j < dt.Columns.Count; j++)
{
dtstring = dtstring + dt.Rows[i][j] + "t";
}
dtstring = dtstring + "rn";
}
return dtstring;
}
public static string GetFindStringContentFromDatatable(DataTable dt,string str)//获取指定字符串行
{
string content;
foreach (DataRow dr in dt.Rows)
{
if (dr[0].ToString() == str)
{
content = dr[0].ToString() + "t" + dr[1].ToString() + "t" + dr[2].ToString()+"t";
return content;
}
}
return null;
}
public static string GetValueStringOfDataGridView(DataGridView dg)//获取数据表中参数值
{
string dataTemp = DataServer.DataTableToString((DataTable)dg.DataSource);
string slipStr = DataServer.GetFindStringContentFromDatatable((DataTable)dg.DataSource, "识别码");
{
string dataTemp = DataServer.DataTableToString((DataTable)dg.DataSource);
string slipStr = DataServer.GetFindStringContentFromDatatable((DataTable)dg.DataSource, "识别码");
string[] sArray = Regex.Split(dataTemp, slipStr, RegexOptions.IgnoreCase);
slipStr = DataServer.GetFindStringContentFromDatatable((DataTable)dg.DataSource, "累加和");
sArray = Regex.Split(sArray[1], slipStr, RegexOptions.IgnoreCase);
return sArray[0];
}
public static void ChangeValueInTheDataGridView(DataGridView dg,string parameterName,string value)//改变数据表中指定参数值
{
DataTable dt = (DataTable)dg.DataSource;
if (dt == null) return;
string dtstring = value ;
for (int i = 0; i < dt.Rows.Count; i++)
{
for (int j = 0; j < dt.Columns.Count; j++)
{
if (dt.Rows[i][j].ToString() == parameterName)
{
dt.Rows[i][2] = value;
return;
}
}
}
slipStr = DataServer.GetFindStringContentFromDatatable((DataTable)dg.DataSource, "累加和");
sArray = Regex.Split(sArray[1], slipStr, RegexOptions.IgnoreCase);
return sArray[0];
}
public static void ChangeValueInTheDataGridView(DataGridView dg,string parameterName,string value)//改变数据表中指定参数值
{
DataTable dt = (DataTable)dg.DataSource;
if (dt == null) return;
string dtstring = value ;
for (int i = 0; i < dt.Rows.Count; i++)
{
for (int j = 0; j < dt.Columns.Count; j++)
{
if (dt.Rows[i][j].ToString() == parameterName)
{
dt.Rows[i][2] = value;
return;
}
}
}
}
public static int GetIndexWhereUseTheParameter(DataGridView dg, string parameterName)//获得数据表中指定参数名所在的索引
{
DataTable dt = (DataTable)dg.DataSource;
for (int i = 0; i < dt.Rows.Count; i++)
{
for (int j = 0; j < dt.Columns.Count; j++)
{
if (dt.Rows[i][j].ToString() == parameterName)
{
return i;
}
}
public static int GetIndexWhereUseTheParameter(DataGridView dg, string parameterName)//获得数据表中指定参数名所在的索引
{
DataTable dt = (DataTable)dg.DataSource;
for (int i = 0; i < dt.Rows.Count; i++)
{
for (int j = 0; j < dt.Columns.Count; j++)
{
if (dt.Rows[i][j].ToString() == parameterName)
{
return i;
}
}
}
return 0;
return 0;
}
}
}
}
}
最后
以上就是留胡子枫叶为你收集整理的dataSerVer操作方法总结的全部内容,希望文章能够帮你解决dataSerVer操作方法总结所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复