C#SQL客户端处理大文本数据通用接口
C#SQL客户端处理大文本数据通用接口
A uniform interface for large texts for a C# SQL clientBy AlexS9999The article provides a simple interface for handling large text in SqlClient and C# file streams. We also might want to change the implementation from SQL storage to file storage. So, we suggested a uniform interface and provided two implementations of it: one for C# text streams and one for the SQL Server database. 译: 我们有可能要把SQL的数据存储到本地文件。所以我们建议策划出一个通用接口,并提供这两种实现方式: public interface ICharsHandler { char[] GetChars(long offset, int length); // read data chunk void PutChars(long offset, char[] buffer); // put data chunk void Close(); // release the resources (recordset, connection or stream) } The implementation for the text writers follows: public class StreamTextHandler : ICharsHandler { TextReader reader; TextWriter writer; char[] buffer; public StreamTextHandler(TextWriter wr, TextReader rd) { reader = rd; writer = wr; } #region ICharsHandler Members public char[] GetChars(long offset, int length) { if (reader == null) throw new InvalidOperationException("Can’t read data"); if (buffer == null || buffer.Length != length) buffer = new char[length]; int cnt = reader.Read(buffer, (int)offset, length); if (cnt < length) { char[] nv = new char[cnt]; Array.Copy(buffer, nv, cnt); return nv; } return buffer; } public void PutChars(long offset, char[] buffer) { if (writer == null) throw new InvalidOperationException("Can’t write data"); writer.Write(buffer, (int)offset, buffer.Length); } public void Close() { if (reader != null) reader.Close(); if (writer != null) writer.Close(); } #endregion } Maybe, it would be a good idea to split this interface to a "reader" and a "writer". class SqlTextHandler : ICharsHandler { SqlCommand readCommand; SqlCommand writeCommand; int column; SqlDataReader rd; bool previousConn = false; public SqlTextHandler(SqlCommand cmd, SqlCommand wr, int _column) { readCommand = cmd; writeCommand = wr; column = _column; previousConn = (wr != null) ? wr.Connection.State == ConnectionState.Open : cmd.Connection.State == ConnectionState.Open; } protected void OpenReader() { readCommand.Connection.Open(); rd = readCommand.ExecuteReader(CommandBehavior.SequentialAccess | CommandBehavior.SingleRow); rd.Read(); } // We assume that the input command // contain variables: @Value, @Offset and @Length protected void OpenWriter() { SqlParameter Out = writeCommand.Parameters.Add("@Value", SqlDbType.NVarChar); SqlParameter OffsetParam = writeCommand.Parameters.Add("@Offset", SqlDbType.BigInt); SqlParameter LengthParam = writeCommand.Parameters.Add("@Length", SqlDbType.Int); writeCommand.Connection.Open(); } char[] buffer; #region ICharsHandler Members public char[] GetChars(long offset, int length) { if (rd == null) OpenReader(); if (buffer == null || buffer.Length != length) { buffer = new char[length]; } long cnt = rd.GetChars(column, offset, buffer, 0, length); if (cnt < length) { char[] nv = new char[cnt]; Array.Copy(buffer, nv, cnt); return nv; } return buffer; } public void PutChars(long offset, char[] buffer) { if (writeCommand.Parameters.Count < 4) OpenWriter(); writeCommand.Parameters["@Length"].Value = buffer.Length; writeCommand.Parameters["@Value"].Value = buffer; writeCommand.Parameters["@Offset"].Value = offset; writeCommand.ExecuteNonQuery(); } public void Close() { if (rd != null) rd.Close(); if (!previousConn) { if (readCommand != null) readCommand.Connection.Close(); if (writeCommand != null) writeCommand.Connection.Close(); } } #endregion } We provide two SQL commands, the The code below shows a sample of input parameters for
{ SqlConnection _connection = new System.Data.SqlClient.SqlConnection(); _connection.ConnectionString = MyApp.Properties.Settings.Default.MyAppConnectionString; SqlCommand cmdWriter = new SqlCommand("UPDATE dbo.MessageUnit" + " SET plainText .WRITE (@Value, @Offset, @Length) WHERE id = @id ", _connection); cmdWriter.Parameters.Add(new SqlParameter("@id", id)); SqlCommand cmdReader = new SqlCommand( "SELECT plainText FROM dbo.MessageUnit WHERE id = @id", _connection); cmdReader.Parameters.Add(new SqlParameter("@id", id)); return new SqlTextHandler(cmdReader, cmdWriter, 0); } An alternative implementation can be based on the Two possible requirements should be mentioned:
A usage sample code may look like: void MoveText(ICharHandler source, ICharHandler target) { long offset = 0; for (; ; ) { char[] buffer = source.GetChars(offset, BUFFER_SIZE); ptext.PutChars(offset, buffer); if (buffer.Length < BUFFER_SIZE) break; offset += BUFFER_SIZE; } } The conclusive notes are:
总结:
参考文档:
通用十、十六进制数据处理类 C#数据访问层主类.(SqlHelper)SQLDataAccess.cs ADO Direct(直连模式)能不能改成客户端在服务端注册后再访问数据库 C# C/S架构下客户、供应商等大批量数据单条记录选取组件 C#多数据库组件包支持MSSQL+Oracle+MySQL+用户操作手册|C/S框架网 C#.NET短信通用接口设计-调用短信API案例|C/S框架网 C/S客户端Winform窗体调用WebApi接口(C# 实例) C#异步操作等待窗体,异步多线程处理数据通用界面(frmThreadOperating) C#多线程异步处理数据通用界面窗体(frmThreadOperating) CSFramework通用自动提交数据工具类(DbDataUpdate),支持MsSQL,MySQL,Oracle三种数据库 C#.NET 处理SQL特殊数据类型Geography/Geometry/Hierarchyid/XML WCF开发框架-客户端采用Certificate认证模式调用基于HTTPS协议的WCF接口 C# C/S架构软件自动升级程序Winform+多数据库(MySQL,MsSQL,Oracle) C#源码-SQLServer数据库生成修改工具-源码下载 C# SqlBulkCopy通用数据同步程序|跨数据库导入数据
其它资料:
什么是C/S结构? | C/S框架核心组成部分 | C/S框架-WebService部署图 | C/S框架-权限管理 | C/S结构系统框架 - 5.1旗舰版介绍 | C/S结构系统框架 - 功能介绍 | C/S结构系统框架 - 产品列表 | C/S结构系统框架 - 应用展示(图) | 三层体系架构详解 | C/S架构轻量级快速开发框架 | C/S框架网客户案例 | WebApi快速开发框架 | C/S框架代码生成器 | 用户授权注册软件系统 | 版本自动升级软件 | 数据库底层应用框架 | CSFramework.CMS内容管理系统 | |