OLEDB custom encoding

后端 未结 1 492
野趣味
野趣味 2020-12-12 07:08

My goal

I need to create a .dbf file in a specific format, specified by a client. The format being dBase III .dbf with kamenicky encoding, using Int

相关标签:
1条回答
  • 2020-12-12 07:43

    Here is a quick something I tried and appears to be working with special Unicode characters and proper recognition of codepage 895 as you are trying to work with. This does use the VFP OleDb provider from Microsoft. However, I have as 4 parts.

    1. Create the table with explicit codepage reference which typically results in VFP readable format.

    2. To retain backward compatibility as you mentioned you needed in dBASE, use the COPY TO command to convert the VFP version table header to the older (and should be) dBASE recognized format

    3. Simple insert into the dBASE version of the table (also codepage 895)

    4. Retrieve all records and look at the Unicode results.

    // 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"];
    }
    

    Obviously you have code to cycle through all columns and set applicable parameters, so I leave that up to you.

    0 讨论(0)
提交回复
热议问题