예제 #1
0
        public static void CreateDatabaseTest(MainForm frm)
        {
            PFSQLServerCE40 db               = new PFSQLServerCE40();
            string          filename         = string.Empty;
            string          fileext          = string.Empty;
            string          defaultFileExt   = AppConfig.GetStringValueFromConfigFile("DefaultDatabaseFileExtension", ".sdf");
            string          dataSource       = frm.txtDataSource.Text;
            string          connectionString = string.Empty;
            string          createScript     = string.Empty;

            try
            {
                _msg.Length = 0;
                _msg.Append("Running CreateDatabaseTest ...");
                Program._messageLog.WriteLine(_msg.ToString());

                if (frm.txtDataSource.Text.Trim().Length == 0)
                {
                    throw new System.Exception("You must specify the data source for CreateDatabaseTest.");
                }
                if (Path.GetFileName(frm.txtDataSource.Text) == string.Empty)
                {
                    throw new System.Exception("You must specify the file name for CreateDatabaseTest.");
                }

                filename = Path.GetFileName(frm.txtDataSource.Text);
                fileext  = Path.GetExtension(filename);
                if (fileext != defaultFileExt)
                {
                    dataSource = dataSource + defaultFileExt;
                }

                db.DatabasePath     = dataSource;
                db.DatabasePassword = frm.txtPassword.Text;
                if (frm.chkEncryptionOn.Checked)
                {
                    db.EncryptionOn   = true;
                    db.EncryptionMode = PFSQLServerCE40.GetEncryptionMode(frm.cboEncryptionMode.Text);
                }

                //if sdf file already exists, prompt user on whether or not delete
                if (File.Exists(dataSource))
                {
                    _msg.Length = 0;
                    _msg.Append(dataSource);
                    _msg.Append(" already exists.\r\n");
                    DialogResult res = MessageBox.Show(_msg.ToString(), "Delete File?", MessageBoxButtons.YesNo);
                    if (res == DialogResult.Yes)
                    {
                        File.Delete(dataSource);
                    }
                }

                connectionString = db.ConnectionString;
                bool dbCreated = db.CreateDatabase(connectionString);
                _msg.Length = 0;
                if (dbCreated)
                {
                    _msg.Append("Database create successful.");
                }
                else
                {
                    _msg.Append("Database create failed.");
                }

                Program._messageLog.WriteLine(_msg.ToString());


                Program._messageLog.WriteLine("\r\nCreating a table in the new database ...");
                DataTable  dt = new DataTable("TestTabX1");
                DataColumn k1 = new DataColumn("K1", Type.GetType("System.Int32"));
                k1.AllowDBNull = false;
                dt.Columns.Add(k1);
                DataColumn f1 = new DataColumn("F1", Type.GetType("System.String"));
                f1.MaxLength = 50;
                dt.Columns.Add(f1);
                DataColumn f1a = new DataColumn("F1A", Type.GetType("System.String"));
                f1a.MaxLength = 50000;
                dt.Columns.Add(f1a);
                DataColumn f2 = new DataColumn("F2", Type.GetType("System.Int32"));
                dt.Columns.Add(f2);
                DataColumn f2a = new DataColumn("F2A", Type.GetType("System.UInt32"));
                dt.Columns.Add(f2a);
                DataColumn f3 = new DataColumn("F3", Type.GetType("System.Int64"));
                dt.Columns.Add(f3);
                DataColumn f3a = new DataColumn("F3A", Type.GetType("System.UInt64"));
                dt.Columns.Add(f3a);
                DataColumn f4 = new DataColumn("F4", Type.GetType("System.Int16"));
                dt.Columns.Add(f4);
                DataColumn f4a = new DataColumn("F4A", Type.GetType("System.UInt16"));
                dt.Columns.Add(f4a);
                DataColumn f5 = new DataColumn("F5", Type.GetType("System.Double"));
                dt.Columns.Add(f5);
                DataColumn f6 = new DataColumn("F6", Type.GetType("System.Single"));
                dt.Columns.Add(f6);
                DataColumn f7 = new DataColumn("F7", Type.GetType("System.Decimal"));
                dt.Columns.Add(f7);
                DataColumn f8 = new DataColumn("F8", Type.GetType("System.Char"));
                dt.Columns.Add(f8);
                DataColumn f9 = new DataColumn("F9", Type.GetType("System.Byte"));
                dt.Columns.Add(f9);
                DataColumn f9a = new DataColumn("F9A", Type.GetType("System.SByte"));
                dt.Columns.Add(f9a);
                DataColumn f10 = new DataColumn("F10", Type.GetType("System.Boolean"));
                dt.Columns.Add(f10);
                DataColumn f11 = new DataColumn("F11", Type.GetType("System.Object"));
                dt.Columns.Add(f11);
                DataColumn f12 = new DataColumn("F12", Type.GetType("System.DateTime"));
                dt.Columns.Add(f12);
                DataColumn f13 = new DataColumn("F13", Type.GetType("System.Guid"));
                dt.Columns.Add(f13);

                createScript = string.Empty;
                bool result = db.CreateTable(dt, out createScript);
            }
            catch (System.Exception ex)
            {
                _msg.Length = 0;
                _msg.Append(AppGlobals.AppMessages.FormatErrorMessage(ex));
                Program._messageLog.WriteLine(_msg.ToString());
                AppMessages.DisplayErrorMessage(_msg.ToString(), _saveErrorMessagesToAppLog);
            }
            finally
            {
                _msg.Length = 0;
                _msg.Append("SQL Script: \r\n");
                _msg.Append(createScript);
                Program._messageLog.WriteLine(_msg.ToString());
                _msg.Length = 0;
                _msg.Append("...CreateDatabaseTest Finished.");
                Program._messageLog.WriteLine(_msg.ToString());
            }
        }