连接数据库

Posted 梁娜

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了连接数据库相关的知识,希望对你有一定的参考价值。

private string connectionString;
        private void SaveCustomerInfo() {
            var entityConnStr = ConfigurationManager.ConnectionStrings["DcsEntities"].ConnectionString;
            if(!string.IsNullOrEmpty(entityConnStr)) {
                var entityConnection = new EntityConnectionStringBuilder(entityConnStr);
                this.connectionString = entityConnection.ProviderConnectionString;
            }
            var db = DbHelper.GetDbHelp(this.connectionString);
            var sqlInsert = db.GetInsertSql("reportquerylog ", "Id", new[] {
                                "UserId","EnterpriseId","CreateTime","PageName"
                            });
 
            using(var conn = db.CreateDbConnection()) {
                conn.Open();
                var ts = conn.BeginTransaction();
                try {
                    var cmd = db.CreateDbCommand(sqlInsert, conn, null);
                   // cmd.Parameters.Add(db.CreateDbParameter("Id", Guid.NewGuid()));
                    cmd.Parameters.Add(db.CreateDbParameter("UserId", Utils.GetCurrentUserInfo().LoginId));
                    cmd.Parameters.Add(db.CreateDbParameter("EnterpriseId", Utils.GetCurrentUserInfo().EnterpriseId));
                    cmd.Parameters.Add(db.CreateDbParameter("CreateTime", DateTime.Now));
                    cmd.Parameters.Add(db.CreateDbParameter("PageName", "23"));
                    cmd.ExecuteNonQuery();
                    ts.Commit();
 
                } catch(Exception) {
                    ts.Rollback();
                } finally {
                    conn.Close();
                }
            }
        }

以上是关于连接数据库的主要内容,如果未能解决你的问题,请参考以下文章

java数据库连接池最大连接数最小连接数怎么设置

mysql数据库——连接查询(内连接:自然连接,等值连接。外连接:左连接,右连接,全连接)

数据库连接池是啥

怎样建立稳定的长连接的数据库连接池

数据库连接

数据库表连接(内连接,外连接左连接右连接全连接交叉连接)