SDF文件转换为CSV文件。 [英] SDF file convert to CSV file.

查看:392
本文介绍了SDF文件转换为CSV文件。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿,我正在尝试制作一个可以将SDF文件传输到CSV文件的转换器。但我不知道为什么它不起作用..你在这里看到的部分我几乎已经离开互联网..



Hey i am trying to make a converter which can transfer a SDF File into a CSV File. But i don't know why it isn't working.. The section which ya can see here I almost got out of the internet..

private void button2_Click(object sender, EventArgs e)
        {
            
            SqlCeConnection conn = null;
            SqlCeCommand cmd = null;
            SqlCeDataReader rdr = null;

            try
            {
                System.IO.TextWriter stm = new System.IO.StreamWriter(new System.IO.FileStream(@"D:\\TEST\Data.csv", System.IO.FileMode.Create), Encoding.Default);
                while ()
                {
                    
                    for (int i = 0; i < rdr.FieldCount - 2; i++)
                    {
                        if (rdr[i] != null)
                        {
                            stm.Write(rdr[i].ToString());
                            stm.Write(";");
                        }
                        else
                        {
                            stm.Write(";");
                        }
                    }
                    if (rdr[rdr.FieldCount - 1] != null)
                    {
                        stm.Write(rdr[0].ToString());
                    }
                    stm.Write(System.Environment.NewLine);
                }
                stm.Close();
                rdr.Close();
                cmd.Dispose();
            }
            finally
            {
                conn.Close();
            }
        }





我的尝试:



没什么,我在互联网上抬头但找不到任何东西......



What I have tried:

Nothing, i looked up in the internet but couldn't find anything...

推荐答案

它不起作用因为你永远不会给 rdr 分配任何东西:

It won't work because you never assign anything to rdr:
SqlCeDataReader rdr = null;

try
{
    System.IO.TextWriter stm = new System.IO.StreamWriter(new System.IO.FileStream(@"D:\\TEST\Data.csv", System.IO.FileMode.Create), Encoding.Default);
    while ()
    {

        for (int i = 0; i < rdr.FieldCount - 2; i++)

until你这样做,总是会失败并带有空引用异常。



和顺便说一句:在互联网上寻找代码并试图在不知道你是什么的情况下使用它做不是试图转换器 - 它希望并祈祷奇迹发生。它不会。即使您发现工作代码完全按照您在其他人的程序中完成的工作,您需要了解代码及其工作原理,然后将其更改为在您的工作中完成该工作...

Until you do, that will always fail with a null reference exception.

And BTW: looking for code on the internet and trying to use it without knowing anything about what you are doing is not "trying to make a converter" - it's hoping and praying for a miracle to occur. It won't. Even if you find working code that does exactly the job you need it to in someone else's program, you need to understand the code and how it works, and then change it to do that job in yours...


重用网上的一些代码需要理解它。



在编写(或复制)任何代码之前要做的第一件事就是熟悉要处理的数据。在您的情况下,这些是 SQL Server Compact - Wikipedia [ ^ ]和以逗号分隔的值 - 维基百科 [ ^ ]。



对于后者,除了缺少的引用支持外,你的代码看起来基本上没问题(应该引用包含换行符,双引号或分隔字符的字段,并且字段中的双引号必须是用另一个双引号逃脱。



但它永远不会起作用,因为你的 rdr null



您必须知道如何使用 DataReader 。所使用的类的文档涵盖了这一点,如 SqlCeDataReader类(System.Data.SqlServerCe) [ ^ ]其中也包含示例代码。



最后,你必须使用SQL语句适用于您的数据库文件。也就是说,您必须知道要导出到CSV文件的表的名称以及从表中查询数据的SQL语法。
Reusing some code found on the net requires understanding it.

The first thing to do before writing (or copying) any code is becoming familiar with the data to be processed. In your case these are SQL Server Compact - Wikipedia[^] and Comma-separated values - Wikipedia[^].

For the latter your code looks basically fine besides the missing quote support (fields containing a line-break, double-quote or the delimiting character should be quoted and double quotes in fields must be escaped with another double quote).

But it will never work because your rdr is null.

You have to know how to access SDF files using a DataReader. That is covered by the documentation of the used classes like SqlCeDataReader Class (System.Data.SqlServerCe)[^] which contains also example code.

Finally, you have to use SQL statements that apply to your database file. That is, you have to know the name(s) of the table(s) you want to export to CSV file(s) and the SQL syntax to query data from a table.


这篇关于SDF文件转换为CSV文件。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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