示例#1
0
        public static IEnumerable <TProxy> Get <TProxy>(this SqlConnectionString cs, string sql)
            where TProxy : new()
        {
            var table = cs.GetDataTable(sql);

            return(table.ReadProxies <TProxy>());
        }
示例#2
0
        //private static DataTable GetDataTable(this SqlConnectionString cs, SqlTableQuery query)
        //{
        //    var dataTable = new DataTable(query.SchemaQualifiedTableName);
        //    var sql = query.ToSql();
        //    return cs.GetDataTable(sql, dataTable);
        //}

        /// <summary>
        /// Opens a connection to an identified data source
        /// </summary>
        /// <param name="cs">Provides access to the data source</param>
        /// <returns></returns>
        public static SqlConnection OpenConnection(this SqlConnectionString cs)
        {
            var connection = new SqlConnection(cs);

            connection.Open();
            return(connection);
        }
示例#3
0
 public static IEnumerable <SchemaTableRecord> GetResultSchema(this SqlConnectionString cs, string sql)
 {
     using (var connection = cs.OpenConnection())
     {
         using (var command = connection.CreateCommand(sql))
         {
             var table = command.GetSchemaTable();
             return(table.ReadProxies <SchemaTableRecord>());
         }
     }
 }
示例#4
0
        public static void GenerateProxiesFromDb(string path, string @namespace, string connStr,
                                                 IEnumerable <string> tableNames = null, IEnumerable <string> udttNames = null, string schema = "dbo")
        {
            var sqlConnStr = new SqlConnectionString(connStr);

            sqlConnStr.GenerateProxiesFromDb(
                tableNames ?? sqlConnStr.GetDbObjectNames(TableType.TableOrView, null, schema),
                udttNames ?? sqlConnStr.GetDbObjectNames(TableType.Udtt, null, schema),
                path, @namespace, schema
                );
        }
示例#5
0
        public static void GenerateProxiesFromDb(string path, string @namespace, string connStr,
                                                 string prefix = null, bool includeTablesAndViews = true, bool includeUdtts = true, string schema = "dbo")
        {
            var sqlConnStr = new SqlConnectionString(connStr);

            sqlConnStr.GenerateProxiesFromDb(
                includeTablesAndViews ? sqlConnStr.GetDbObjectNames(TableType.TableOrView, prefix, schema) : null,
                includeUdtts ? sqlConnStr.GetDbObjectNames(TableType.Udtt, prefix, schema) : null,
                path, @namespace, schema
                );
        }
示例#6
0
        private static DataTable GetDataTable(this SqlConnectionString cs, string sql, DataTable dst = null)
        {
            var dataTable = dst ?? new DataTable();

            using (var connection = cs.OpenConnection())
            {
                using (var command = connection.CreateCommand(sql))
                {
                    using (var adapter = new SqlDataAdapter(command))
                    {
                        adapter.Fill(dataTable);
                    }
                    return(dataTable);
                }
            }
        }
示例#7
0
        internal static IEnumerable <string> GetDbObjectNames(this SqlConnectionString sqlConnStr,
                                                              TableType type, string prefix = null, string schema = null)
        {
            var clause = "";

            if (!string.IsNullOrEmpty(prefix))
            {
                clause = string.Format(GetNamesClauses[type], prefix);
            }
            if (!string.IsNullOrEmpty(schema))
            {
                clause += string.Format(GetNamesSchemaClauses[type], schema);
            }
            var cmd = string.Format(GetNamesCommands[type], clause);

            return(sqlConnStr.GetDbObjectNames(cmd));
        }
示例#8
0
        private static void GenerateProxiesFromDb(this SqlConnectionString sqlConnStr, IEnumerable <string> dbObjectNames,
                                                  TableType type, string path, string @namespace, string schema)
        {
            if (dbObjectNames == null)
            {
                return;
            }
            foreach (var name in dbObjectNames)
            {
                var script =
                    sqlConnStr
                    .GetResultSchema(string.Format(GetEmptyDataCommands[type], schema, name))?
                    .ToTypeTemplate(name, @namespace)?
                    .Expand();

                script?.SaveToFile(path ?? @namespace.Replace('.', '\\'), name + ".cs");
            }
        }
示例#9
0
 private static void GenerateProxiesFromDb(this SqlConnectionString sqlConnStr, IEnumerable <string> tableNames, IEnumerable <string> udttNames,
                                           string path, string @namespace, string schema)
 {
     sqlConnStr.GenerateProxiesFromDb(tableNames, TableType.TableOrView, path, @namespace, schema);
     sqlConnStr.GenerateProxiesFromDb(udttNames, TableType.Udtt, path, @namespace, schema);
 }
示例#10
0
        private static IEnumerable <string> GetDbObjectNames(this SqlConnectionString sqlConnStr, string cmd)
        {
            var result = sqlConnStr.Get <StringWrapper>(cmd).Select(x => x.Value);

            return(result);
        }