Click here to Skip to main content
15,888,242 members
Please Sign up or sign in to vote.
1.24/5 (3 votes)
See more:
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...
Posted
Updated 19-Jul-18 2:58am

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 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...
 
Share this answer
 
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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900