具有blob字段到CSV的数据表 [英] Datatable with blob fields to CSV

查看:194
本文介绍了具有blob字段到CSV的数据表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要转换DataTable,将Blob作为其字段的一部分转换为CSV。

所以我可以将MySqlBulkCopy转换为MySql数据库。

我已成功复制所有除了图像数据以外的列。

我需要帮助。

Thak you。



什么我试过了:



我已成功批量删除所有列,除了它复制它的blob coumn并在数据库中插入System.Byte []



这是要插入数据库的代码



Please i want to COnvert DataTable thta has Blob as part of its fields to CSV.
so i can do MySqlBulkCopy to MySql Database.
I have sucessfully copy all columns except the image data.
I need help please.
Thak you.

What I have tried:

I have successfully bulkcopy all columns except the blob coumn it copies it and inserts "System.Byte[]" in the database

This is the code to insert to Database

IGRDal dl = SQLDalFactory.GetDal(GrConnector.AccessSQLDal);

             string tempCsvFileSpec = APP_PATH+ @"\dump.csv";

             using (StreamWriter writer = new StreamWriter(tempCsvFileSpec))
             {
                 DBHelper.WriteDataTable(dl.CreateDataTable(), writer, false);
             }
             dbConection.Open();
             var msbl = new MySqlBulkLoader(dbConection);
             msbl.TableName = "tb_templates";
             msbl.FileName = tempCsvFileSpec;
             msbl.FieldTerminator = ",";
             msbl.FieldQuotationCharacter = '"';
             msbl.Load();
             File.Delete(tempCsvFileSpec);



this is the code to make CSV
public static void WriteDataTable(DataTable sourceTable, TextWriter writer, bool includeHeaders)
        {
            if (includeHeaders)
            {
                IEnumerable<string> headerValues = sourceTable.Columns
                    .OfType<datacolumn>()
                    .Select(column => QuoteValue(column.ColumnName));

                writer.WriteLine(String.Join(",", headerValues));
            }

            IEnumerable<string> items = null;

            foreach (DataRow row in sourceTable.Rows)
            {
                items = row.ItemArray.Select(o => QuoteValue(o.ToString()));
                writer.WriteLine(String.Join(",", items));
            }

            writer.Flush();
        }

        private static string QuoteValue(string value)
        {
            return String.Concat("\"",
            value.Replace("\"", "\"\""), "\"");
        }</string></datacolumn></string> 

推荐答案

这是因为您在创建CSV数据时使用字符串连接,或者当您保存到数据库时(可能是原始商店或新版本 - 没有我们无法分辨的代码)

这与此处描述的问题类似:为什么我得到参数无效。我从数据库中读取图像时出现异常? [ ^ ] - 它是相同的解决方案或非常类似!
That's because you are using string concatenation, either when you create the CSV data, or when you save to the DB (could be the original store or the new version - without the code we can't tell)
It's a similar problem to the one described here: Why do I get a "Parameter is not valid." exception when I read an image from my database?[^] - and it's the same solution or very much like it!


这篇关于具有blob字段到CSV的数据表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆