public bool TransactionOPEntitysCommit(Func <IDbConnection, IDbTransaction, bool> fun)
        {
            try
            {
                using (var cn = DbConnectionHelper.CreateConnection())
                {
                    cn.Open();


                    IDbTransaction transaction = cn.BeginTransaction();

                    if (fun(cn, transaction))
                    {
                        transaction.Commit();
                    }

                    cn.Close();
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
                return(false);
            }


            return(true);
        }
 public string GetEntityViewJson <TV>(string sql, object param = null)
 {
     try
     {
         using (var cn = DbConnectionHelper.CreateConnection())
         {
             cn.Open();
             var itemList = cn.Query <TV>(sql, param).ToList();
             cn.Close();
             if (itemList != null)
             {
                 return(JsonUtil.toJson(new { Msg = true, Content = JsonUtil.toJson(itemList) }));
             }
             else
             {
                 return(JsonUtil.toJson(new { Msg = false }));
             }
         }
     }
     catch (Exception ex)
     {
         Debug.WriteLine(ex.Message);
         return(JsonUtil.toJson(new { Msg = false }));
     }
 }
Exemple #3
0
        public void CreateConnectionTest()
        {
            DbSource dbSource = new DbSource();

            dbSource.Provider = "SqlClient Data Provider";
            DbConnectionHelper helper     = new DbConnectionHelper(dbSource);
            DbConnection       connection = helper.CreateConnection();

            Assert.IsNotNull(connection);
        }
        public string Execute(string sql, object param = null)
        {
            try
            {
                using (var cn = DbConnectionHelper.CreateConnection())
                {
                    cn.Open();
                    var item = cn.Execute(sql, param);//返回命令影响的行数
                    cn.Close();

                    return(JsonUtil.toJson(new { Msg = true, Content = item }));
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
                return(JsonUtil.toJson(new { Msg = false }));
            }
        }
        public string GetNewSerialNumber(string serialtype)
        {
            try
            {
                using (var cn = DbConnectionHelper.CreateConnection())
                {
                    lock (_lock)
                    {
                        var para = new DynamicParameters();
                        para.Add("@SerialType", serialtype);
                        para.Add("@SerialNumber", string.Empty, DbType.String, ParameterDirection.Output);

                        var res1 = cn.Query("proc_GetSerialNumber", para, null, true, null, CommandType.StoredProcedure);  //0

                        return(para.Get <string>("@SerialNumber"));
                    }
                }
            }
            catch
            {
                return(null);
            }
        }
Exemple #6
0
        protected RepositoryOptionsBuilder GetRepositoryOptionsBuilder(ContextProviderType provider)
        {
            var builder = new RepositoryOptionsBuilder();

            switch (provider)
            {
            case ContextProviderType.InMemory:
            {
                builder.UseInMemoryDatabase(Guid.NewGuid().ToString());
                break;
            }

            case ContextProviderType.Json:
            {
                builder.UseJsonDatabase(Path.GetTempPath() + Guid.NewGuid().ToString("N"));
                break;
            }

            case ContextProviderType.Xml:
            {
                builder.UseXmlDatabase(Path.GetTempPath() + Guid.NewGuid().ToString("N"));
                break;
            }

            case ContextProviderType.AdoNet:
            {
                builder.UseAdoNet(DbConnectionHelper.CreateConnection(), ensureDatabaseCreated: true);
                break;
            }

            case ContextProviderType.NHibernate:
            {
                builder.UseNHibernate(cfg =>
                    {
                        var currentFile      = PathHelper.GetTempFileName();
                        var connectionString = $"Data Source={currentFile};Persist Security Info=False";

                        cfg.DataBaseIntegration(x =>
                        {
                            x.Dialect <TestFixedMsSqlCe40Dialect>();
                            x.Driver <SqlServerCeDriver>();
                            x.ConnectionString = connectionString;
                            x.LogSqlInConsole  = true;
                            x.LogFormattedSql  = true;
                        });

                        var mapper = new ModelMapper();

                        mapper.AddMappings(Assembly.GetExecutingAssembly().GetExportedTypes());

                        var mapping = mapper.CompileMappingForAllExplicitlyAddedEntities();

                        cfg.AddMapping(mapping);

                        var exporter = new SchemaExport(cfg);

                        exporter.Execute(true, true, false);
                    });

                break;
            }

            case ContextProviderType.EntityFramework:
            {
                builder.UseEntityFramework <TestEfDbContext>(DbConnectionHelper.CreateConnection());
                break;
            }

            case ContextProviderType.EntityFrameworkCore:
            {
                builder.UseEntityFrameworkCore <TestEfCoreDbContext>(options =>
                    {
                        options
                        .UseInMemoryDatabase(Guid.NewGuid().ToString())
                        .ConfigureWarnings(x => x.Ignore(InMemoryEventId.TransactionIgnoredWarning));
                    });
                break;
            }

            case ContextProviderType.AzureStorageBlob:
            {
                builder.UseAzureStorageBlob(
                    nameOrConnectionString: "AzureStorageBlobConnection",
                    container: Guid.NewGuid().ToString(),
                    createIfNotExists: true);
                break;
            }

            case ContextProviderType.AzureStorageTable:
            {
                builder.UseAzureStorageTable(
                    nameOrConnectionString: "AzureStorageTableConnection",
                    tableName: "TableName" + Guid.NewGuid().ToString("N").ToUpper(),
                    createIfNotExists: true);
                break;
            }

            default:
                throw new ArgumentOutOfRangeException(nameof(provider));
            }

            builder.UseLoggerProvider(TestXUnitLoggerProvider);

            return(builder);
        }