/// <summary> /// 得到工厂层代码 /// </summary> /// <param name="param"></param> /// <returns></returns> public string GetFactoryClass(Model.CodeCreate param) { Model.Servers server = Config.GetServer(param.ServerID); if (server == null) { return(string.Empty); } List <Model.Fields> fields = databaseInstance.GetFields(server.ID, param.DbName, param.TableName); StringBuilder factory = new StringBuilder(import.GetImport_Factory()); factory.Append("namespace " + param.NameSpace + (param.NameSpace == null ? "" : ".") + param.CNSC.Factory + (param.NameSpace1 == null ? "" : "." + param.NameSpace1) + "\r\n"); factory.Append("{\r\n"); factory.Append("\tpublic class " + param.ClassName + "\r\n"); factory.Append("\t{\r\n"); factory.Append("\t\t/// <summary>\r\n"); factory.Append("\t\t/// 创建实例对象\r\n"); factory.Append("\t\t/// </summary>\r\n"); factory.Append("\t\tpublic static " + param.NameSpace + (param.NameSpace == null ? "" : ".") + param.CNSC.Interface + (param.NameSpace1 == null ? "" : "." + param.NameSpace1) + ".I" + param.ClassName + " CreateInstance()\r\n"); factory.Append("\t\t{\r\n"); factory.Append("\t\t\treturn CreateInstance(\"" + param.ClassName + "\") as " + param.CNSC.Interface + ".I" + param.ClassName + ";\r\n"); factory.Append("\t\t}\r\n"); factory.Append("\t\tpublic static object CreateInstance(string dllName)\r\n"); factory.Append("\t\t{\r\n"); factory.Append("\t\t\treturn Assembly.Load(\"" + param.CNSC.Data + "\").CreateInstance(string.Format(\"" + param.CNSC.Data + ".{1}" + "\"" + ",dllName));\r\n"); factory.Append("\t\t}\r\n"); factory.Append("\t}\r\n"); factory.Append("}\r\n"); return(factory.ToString()); }
/// <summary> /// 注销数据库 /// </summary> public void RemoveServer() { if (treeView1.SelectedNode == null) { return; } TreeNode rootNode = GetRoot(treeView1.SelectedNode); if (rootNode == null) { return; } Model.TreeNodeTag tag = (Model.TreeNodeTag)rootNode.Tag; if (tag.Type != TreeNodeType.Server) { return; } Model.Servers server = (Model.Servers)tag.Tag; new Config_Servers().Delete(server.ID); rootNode.Remove(); }
/// <summary> /// 得到一个表中所有字段 /// </summary> /// <param name="serverID"></param> /// <param name="dbName"></param> /// <param name="tableName"></param> /// <returns></returns> public static List <Model.Fields> GetFields(string serverID, string dbName, string tableName) { List <Model.Fields> fieldsList = new List <Model.Fields>(); Model.Servers server = Config.GetServer(serverID); if (server == null) { return(fieldsList); } using (SqlConnection conn = new SqlConnection(Config.GetConnectionString(serverID, dbName))) { conn.Open(); string sql = server.Type == Model.DatabaseType.SqlServer2000 ? string.Format(@"SELECT name=a.name,isidentity=case when COLUMNPROPERTY( a.id,a.name,'IsIdentity')=1 then 1 else 0 end,isprimarykey=case when exists(SELECT 1 FROM sysobjects where xtype='PK' and parent_obj=a.id and name in (SELECT name FROM sysindexes WHERE indid in(SELECT indid FROM sysindexkeys WHERE id = a.id AND colid=a.colid))) then 1 else 0 end,type=b.name,bbyte=a.length,length=COLUMNPROPERTY(a.id,a.name,'PRECISION'), dec=isnull(COLUMNPROPERTY(a.id,a.name,'Scale'),0),isnull=case when a.isnullable=1 then 1 else 0 end, isdefault=isnull(e.text,''),note=isnull(g.[value],'')FROM syscolumns a left join systypes b on a.xusertype=b.xusertype inner join sysobjects d on a.id=d.id and d.xtype='U' and d.name<>'dtproperties' left join syscomments e on a.cdefault=e.id left join sysproperties g on a.id=g.id and a.colid=g.smallid left join sysproperties f on d.id=f.id and f.smallid=0 where d.name='{0}' order by a.id,a.colorder", tableName) : string.Format(@"select a.name as f_name,b.name as t_name,[length],a.isnullable as is_null from sys.syscolumns a inner join sys.types b on b.user_type_id=a.xtype where object_id('{0}')=id order by a.colid", tableName); using (SqlCommand cmd = new SqlCommand(sql, conn)) { SqlDataReader dr = cmd.ExecuteReader(); if (server.Type != Model.DatabaseType.SqlServer2000) { while (dr.Read()) { fieldsList.Add(new Model.Fields() { Name = dr.GetString(0), Type = dr.GetString(1), Length = GetFieldLength(dr.GetString(1), dr.GetInt16(2)), IsNull = 1 == dr.GetInt32(3), IsPrimaryKey = IsPrimaryKey(serverID, dbName, tableName, dr.GetString(0)), IsIdentity = IsIdentity(serverID, dbName, tableName, dr.GetString(0)), DotNetType = GetFieldType(dr.GetString(1), 1 == dr.GetInt32(3)), DotNetSqlType = GetFieldSqlType(dr.GetString(1)), Note = GetFieldNote(serverID, dbName, tableName, dr.GetString(0)) }); } } else { while (dr.Read()) { fieldsList.Add(new Model.Fields() { Name = dr["name"].ToString(), Type = dr["type"].ToString(), Length = GetFieldLength(dr["type"].ToString(), dr["length"].ToString().ToInt()), IsNull = "1" == dr["isnull"].ToString(), IsPrimaryKey = "1" == dr["isprimarykey"].ToString(), IsIdentity = "1" == dr["isidentity"].ToString(), DotNetType = GetFieldType(dr["type"].ToString(), "1" == dr["isnull"].ToString()), DotNetSqlType = GetFieldSqlType(dr["type"].ToString()), Note = dr["note"].ToString() }); } } dr.Close(); dr.Dispose(); } } return(fieldsList); }