日期:2014-05-18 浏览次数:21046 次
//依靠反射原理获得所要的数据库表对象和其属性 private void register() { string[] columnlist = new string[13]; string[] values = new string[8]; string[] tablelist = new string[4]; TextBox[] tbs = new TextBox[8]; tablelist[0] = "T_Controler_Info"; tablelist[1] = "T_Acount_Info"; tablelist[2] = "T_Acount_State_Info"; tablelist[3] = "T_Acount_History_Info"; int j=0; for (int i=0;i<tablelist.Length ;i++ ) { Assembly _Assembly = Assembly.Load("System.Object");//这行报错。。。 Type _Type = _Assembly.GetType("创扬电货物管理系统.Bean.Table."+tablelist[i]); FieldInfo[] fs=_Type.GetFields(); foreach (FieldInfo f in fs) { if (!f.Name.Substring(f.Name.Length-3,3).Equals("_id")) { if (j < 12) { columnlist[j] = f.Name; } else { break; } } j++; } } int k = 0; foreach (Control c in this.Controls) { if (k < 9) { if (c.GetType() == typeof(TextBox)) { tbs[k] = (TextBox)c; k++; } } else { break; } } for (int i = 0; i < values.Length; i++) { if (i != 2) { values[i] = tbs[i].Text; } else { continue; } } string sql = SqlCreater.GetSqlCreate("insert", tablelist, columnlist, values, null); MyMessageBox mm=new MyMessageBox(); try { if( SQLHelper.SetSqlData(sql) > 0 ) { mm.setMessage("插入成功!", "信息"); } else { mm.setMessage("插入失败!", "错误"); } }catch(Exception e) { mm.setMessage(e.Message, "错误"); } }