DALBase Dapper数据库链接

/// <summary>
        /// 获取链接
        /// </summary>
        /// <param name="MYorSqlServer">0 sqlserver ,1 mysql</param>
        /// <returns></returns>
        public IDbConnection DALBaseConn( )
        {
            IDbConnection conn = null;
            ServerSet linkModel = new ServerSet();
            linkModel = JsonToIni.GetClass<ServerSet>(ConfigFiles.ConfigFile);
            if (linkModel != null)
            {
                string defaultcon = ""; 
                    defaultcon = $"Data Source ={linkModel.DBAdd}; Initial Catalog ={linkModel.DBName}; User Id = {linkModel.DBUser}; Password ={linkModel.DBPwd}";
                    try
                    {
                        conn = new SqlConnection(defaultcon);
                        conn.Open();

                    }
                    catch (Exception ex)
                    {
                        string ErrMsg = string.Format("sql数据库连接错误,错误原因是:{0}", ex.Message);
                        Common.LogWrite.WriteLog(ErrMsg);
                        conn = null;
                        //throw new Exception(ErrMsg);
                    }  
            }
            return conn;
        }

不做解释。。。
其他程序使用方法是:

 public ResultMsg Insert(SBRecordMain model)
        {
            ResultMsg msg = new ResultMsg();
            using (IDbConnection conn = new DALBase().DALBaseConn())
            {
                IDbTransaction transaction = conn.BeginTransaction();
                try
                {
                    string sql = $@"
                    DECLARE   @SBIdIn   int; 
                    IF Not EXISTS(select  1 from  SBInfo where  Name =@Name)
                    begin 
                        insert into [SBInfo] ([Name],[SBType],[SBNo],[IsUsed],[Remark],[CreateDate],[Updated] ) values (@Name,'流量计','',1,'采集数据',getdate(),getdate());
                        set @SBIdIn=  @@IDENTITY; ---获取插入的数据ID
                    end 
                    else 
                     begin 
                           select   @SBIdIn=Id  from  SBInfo where  Name =@Name 
                     end 
                     INSERT [SBRecordMain]( 
                    [Name],
                    [SBType],
                    [SBNo],
                    SBId,
                    [RecordCount],
                    [Dianya],
                    [XinhaoZhiliang],
                    [Remark],
                    [CreateDate]
                    )VALUES( 
                    @Name,
                    @SBType,
                    @SBNo,
                    @SBIdIn,
                    @RecordCount,
                    @Dianya,
                    @XinhaoZhiliang,
                    @Remark,
                    getdate());
                    SELECT @@IDENTITY";
                    msg.ReturnInt = conn.ExecuteScalar<int>(sql, model, transaction);
                    msg.Success = true;
                    if (model.ListMX != null)
                    {
                        model.ListMX.ForEach(x => x.MainInfoId = msg.ReturnInt);
                        new DALSBRecordMx().UpdateAll(model.ListMX, transaction, conn);
                    }
                    transaction.Commit();//提交事务
                    msg.Success = true;
                }
                catch (Exception ex)
                {
                    if (transaction != null)
                    {
                        transaction.Rollback();
                    }
                    msg.Success = false;
                    msg.ErrMsg = ex.Message;
                }
            }
            return msg;
        }

查询:

 /// <summary>
        /// 获取1000数据
        /// </summary>
        /// <param name="Model">SBRecordMain</param>
        /// <returns></returns>
        public IEnumerable<SBRecordMain> QueryList(string where)
        { 
            using (IDbConnection conn = new DALBase().DALBaseConn())
            { 
                try
                {
                    string sql = $@"select top 1000  * from SBRecordMain where 1=1 {where} order by CreateDate desc ";
                    return conn.Query<SBRecordMain>(sql);
                }
                catch (Exception ex)
                { 
                    return null;
                }
            }
        }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值