Example #1
0
        public static void Init()
        {
            var db = new SqlSugarScope(new SqlSugar.ConnectionConfig()
            {
                ConnectionString      = Config.ConnectionString,
                DbType                = SqlSugar.DbType.PostgreSQL,
                IsAutoCloseConnection = true
            });

            db.Aop.OnLogExecuted = (s, p) =>
            {
                Console.WriteLine(s);
            };
            //db.CodeFirst.InitTables<User_Test001>();
            var list = db.Queryable <User_Test001>().ToList();


            var dt = new DataTable();

            dt.Columns.Add("ID", typeof(int));
            dt.Columns.Add("UserName");
            dt.TableName = "public.unitUser_Test001"; //设置表名
            var addRow = dt.NewRow();

            addRow["ID"]       = 1;
            addRow["UserName"] = "******";
            dt.Rows.Add(addRow);                                       //添加娄据

            var x = db.Storageable(dt).WhereColumns("id").ToStorage(); //id作为主键
        }
Example #2
0
        public static void Init()
        {
            var db = new SqlSugarScope(new SqlSugar.ConnectionConfig()
            {
                ConnectionString      = Config.ConnectionString,
                DbType                = DbType.SqlServer,
                IsAutoCloseConnection = true
            });

            db.Aop.OnLogExecuted = (s, p) =>
            {
                Console.WriteLine(s);
            };
            //建表
            if (!db.DbMaintenance.IsAnyTable("User_Test001", false))
            {
                db.CodeFirst.InitTables <User_Test001>();
            }
            if (!db.DbMaintenance.IsAnyTable("UserRole_Test001", false))
            {
                db.CodeFirst.InitTables <UserRole_Test001>();
            }

            //用例代码
            var result = db.Queryable <User_Test001, UserRole_Test001>((u, ur) => new object[] {
                JoinType.Left, u.ID == ur.UserID
            }).Select((u, ur) => new

            {
                customName = SqlFunc.Subqueryable <User_Test001>().Where(s => s.UserName == u.UserName).Select(s => s.UserName + "")
            }).ToPageList(1, 10);
        }
Example #3
0
        private static void Fastest2()
        {
            var db = new SqlSugarScope(new SqlSugar.ConnectionConfig()
            {
                ConnectionString      = Config.ConnectionString,
                DbType                = DbType.SqlServer,
                IsAutoCloseConnection = true
            });

            db.CodeFirst.InitTables <Test2>();
            db.DbMaintenance.TruncateTable <Test2>();
            //用例代码
            db.Insertable(new Test2()
            {
                p = "1"
            }).ExecuteCommand();                                    //用例代码

            db.Insertable(new Test2()
            {
                p = "2", delPer = 1
            }).ExecuteCommand();                                                //用例代码

            var updateList = db.Queryable <Test2>()
                             .ToList();

            db.Fastest <Test2>().BulkCopy(updateList);

            int index = 0;

            foreach (var update in updateList)
            {
                update.p = index.ToString();

                index++;
            }

            db.Fastest <Test2>().BulkUpdate(updateList);

            Console.WriteLine("用例跑完");
        }
Example #4
0
        public static void UConfig()
        {
            SqlSugarScope db = new SqlSugarScope(new ConnectionConfig()
            {
                ConfigId              = "1",
                DbType                = DbType.SqlServer,
                ConnectionString      = Config.ConnectionString,
                InitKeyType           = InitKeyType.Attribute,
                IsAutoCloseConnection = true,
                AopEvents             = new AopEvents
                {
                    OnLogExecuting = (s, p) =>
                    {
                        Console.WriteLine(s);
                        Console.WriteLine(string.Join(",", p?.Select(it => it.ParameterName + ":" + it.Value)));
                    }
                },
                MoreSettings = new ConnMoreSettings()
                {
                    IsWithNoLockQuery = true
                }
            });


            var sql = db.Queryable <Order>().ToSql();

            if (!sql.Key.Contains("WITH(NOLOCK)"))
            {
                throw new Exception("unit config error");
            }

            db.CurrentConnectionConfig.MoreSettings = null;

            sql = db.Queryable <Order>().ToSql();
            if (sql.Key.Contains("WITH(NOLOCK)"))
            {
                throw new Exception("unit config error");
            }
            if (db.CurrentConnectionConfig.ConfigId != "1")
            {
                throw new Exception("unit config error");
            }

            db.CurrentConnectionConfig.IsAutoCloseConnection = false;

            Task.Run(() =>
            {
                if (db.CurrentConnectionConfig.IsAutoCloseConnection == false)
                {
                    throw new Exception("unit config error");
                }
            });
            System.Threading.Thread.Sleep(1000);
            if (db.CurrentConnectionConfig.IsAutoCloseConnection == true)
            {
                throw new Exception("unit config error");
            }

            if (!db.IsAnyConnection("0"))
            {
                db.AddConnection(new ConnectionConfig()
                {
                    ConfigId              = "0",
                    DbType                = DbType.SqlServer,
                    ConnectionString      = Config.ConnectionString,
                    InitKeyType           = InitKeyType.Attribute,
                    IsAutoCloseConnection = true,
                    AopEvents             = new AopEvents
                    {
                        OnLogExecuting = (s, p) =>
                        {
                            Console.WriteLine(s);
                            Console.WriteLine(string.Join(",", p?.Select(it => it.ParameterName + ":" + it.Value)));
                        }
                    },
                    MoreSettings = new ConnMoreSettings()
                    {
                        IsWithNoLockQuery = true
                    }
                });
            }
            var x = db.GetConnection("0");

            if (db.IsAnyConnection("0") == false)
            {
                throw new Exception("unit config error");
            }
            Task.Run(() =>
            {
                if (db.IsAnyConnection("0"))
                {
                    throw new Exception("unit config error");
                }
                else
                {
                    db.AddConnection(new ConnectionConfig()
                    {
                        ConfigId              = "11",
                        DbType                = DbType.SqlServer,
                        ConnectionString      = Config.ConnectionString,
                        InitKeyType           = InitKeyType.Attribute,
                        IsAutoCloseConnection = true,
                        AopEvents             = new AopEvents
                        {
                            OnLogExecuting = (s, p) =>
                            {
                                Console.WriteLine(s);
                                Console.WriteLine(string.Join(",", p?.Select(it => it.ParameterName + ":" + it.Value)));
                            }
                        },
                        MoreSettings = new ConnMoreSettings()
                        {
                            IsWithNoLockQuery = true
                        }
                    });
                }
            });
            System.Threading.Thread.Sleep(1000);
        }