Wednesday, April 29, 2009

SQL Server CE Export

I ran into what is probably not a unique problem. I designed a CE database thinking I could export the structure as a SQL script as you do for SQL Server. I googled and didn't manage to find any exporters (I found a few that export from SQL Server to SQL Server Compact Edition). I ended up writing my own. The enclosed version is far from complete and will only export tables with standard fields and primary keys. This is fine for what I have done to date but I might add some more features later.

The program is a console progam.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlServerCe;

namespace ScriptCEDatabase
{
    class Program
    {
        static void Main(string[] args)
        {
            if (args.GetUpperBound(0) <>
            {
                Console.WriteLine("Usage:ScriptCEDatabase {Dbfile} {sqlfilepath} [password]");
                return;
            }
                System.Data.SqlServerCe.SqlCeConnection conn = new SqlCeConnection();
                conn.ConnectionString = "Data Source=" + args[0] + "; Persist Security Info=True";
                if (args.GetUpperBound(0) == 2)
                    conn.ConnectionString = conn.ConnectionString + ";password=" + args[2];
                conn.Open();
                SqlCeCommand TableList = new SqlCeCommand("select * from information_schema.tables", conn);
                SqlCeDataReader TableReader = TableList.ExecuteReader();

                string filename = args[1];

                if (filename.EndsWith("\\")) filename += "\\";
                filename += System.Guid.NewGuid().ToString() + ".sql";
                System.IO.StreamWriter sw = new System.IO.StreamWriter(filename, false);
 
                while (TableReader.Read())
                {
                    Console.WriteLine("CREATE TABLE [" + TableReader["TABLE_NAME"].ToString() + "] (");
                    sw.WriteLine("CREATE TABLE [" + TableReader["TABLE_NAME"].ToString() + "] (");
                    SqlCeCommand FieldList = new SqlCeCommand("select * from information_schema.columns where TABLE_NAME = '" + TableReader["TABLE_NAME"].ToString() + "'"
                        + " order by ordinal_position",conn );
                    SqlCeDataReader ColumnReader = FieldList.ExecuteReader();
                    bool isFirst = true;
                    while (ColumnReader.Read())
                    {
                        if (!isFirst)
                            sw.Write(",");
                        else
                            isFirst = false;

                        Console.Write( "[" + ColumnReader["Column_name"] + "] " + ColumnReader["DATA_TYPE"]);
                        if (ColumnReader["DATA_TYPE"].ToString() == "nchar" || ColumnReader["DATA_TYPE"].ToString() == "nvarchar")
                            Console.Write("(" + ColumnReader["CHARACTER_MAXIMUM_LENGTH"].ToString() + ")");
                        if (ColumnReader["IS_NULLABLE"].ToString() == "YES")
                            Console.WriteLine(" NULL,");
                        else
                            Console.WriteLine(" NOT NULL,");
                            sw.Write(ColumnReader["Column_name"] + " " + ColumnReader["DATA_TYPE"]);
                        if (ColumnReader["DATA_TYPE"].ToString() == "nchar" || ColumnReader["DATA_TYPE"].ToString() == "nvarchar")
                            sw.Write("(" + ColumnReader["CHARACTER_MAXIMUM_LENGTH"].ToString() + ")");
                        if (ColumnReader["IS_NULLABLE"].ToString() == "YES")
                            sw.WriteLine(" NULL");
                        else
                            sw.WriteLine(" NOT NULL");
                    }
                    ColumnReader.Close();
                    Console.WriteLine(");");
                    sw.WriteLine(");");
                    sw.WriteLine();

                }
                TableReader.Close();
            // Indexes
                
                SqlCeCommand cmdIndex = new SqlCeCommand("SELECT * FROM INFORMATION_SCHEMA.INDEXES", conn);
                SqlCeDataReader rdrIndex = cmdIndex.ExecuteReader();
                while(rdrIndex.Read()){
                    if  ( rdrIndex["PRIMARY_KEY"].ToString() == "True")
                    {
                        Console.WriteLine ("ALTER TABLE " + rdrIndex["TABLE_NAME"] + " ADD CONSTRAINT " + rdrIndex["INDEX_NAME"] + " PRIMARY KEY (" + rdrIndex["COLUMN_NAME"] + ");");
                        sw.WriteLine();
                        sw.WriteLine ("ALTER TABLE [" + rdrIndex["TABLE_NAME"] + "] ADD CONSTRAINT " + rdrIndex["INDEX_NAME"] + " PRIMARY KEY ( [" + rdrIndex["COLUMN_NAME"] + "] );"); 
                    }
                }

                sw.Close();
        }
    }
}

Thursday, April 9, 2009

Computer immediately logs off

I had an XP computer that immediately logged off once the user logged onto the computer. I slaved the drive to my workshop computer and found that the userinit.exe was missing from c:\windows\system32. I copied the file over from c:\windows\system32\dllcache and the computer rebooted.

The fact that this file just disappeared was interesting. I think that what happened was that their anti-virus program zapped a bug while it was still installing itself. Reading further on, sometimes the problem can be a bad setting in HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\WindowsNT\CurrentVersion\Winlogon\userinit where the setting should be the same file c:\windows\system32\userinit.exe, . Note the comma.

Saturday, March 28, 2009

Office Get Genuine Warnings

Microsoft with it's update cycle has released an update which checks to see if the version of Office on a computer is genuine. This is similar to what is happening with the operating system. A lot of customers will be surprised, especially those who have had the computer setup by a well meaning family member of friend who "borrowed" some office disks from their work.

The biggest shock will be the cost of getting genuine. I haven't got the details of actual charges but the retail cost of Office professional runs at over $500 much more than the $240 cost for the operating system get genuine campaign.

I can see that a lot more people will get caught out as well because most computers come with XP or Vista but Office needs to be purchased as an extra.

Wednesday, November 12, 2008

WinAPI OpenProcess returns details for killed processes

I have just finished writing a program that involves a dispatch program that starts and manages sub programs. The loop was using OpenProcess to find if the process was still alive. The OpenProcess function returns a handle. The handle is supposed to be zero if the process doesn't exist. However, I found that after the process died, it still returned a valid handle (and many of the functions using the handle returned valid values).

To find out if the process is alive or not I finally found that using the function

ret = GetExitCodeProcess( handle, &ExitCode );

works. It returns an exit code of 259 if the process has not failed.

Friday, September 19, 2008

Programming in C

I have a customer who wants me to do some programming in 'c'. It is like back to my childhood again and I am thoroughly enjoying. The job involves sockets data transfers and the best samples I can find are unfortunately on hacker sites. Their code is pretty ordinary and you get the sense that they are particularly young. Their coding style is pretty average but I suppose that if they were good coders they would have real jobs.
I didn't have a c compiler or IDE so I started with LLC. I liked it and it had a lot of nice features but the editor drove me crazy. I next downloaded NetBeans. I think that it is supposed to be a Java IDE but has a C/C++ version. However, you also need a C compiler to go with it.

It supports a number of compilers but I chose MinGW. To get it all working well you need to do things in this order:

Download and install MinGW and put the bin folder in your path statement. Download and install the latest debugger (gdb). To make the MAKE work you need to download and installed MSYS. It will have a little command file that will normalise it with MinGW.
Finally you can download NetBeans. I feel a little like going onto the dark-side given what the CEO of Sun has said about Microsoft over the years.
You can finally download