enterprise library 5 Data应用

项目里只需引Microsoft.Practices.EnterpriseLibrary.Data.dll就可以

配置:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="true" />
</configSections>
<dataConfiguration defaultDatabase="spare" ></dataConfiguration>
<connectionStrings>
<add name="spare" connectionString="uid=sa;pwd=123456;Initial catalog=dbit_documentspare;server=." providerName="System.Data.SqlClient" />
</connectionStrings>
</configuration>

引用命名空间:

using Microsoft.Practices.EnterpriseLibrary.Data;

 

protected Database database = null;
protected SqlConnection connection = null;

database = DatabaseFactory.CreateDatabase();
connection = database.CreateConnection() as SqlConnection;

ExecuteScalar:

string cmdText = "INSERT INTO [ERRORLOG] values(@P_CreateDate,@P_Thread,@P_Level,@P_Logger,@P_Message,@P_Exception);select @@identity;";

SqlCommand cmd = new SqlCommand(cmdText, connection);
database.AddInParameter(cmd, "@P_CreateDate", DbType.DateTime, entity.CreateDate);
database.AddInParameter(cmd, "@P_Thread", DbType.String, entity.Thread);
database.AddInParameter(cmd, "@P_Level", DbType.String, entity.Level);
database.AddInParameter(cmd, "@P_Logger", DbType.String, entity.Logger);
database.AddInParameter(cmd, "@P_Message", DbType.String, entity.Message);
database.AddInParameter(cmd, "@P_Exception", DbType.String, entity.Exception);

object o = database.ExecuteScalar(cmd);
entity.Id = Convert.ToInt64(o);

 

ExecuteNonQuery:

string cmdText = "UPDATE [ERRORLOG] SET CreateDate=@P_CreateDate,Thread=@P_Thread,Level=@P_Level,Logger=@P_Logger,Message=@P_Message,Exception=@P_Exception WHERE ID=@P_ID";

SqlCommand cmd = new SqlCommand(cmdText, connection);
database.AddInParameter(cmd, "@P_CreateDate", DbType.DateTime, entity.CreateDate);
database.AddInParameter(cmd, "@P_Thread", DbType.String, entity.Thread);
database.AddInParameter(cmd, "@P_Level", DbType.String, entity.Level);
database.AddInParameter(cmd, "@P_Logger", DbType.String, entity.Logger);
database.AddInParameter(cmd, "@P_Message", DbType.String, entity.Message);
database.AddInParameter(cmd, "@P_Exception", DbType.String, entity.Exception);
database.AddInParameter(cmd, "@P_ID", DbType.Int64, entity.Id);

int i = database.ExecuteNonQuery(cmd);

ExecuteReader:

string cmdText = "SELECT * FROM [ERRORLOG] WHERE ID=@P_ID";
SqlCommand cmd = new SqlCommand(cmdText, connection);
database.AddInParameter(cmd, "@P_ID", DbType.Int64, id);

IDataReader dr = database.ExecuteReader(cmd);
ErrorLog result = null;
try
{
if (dr != null && dr.Read())
{
result = new ErrorLog();
result.CreateDate = dr.GetDateTime(1);
result.Id = id;
result.Thread = dr.GetString(2);
result.Level = dr.GetString(3);
result.Logger = dr.GetString(4);
result.Message = dr.GetString(5);
result.Exception = dr.GetString(6);
}
}
finally
{
dr.Close();
dr.Dispose();
}

你可能感兴趣的:(library)