Files
lserp_cs_5.0/插件库/Zhaizj.Framework/Data/EasyDB.cs
T
2026-07-10 15:25:05 +08:00

143 lines
5.7 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using System;
using System.Collections;
using System.Data;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Xml.Serialization;
using Zhaizj.Framework.Log;
namespace Zhaizj.Framework.Data {
/// <summary>
/// 简易数据库操作工具,兼容多种数据库,可执行sql,返回DataReader等
/// </summary>
public class EasyDB {
private static readonly ILog logger = LogManager.GetLogger( typeof( EasyDB ) );
public static int Execute( String sql, IDbConnection cn ) {
logger.Info( LoggerUtil.SqlPrefix+"execute sql : " + sql );
IDbCommand cmd = DataFactory.GetCommand( sql, cn );
int result = cmd.ExecuteNonQuery();
logger.Info( "affected : " + result );
return result;
}
public static IDataReader ExecuteReader( String sql, IDbConnection cn ) {
logger.Info( LoggerUtil.SqlPrefix+"execute sql" + sql );
return DataFactory.GetCommand( sql, cn ).ExecuteReader();
}
public static Object ExecuteScalar( String sql, IDbConnection cn ) {
Object result = null;
logger.Info( LoggerUtil.SqlPrefix+"execute sql" + sql );
result = DataFactory.GetCommand( sql, cn ).ExecuteScalar();
if (result == DBNull.Value) {
return null;
}
return result;
}
public static DataTable ExecuteTable( String sql, IDbConnection cn ) {
DataTable dataTable = new DataTable();
logger.Info(LoggerUtil.SqlPrefix+ "execute sql" + sql );
DataFactory.GetAdapter( sql, cn ).Fill( dataTable );
return dataTable;
}
//-----------------------------------------------------------------------------------------------------------
public static Hashtable LoadDicFromString( String targetString ) {
Hashtable hashtable = new Hashtable();
if (!strUtil.IsNullOrEmpty( targetString )) {
String target = "";
String[] strArray = targetString.Split( '=' );
if (strArray.Length == 2) {
target = strArray[1];
}
if (strUtil.HasText( target )) {
foreach (String item in target.Split( '|' )) {
String[] arr = item.Split( ':' );
hashtable[arr[0]] = arr[1];
}
}
}
return hashtable;
}
public static Object LoadFromFile( String absFilePath ) {
if (!Zhaizj.Framework.IO.File.Exists( absFilePath )) {
return null;
}
IFormatter formatter = new BinaryFormatter();
Stream serializationStream = new FileStream( absFilePath, FileMode.Open, FileAccess.Read, FileShare.Read );
Object result = formatter.Deserialize( serializationStream );
serializationStream.Close();
return result;
}
public static Object LoadFromString( String targetString, Type targetType ) {
XmlSerializer serializer = new XmlSerializer( targetType );
TextReader textReader = new StringReader( targetString );
Object result = serializer.Deserialize( textReader );
textReader.Close();
return result;
}
public static Object LoadFromXml( String filePath, Type targetType ) {
if (!System.IO.File.Exists( filePath )) {
return null;
}
XmlSerializer serializer = new XmlSerializer( targetType );
Stream stream = new FileStream( filePath, FileMode.Open );
Object result = serializer.Deserialize( stream );
stream.Close();
return result;
}
//-----------------------------------------------------------------------------------------------------------
public static String SaveDicToString( Hashtable tbl ) {
if (tbl == null) {
return null;
}
String str = "HashTable";
StringBuilder builder = new StringBuilder();
builder.Append( str );
builder.Append( "=" );
foreach (DictionaryEntry entry in tbl) {
builder.Append( entry.Key.ToString() + ":" + entry.Value.ToString() + "|" );
}
return builder.ToString().TrimEnd( new char[] { '|' } );
}
public static void SaveToFile( Object target, String absFilePath ) {
IFormatter formatter = new BinaryFormatter();
Stream serializationStream = new FileStream( absFilePath, FileMode.Create, FileAccess.Write, FileShare.None );
formatter.Serialize( serializationStream, target );
serializationStream.Close();
}
public static String SaveToString( Object target ) {
StringBuilder sb = new StringBuilder();
XmlSerializer serializer = new XmlSerializer( target.GetType() );
TextWriter textWriter = new StringWriter( sb );
serializer.Serialize( textWriter, target );
textWriter.Close();
return sb.ToString();
}
public static void SaveToXml( String filePath, Object target ) {
XmlSerializer serializer = new XmlSerializer( target.GetType() );
TextWriter textWriter = new StreamWriter( filePath );
serializer.Serialize( textWriter, target );
textWriter.Close();
}
//-----------------------------------------------------------------------------------------------------------
}
}