/// <summary> /// 测试数据源的连接。 /// </summary> public virtual Result TestConnection() { Result r = new Result(); DbConnection conn = null; try { using(conn = this.Provider.CreateConnection()) { conn.Open(); } } catch(Exception ex) { r.ToFailded(ex); } finally { if(conn != null) { conn.TryClose(); } } return r; }
/// <summary> /// 写入可序列化的对象。 /// </summary> /// <typeparam name="TData">可序列化对象的类型。</typeparam> /// <param name="stream">可序列化的流。</param> /// <param name="data">可序列化的对象。</param> /// <returns>返回一个结果,指示序列化是否成功。</returns> public Result Write <TData>(Stream stream, TData data) { Result result = new Result(); try { this.Writing <TData>(stream, data); } catch (Exception ex) { result.ToFailded(new System.IO.IOException("对象 {0} 序列化失败。".Fmt((data == null ? typeof(TData) : data.GetType()).FullName), ex)); } finally { this.OnWriteFinish(result); } return(result); }
/// <summary> /// 读取对象。 /// </summary> /// <param name="stream">序列化的流。</param> /// <returns>返回序列化对象的结果。</returns> public Result <object> Read(Stream stream) { Result <object> result = new Result <object>(); try { result.Value = this.Reading(stream); } catch (Exception ex) { result.ToFailded(new System.IO.IOException("对象反序列化失败。", ex)); } finally { this.OnReadFinish(result); } return(result); }
/// <summary> /// 读取对象。 /// </summary> /// <typeparam name="TData">可序列化对象的类型。</typeparam> /// <param name="stream">序列化的流。</param> /// <returns>返回序列化对象的结果。</returns> public Result <TData> Read <TData>(Stream stream) { Result <TData> result = new Result <TData>(); try { result.Value = (TData)this.Reading <TData>(stream); } catch (Exception ex) { result.ToFailded(new System.IO.IOException("对象 {0} 反序列化失败。".Fmt(typeof(TData).FullName), ex)); } finally { this.OnReadFinish(result); } return(result); }
/// <summary> /// 写入可序列化的对象。 /// </summary> /// <typeparam name="TData">可序列化对象的类型。</typeparam> /// <param name="data">可序列化为字符串的对象。</param> /// <param name="encoding">自定义字符编码。</param> /// <returns>返回一个结果,指示序列化是否成功。</returns> public Result <string> WriteString <TData>(TData data, Encoding encoding = null) { Result <string> result = new Result <string>(); using (MemoryStream stream = new MemoryStream()) { var r = this.Write <TData>(stream, data); if (r.IsSucceed) { stream.Position = 0; var v = stream.ToArray(); result.Value = (encoding ?? this.Encoding).GetString(v, 0, v.Length); } else { result.ToFailded(r.Exception); } } return(result); }