连接数据库
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();
}
}
}
以上是关于连接数据库的主要内容,如果未能解决你的问题,请参考以下文章