Csharp/C#教程:OLEDB自定义编码分享


OLEDB自定义编码

我的目标

我需要以客户端指定的特定格式创建.dbf文件。 格式为dBase III .dbf,带有kamenicky编码,使用Integer,各种长度的字符和Double列类型。

问题

我几乎所有工作都在工作,只有一个障碍:尽管编写了一个特定的转换表来切换与kamenicky编码兼容的原始字符,但darn编码拒绝工作。 这意味着输出文件以例如对于char的HEX值FF结束,该char在导入的字符串中被指定为A0的hex值。

如果你打算(-1)这个问题,我会非常感谢你为什么在评论中这样做 – 即使是“你不能充分理解这个问题”也会有很大的帮助,因为我知道在哪里继续我的研究(如在那种情况下非常基础)

我有点,有点解决问题(见评论),但解决方案有缺陷,实际上根本没有回答给定的问题。

我如何说服Jet.OLEDB提供商不要弄乱编码?

我试过了什么

一些代码

连接字符串:

"Provider=Microsoft.Jet.OLEDB.4.0; Data Source={0};Extended Properties="dBase III;""; 

然后使用OleDbCommand插入数据,单个单元格填充OleDbParameter类并构造插入字符串。 可能没用,但这是代码:

 private void insertRows(T[] data, OleDbConnection connection) { using (OleDbCommand command = connection.CreateCommand()) { for (int i = 0; i < data.Count(); i++) { constructParams(data[i], i, command); command.CommandText = constructInsert(i, _fileName); command.ExecuteNonQuery(); } } } private void constructParams(T data, int index, OleDbCommand command) { command.Parameters.Clear(); foreach (PropertyInfo prop in _props) { if(_cols.ContainsKey(prop.Name)) { command.Parameters.Add(new OleDbParameter("@" + prop.Name + index, prop.GetValue(data))); } } } private string constructInsert(int dataNum, string tableName) { string insert = "INSERT INTO [" + tableName + "] ("; foreach(string key in _cols.Keys) { insert += "[" + key + "],"; } insert = insert.Remove(insert.Length - 1); insert += ") VALUES"; insert += " ("; foreach (string key in _cols.Keys) { insert += "@" + key + dataNum + ","; } insert = insert.Remove(insert.Length - 1); insert += ");"; return insert; } 

这是我尝试过的一个快速的东西,似乎正在使用特殊的Unicode字符,并在您尝试使用时正确识别代码页895。 这确实使用了Microsoft的VFP OleDb提供程序。 但是,我有4个部分。

  1. 使用显式代码页引用创建表,通常会生成VFP可读格式。

  2. 要保留dBASE中所需的向后兼容性,请使用COPY TO命令将VFP版本表头转换为旧的(应该是)dBASE识别格式

  3. 简单插入表的dBASE版本(也是代码页895)

  4. 检索所有记录并查看Unicode结果。

 // Connection to your data path, but explicitly referencing codepage 895 in connection string string connectionString = @"Provider=VFPOLEDB.1;Data Source=c:\YourDataPath\SomeSubFolder;CODEPAGE=895;"; string ans = ""; using (OleDbConnection connection = new OleDbConnection(connectionString)) { // create table syntax for a free table (not part of a database) that is codepage 895. string cmd = "create table MyTest1 free codepage=895 ( oneColumn c(10) )"; OleDbCommand command = new OleDbCommand(cmd, connection); connection.Open(); command.ExecuteNonQuery(); // Now, create a script to use the MyTest1 table and create MyTest2 which // SHOULD BE recognized in dBASE format. string vfpScript = @"use MyTest1 Copy to MyTest2 type foxplus"; command.CommandType = CommandType.StoredProcedure; command.CommandText = "ExecScript"; command.Parameters.Add("myScript", OleDbType.Char).Value = vfpScript; command.ExecuteNonQuery(); // Simple insert into the 2nd instance of the table command = new OleDbCommand("insert into Mytest2 ( oneColumn ) values ( ? )", connection); command.Parameters.AddWithValue("parmForColumn", "çšjír_Þ‰"); command.ExecuteNonQuery(); // Now, get the data back. command = new OleDbCommand("select * from Mytest2", connection); OleDbDataAdapter da = new OleDbDataAdapter(command); DataTable oTbl = new DataTable(); da.Fill(oTbl); if (oTbl.Rows.Count != 0) // we should have one row, so get the string from the column // and it SHOULD loo like the Unicode sample I inserted above. ans = (string)oTbl.Rows[0]["oneColumn"]; } 

显然,你有代码循环遍历所有列并设置适用的参数,所以我把它留给你。

上述就是C#学习教程:OLEDB自定义编码分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/cdevelopment/1009101.html

(0)
上一篇 2021年12月29日
下一篇 2021年12月29日

精彩推荐