//根据表名的到该表字段名 private void listBox_Field_Init(string TableName) { PublicBD.Path = System.Windows.Forms.Application.StartupPath + "/Resave/System.xml"; PublicBD.DB = "MSSQL"; PublicBD.ReInit(); _51Data dt = PublicBD.db.dt as _51Data; DataTable datatable = dt.Select("SysColumns", new string [] { "Name" }, "Where id=Object_Id('" + TableName + "')"); if (datatable != null && datatable.Rows.Count > 0) { for (int i = 0; i < datatable.Rows.Count; i++) { listBox_Field.Items.Add(datatable.Rows[i][0]); } } }
//测试成功,表List初始化 private bool listBox_Table_Init() { listBox_Table.Items.Clear(); PublicBD.Path = System.Windows.Forms.Application.StartupPath + "/Resave/System.xml"; PublicBD.DB = "MSSQL"; PublicBD.ReInit(); _51Data dt = PublicBD.db.dt as _51Data; DataTable datatable = dt.Select("sys.indexes a , sys.objects b , sys.dm_db_partition_stats c", new string [] { "b.name as tablename ,c.row_count as datacount" }, "where a.[object_id] = b.[object_id] AND b.[object_id] = c.[object_id] AND a.index_id = c.index_id AND a.index_id < 2 AND b.is_ms_shipped = 0"); if (datatable != null && datatable.Rows.Count > 0) { for (int i = 0; i < datatable.Rows.Count; i++) { listBox_Table.Items.Add(datatable.Rows[i][0].ToString()); } return(true); } else { return(false); } }
private void DBtoDB() { #region 读取转存字段配置信息 List <DataResaveClass> list = xmlToList <DataResaveClass>(ReadFieldToFieldXML()); List <string> tables = new List <string>(); foreach (var item in list) { tables.Add(item.TableName); } tables = tables.Distinct().ToList(); #endregion List <string> fields; List <object> values; List <object> values_1; string whereAnd = ""; foreach (var table in tables) { var l = from L in list where table == L.TableName select L; fields = new List <string>(); values = new List <object>(); values_1 = new List <object>(); foreach (var item in l) { fields.Add(item.NewField); if (item.OldField == "元 素" || item.OldField == "其 他") { values.Add(item.Value); values_1.Add(item.Value); bool b = true; if (item.OldField == "元 素") { if (b) { whereAnd += " and ItemID='" + item.Value + "'"; b = false; } else { whereAnd += " or ItemID='" + item.Value + "'"; } } } else if (item.OldField == "站 号") { values.Add("[0]"); values_1.Add("[0]"); } else if (item.OldField == "监测时间") { values.Add("[1]"); values_1.Add("[1]"); } else if (item.OldField == "接收时间") { values.Add("[2]"); values_1.Add("[2]"); } else if (item.OldField == "值") { values.Add("[3]"); values_1.Add("[3]"); } } #region 读取原始数据库中的数据 string datetime = ReadDateTimeXML(); datetime = DateTime.Parse(datetime).AddSeconds(-60).ToString("yyyy-MM-dd HH:mm:ss"); PublicBD.Path = System.Windows.Forms.Application.StartupPath + "/System.xml"; PublicBD.DB = "MSSQL"; PublicBD.ReInit(); _51Data dt = PublicBD.db.dt as _51Data; DataTable datatable = dt.Select("YY_DATA_AUTO", new string[] { "*" }, "where DOWNDATE>='" + datetime + "'" + whereAnd); #endregion if (datatable != null && datatable.Rows.Count > 0) { for (int i = 0; i < datatable.Rows.Count; i++) { for (int j = 0; j < values.Count; j++) { if (values_1[j].ToString() == "[0]") { values[j] = datatable.Rows[i]["STCD"].ToString(); } else if (values_1[j].ToString() == "[1]") { values[j] = datatable.Rows[i]["TM"].ToString(); } else if (values_1[j].ToString() == "[2]") { values[j] = datatable.Rows[i]["DOWNDATE"].ToString(); } else if (values_1[j].ToString() == "[3]") { values[j] = datatable.Rows[i]["DATAVALUE"].ToString(); } } //写入转存库,并记录成功失败数量 PublicBD.Path = System.Windows.Forms.Application.StartupPath + "/Resave/System.xml"; PublicBD.DB = "MSSQL"; PublicBD.ReInit(); _51Data newdt = PublicBD.db.dt as _51Data; int B = newdt.Insert_1(table, fields.ToArray(), values.ToArray()); if (B == 1) { Success++; } else if (B == 2) { Key++; } else { Failure++; } } } } }