Ejemplo n.º 1
0
        /// <summary>
        /// 根据表名获取实体类的字符串
        /// </summary>
        /// <param name="db"></param>
        /// <param name="sql"></param>
        /// <param name="className"></param>
        /// <returns></returns>
        public string TableNameToClass(SqlSugarClient db, string tableName)
        {
            var dt           = db.GetDataTable(string.Format("select   * from {0} limit 0,1", tableName));
            var tableColumns = GetTableColumns(db, tableName);
            var reval        = DataTableToClass(dt, tableName, null, tableColumns);

            return(reval);
        }
Ejemplo n.º 2
0
        /// <summary>
        ///  创建SQL实体文件,指定表名
        /// </summary>
        public void CreateClassFilesByTableNames(SqlSugarClient db, string fileDirectory, string nameSpace, params string[] tableNames)
        {
            string sql    = GetCreateClassSql(null);
            var    tables = db.GetDataTable(sql);

            if (tables != null && tables.Rows.Count > 0)
            {
                foreach (DataRow dr in tables.Rows)
                {
                    string tableName = dr["name"].ToString().ToLower();
                    if (tableNames.Any(it => it.ToLower() == tableName))
                    {
                        var currentTable = db.GetDataTable(string.Format("select top 1 * from {0}", tableName));
                        var classCode    = DataTableToClass(currentTable, tableName, nameSpace);
                        FileSugar.WriteText(fileDirectory.TrimEnd('\\') + "\\" + tableName + ".cs", classCode);
                    }
                }
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// 多线程请求所有数据库节点,同步汇总结果
        /// </summary>
        /// <typeparam name="T">支持DataTable、实体类和值类型</typeparam>
        /// <param name="sql"></param>
        /// <param name="connectionStringList">连接字符串数组</param>
        /// <param name="whereObj">参数 例如: new { id="1",name="张三"}</param>
        /// <returns></returns>
        public Taskable <T> Taskable <T>(string sql, List <string> connectionStringList, object whereObj = null)
        {
            Taskable <T> reval = new Taskable <T>();

            reval.Sql      = sql;
            reval.WhereObj = whereObj;
            var tasks = new Task <CloudSearchResult <T> > [connectionStringList.Count];

            for (int i = 0; i < tasks.Length; i++)
            {
                CloudPubMethod.TaskFactory <CloudSearchResult <T> >(ti =>
                {
                    string innerSql = sql;
                    var connString  = connectionStringList[ti];
                    var db          = new SqlSugarClient(connString);
                    SettingConnection(db);
                    CloudSearchResult <T> itemReval = new CloudSearchResult <T>();
                    var isDataTable = typeof(T) == typeof(DataTable);
                    var isClass     = typeof(T).IsClass;
                    if (innerSql.Contains("$:->connectionString<-:$"))
                    {
                        innerSql = innerSql.Replace("$:->connectionString<-:$", connString);
                    }
                    if (isDataTable)
                    {
                        itemReval.DataTable = db.GetDataTable(innerSql, whereObj);
                    }
                    else if (isClass)
                    {
                        itemReval.Entities = db.SqlQuery <T>(innerSql, whereObj);
                    }
                    else
                    {
                        var obj = db.GetScalar(sql, whereObj);
                        if (obj == DBNull.Value)
                        {
                            itemReval.Value = default(T);
                        }
                        else
                        {
                            obj             = Convert.ChangeType(obj, typeof(T));
                            itemReval.Value = (T)obj;
                        }
                    }
                    itemReval.ConnectionString = connString;
                    return(itemReval);
                }, tasks, i);
            }
            Task.WaitAll(tasks);
            reval.Tasks = tasks;
            return(reval);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// 多线程请求所有数据库节点,同步汇总结果
        /// </summary>
        /// <typeparam name="T">支持DataTable、实体类和值类型</typeparam>
        /// <param name="sqlSelect">sql from之前(例如: "select count(*)" )</param>
        /// <param name="sqlEnd">sql from之后(例如: "from table where id=1" </param>
        /// <param name="whereObj">参数 例如: new { id="1",name="张三"}</param>
        /// <returns></returns>
        public TaskableWithCount <T> TaskableWithCount <T>(string sqlSelect, string sqlEnd, object whereObj = null)
        {
            TaskableWithCount <T> reval = new TaskableWithCount <T>();

            reval.Sql      = sqlSelect + sqlEnd;
            reval.WhereObj = whereObj;
            var tasks = new Task <CloudSearchResult <T> > [configList.Count];

            for (int i = 0; i < tasks.Length; i++)
            {
                CloudPubMethod.TaskFactory <CloudSearchResult <T> >(ti =>
                {
                    var connString = configList[ti].ConnectionString;
                    var db         = new SqlSugarClient(connString);
                    SettingConnection(db);
                    CloudSearchResult <T> itemReval = new CloudSearchResult <T>();
                    var isDataTable = typeof(T) == typeof(DataTable);
                    var isClass     = typeof(T).IsClass;
                    if (isClass)
                    {
                        itemReval.Entities = db.SqlQuery <T>(reval.Sql, whereObj);
                    }
                    else if (isDataTable)
                    {
                        itemReval.DataTable = db.GetDataTable(reval.Sql, whereObj);
                    }
                    else
                    {
                        var obj         = db.GetScalar(reval.Sql, whereObj);
                        obj             = Convert.ChangeType(obj, typeof(T));
                        itemReval.Value = (T)obj;
                    }
                    itemReval.Count            = db.GetInt("SELECT COUNT(1)" + sqlEnd);;
                    itemReval.ConnectionString = connString;
                    return(itemReval);
                }, tasks, i);
            }
            Task.WaitAll(tasks);
            reval.Tasks = tasks;
            return(reval);
        }