using SunlightCentralizedControlManagement_SCCM_.EX; using SunlightCentralizedControlManagement_SCCM_.UserClass; using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.ComponentModel; using System.Data; using System.Data.SqlClient; using System.Linq; using System.Reflection; using System.Text; using System.Threading.Tasks; namespace SunlightCentralizedControlManagement_SCCM_.ViewModel { public class ManualDyelotModel : ViewModelBase { public static DataTable STUFFdataTable = new DataTable(); //建立STUFF public ManualDyelotModel() { string Connstr_SC; string sql = "SELECT * FROM [dbo].[Pipes] WHERE Dispenser = '" + ManualDyelot.Machines + "'"; try { if (MainWindowViewModel.SQMOD == "Windows Authentication") { Connstr_SC = "server=" + MainWindowViewModel.SQLIP + ";database=" + MainWindowViewModel.SQLNAME + ";Trusted_Connection=SSPI"; } else { Connstr_SC = "server=" + MainWindowViewModel.SQLIP + ";database=" + MainWindowViewModel.SQLNAME + ";User ID=" + MainWindowViewModel.SQLUSER + ";Password=" + MainWindowViewModel.SQLPASWORD; } SqlConnection conn_SC = new SqlConnection(Connstr_SC); conn_SC.Open(); //连接数据库 new SqlDataAdapter(sql, Connstr_SC).Fill(STUFFdataTable); // SqlDataAdapter _data = new SqlDataAdapter(sql, Connstr_SC); //查询 conn_SC.Close(); } catch (Exception ex) { LogGing.ERRDATA(ex); } stuff_Product = ToObservableCollection(STUFFdataTable); //stuff_Product表转换 } public ObservableCollection stuff_Product { get; set; } //stuff_Product动态表实力化 public ObservableCollection ToObservableCollection(DataTable dt) where T : class, new() //DataTable FOR ObservableCollection转换器 { Type t = typeof(T); PropertyInfo[] propertys = t.GetProperties(); ObservableCollection lst = new ObservableCollection(); string typeName = string.Empty; foreach (DataRow dr in dt.Rows) { T entity = new T(); foreach (PropertyInfo pi in propertys) { typeName = pi.Name; if (dt.Columns.Contains(typeName)) { if (!pi.CanWrite) continue; object value = dr[typeName]; if (value == DBNull.Value) continue; if (pi.PropertyType == typeof(string)) { pi.SetValue(entity, value.ToString(), null); } else if (pi.PropertyType == typeof(int) || pi.PropertyType == typeof(int?)) { pi.SetValue(entity, int.Parse(value.ToString()), null); } else if (pi.PropertyType == typeof(DateTime?) || pi.PropertyType == typeof(DateTime)) { pi.SetValue(entity, DateTime.Parse(value.ToString()), null); } else if (pi.PropertyType == typeof(float)) { pi.SetValue(entity, float.Parse(value.ToString()), null); } else if (pi.PropertyType == typeof(double)) { pi.SetValue(entity, double.Parse(value.ToString()), null); } else { pi.SetValue(entity, value, null); } } } lst.Add(entity); } return lst; } //DataTable FOR ObservableCollection转换器 public class Product //stuff_Product { public string ProductCode { get; set; } public string ProductName { get; set; } public int ProductType { get; set; } public int Concentration { get; set; } public override string ToString() { return ProductCode; } } } }