コード例 #1
0
ファイル: frmUpgrade.cs プロジェクト: bojbaj/kDBMigration
        private void cbAuthType_SelectedIndexChanged(object sender, EventArgs e)
        {
            tAuthType item = (tAuthType)cbAuthType.SelectedItem;

            if (item.Id == 1)
            {
                txtUserId.Enabled   = false;
                txtPassword.Enabled = false;
            }
            else
            {
                txtUserId.Enabled   = true;
                txtPassword.Enabled = true;
            }
        }
コード例 #2
0
ファイル: frmUpgrade.cs プロジェクト: bojbaj/kDBMigration
        private void btnUpgrade_Click(object sender, EventArgs e)
        {
            tAuthType item     = (tAuthType)cbAuthType.SelectedItem;
            string    server   = cbServer.Text.Trim();
            string    userId   = txtUserId.Text.Trim();
            string    password = txtPassword.Text.Trim();
            string    dbName   = cbDatabase.Text.Trim();
            byte      authType = item.Id;

            if (string.IsNullOrEmpty(migrationFilePath))
            {
                MessageBox.Show("please select migration file");
                return;
            }
            if (string.IsNullOrEmpty(server))
            {
                MessageBox.Show("enter server name");
                return;
            }
            if (string.IsNullOrEmpty(dbName))
            {
                MessageBox.Show("enter database name");
                return;
            }
            try
            {
                OleDbConnection connection = dbFactoryStatic.connectionMaker(migrationFilePath);
                dbFactory       db         = new dbFactory(connection);

                SqlConnection targetConnection = dbFactoryStatic.connectionMaker(server, authType, userId, password, dbName);
                dbFactory     dbTarget         = new dbFactory(targetConnection);
                dbTarget.initialDb(projectCode);
                tSQLSVNVersion version = dbTarget.Get <tSQLSVNVersion>(projectCode);

                List <tScript> scripts = db.GetAll <tScript>().Where(x => x.tProjectCode == projectCode && x.scriptId > version.VerNumber).OrderBy(x => x.scriptId).ToList();

                JsonConfig configFile = new JsonConfig(connectionFilePath);
                configFile.save(server, dbName, authType, userId, password);

                if (scripts.Count == 0)
                {
                    MessageBox.Show(string.Format("Your database is latest version. version : {0}", version.VerNumber));
                    return;
                }

                dbTarget.connOpen();
                int scriptId = version.VerNumber;
                try
                {
                    progressBarUpgrade.Visible = true;
                    progressBarUpgrade.Minimum = scripts.Min(x => x.scriptId);
                    progressBarUpgrade.Maximum = scripts.Max(x => x.scriptId);
                    progressBarUpgrade.Step    = 1;
                    foreach (tScript script in scripts)
                    {
                        scriptId = script.scriptId;
                        progressBarUpgrade.Value = scriptId;
                        dbTarget.beginTrans();
                        foreach (string command in script.sql.Split(new string[] { "GO\r\n", "\r\nGO" }, StringSplitOptions.RemoveEmptyEntries))
                        {
                            dbTarget.Execute(command);
                        }
                        version.VerNumber = scriptId;
                        dbTarget.Update <tSQLSVNVersion>(version);
                        dbTarget.commitTrans();
                    }
                    MessageBox.Show(string.Format("Successfully upgraded to version : {0}", scriptId));
                }
                catch (Exception ex)
                {
                    dbTarget.rollbackTrans();
                    string errorMessage = string.Empty;
                    errorMessage += string.Format("Error\nScript Id:{0}\n\n{1}", scriptId, ex.Message);
                    MessageBox.Show(errorMessage);
                }
                dbTarget.connClose();
                progressBarUpgrade.Visible = false;
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error in upgrade: \n" + ex.Message);
            }
        }