1. 程式人生 > >一個將Access資料庫轉成Mssql資料庫的簡單工具

一個將Access資料庫轉成Mssql資料庫的簡單工具

frmMain.cs

public partial class frmMain : Form
    {
        private Thread TransferWorkerThread = null;

        public frmMain()
        {
            InitializeComponent();
        }

        private void LoadSettings()
        {
            StreamReader setting_file = null;
            char[] d = new char[] { '='
}; try { setting_file = new StreamReader("setting.ini"); object[] text_type; Program.GeneralAppSetting.Collation = setting_file.ReadLine().Split(d)[1]; text_type = Access2Mssql.AccessToMssql.ParseTextType(setting_file.ReadLine
().Split(d)[1]); Program.GeneralAppSetting.TextType = (string)text_type[0]; Program.GeneralAppSetting.Size = (int)text_type[1]; Program.GeneralAppSetting.AutoFixTextPrimaryAttempt = Convert.ToBoolean(setting_file.ReadLine().Split(d)[1]); Program.GeneralAppSetting
.NewDatabaseDoesNotExist = Convert.ToBoolean(setting_file.ReadLine().Split(d)[1]); Program.GeneralAppSetting.DatabaseStoragePath = setting_file.ReadLine().Split(d)[1]; Program.GeneralAppSetting.TryAutoDetectStoragePath = Convert.ToBoolean(setting_file.ReadLine().Split(d)[1]); Program.GeneralAppSetting.DatabaseLevel = Convert.ToUInt16(setting_file.ReadLine().Split(d)[1]); Program.GeneralAppSetting.MdfMaxSize = Convert.ToInt32(setting_file.ReadLine().Split(d)[1]); Program.GeneralAppSetting.MdfSizeType = (FileSizeType)Convert.ToInt32(setting_file.ReadLine().Split(d)[1]); Program.GeneralAppSetting.LdfMaxSize = Convert.ToInt32(setting_file.ReadLine().Split(d)[1]); Program.GeneralAppSetting.LdfSizeType = (FileSizeType)Convert.ToInt32(setting_file.ReadLine().Split(d)[1]); setting_file.Close(); } catch { Program.GeneralAppSetting.Collation = "Turkish_CI_AS"; Program.GeneralAppSetting.TextType = "varchar"; Program.GeneralAppSetting.Size = 50; Program.GeneralAppSetting.NewDatabaseDoesNotExist = false; Program.GeneralAppSetting.AutoFixTextPrimaryAttempt = false; Program.GeneralAppSetting.DatabaseStoragePath = string.Empty; Program.GeneralAppSetting.TryAutoDetectStoragePath = true; Program.GeneralAppSetting.DatabaseLevel = 9; Program.GeneralAppSetting.MdfMaxSize = 0; Program.GeneralAppSetting.MdfSizeType = FileSizeType.GB; Program.GeneralAppSetting.LdfMaxSize = 1024; Program.GeneralAppSetting.LdfSizeType = FileSizeType.GB; } } private void frmMain_Load(object sender, EventArgs e) { LoadSettings(); cbAuthentication.SelectedIndex = 0; Program.Converter = new AccessToMssql(); } private void SetStatus(string status) { lblStatus.Text = status; } private void ConnectionError(ErrorType type, string extend_error_msg) { string ErrorMessage = string.Empty; switch (type) { case ErrorType.COULD_NOT_CONNECT_ACCESS: ErrorMessage = "無法連線到access資料庫!"; break; case ErrorType.COULD_NOT_CONNECT_MSSQL: ErrorMessage = "無法連線到mssql server資料庫!"; break; } ErrorMessage += "\n\n擴充套件訊息:\n"; ErrorMessage += extend_error_msg; MessageBox.Show(ErrorMessage, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } private void Completed(ConvertSummary summary) { frmOperationSummary frmOS = new frmOperationSummary(); Program.Converter.Disconnect(); Program.Converter.Dispose(); frmOS.SetSummary(ref summary); frmOS.ShowDialog(); this.BeginInvoke((MethodInvoker)delegate() { this.btnStart.Text = "開始轉換"; }); } private void OperationFailed(ref string msg) { } private void SetDatabaseSettingStructure(out DatabaseSettings Setting) { Setting.AccessDbFile = txtAccessSource.Text; Setting.Database = txtDatabase.Text; Setting.IsSqlAuthencation = txtUsername.Enabled; Setting.TrustedConnection = chkTrustedConn.Checked; Setting.Username = txtUsername.Text; Setting.Password = txtPassword.Text; Setting.Server = txtServer.Text; } private void StartOperation() { DatabaseSettings setting = new DatabaseSettings(); SetDatabaseSettingStructure(out setting); if (!Program.Converter.EventSet) { Program.Converter.OnProgressStatus += new OnProgressEventHandler(SetStatus); Program.Converter.OnOperationFailed += new OperationFailedExceptionEventHandler(OperationFailed); Program.Converter.OnConnectionError += new ConnectionExceptionEventHandler(ConnectionError); Program.Converter.OnComplete += new OnCompleteEventHandler(Completed); } if (!Program.Converter.Connect(setting)) { Program.Converter.Dispose(); this.BeginInvoke((MethodInvoker)delegate() { this.btnStart.Text = "開始轉換"; }); } else Program.Converter.StartTransfer(); } private void ActivateOperationWorkerThread() { TransferWorkerThread = null; TransferWorkerThread = new Thread(new ThreadStart(StartOperation)); btnStart.Text = "Abort"; TransferWorkerThread.Start(); } private void cbAuthentication_SelectedIndexChanged(object sender, EventArgs e) { label4.Enabled = label5.Enabled = txtUsername.Enabled = txtPassword.Enabled = chkMasking.Enabled = (cbAuthentication.SelectedIndex == 1); } private void btnBrowse_Click(object sender, EventArgs e) { OpenFileDialog access = new OpenFileDialog(); access.Filter = "Access Database File|*.mdb"; access.InitialDirectory = "C:\\"; access.ShowDialog(); txtAccessSource.Text = access.FileName; } private void chkMasking_CheckedChanged(object sender, EventArgs e) { txtPassword.PasswordChar = (chkMasking.Checked) ? '\0' : '*'; } private void btnStart_Click(object sender, EventArgs e) { if (TransferWorkerThread == null || TransferWorkerThread.ThreadState == ThreadState.Stopped) { ActivateOperationWorkerThread(); return; } switch (TransferWorkerThread.ThreadState) { case ThreadState.Unstarted: case ThreadState.Aborted: { ActivateOperationWorkerThread(); } break; case ThreadState.Running: { frmChoise choise = new frmChoise(); choise.ShowDialog(); switch (choise.m_Stat) { case 0: { lblStatus.Text = "中止...請稍候..."; Program.Converter.AbortOperation(); lblStatus.Text = "使用者中斷操作"; btnStart.Text = "開始轉換"; } break; case 1: { TransferWorkerThread.Suspend(); btnStart.Text = "恢復"; lblStatus.Text = "暫停"; } break; } } break; case ThreadState.Suspended: { TransferWorkerThread.Resume(); btnStart.Text = "Abort"; } break; } } private void btnSettings_Click(object sender, EventArgs e) { frmSettings dlgSettings = new frmSettings(); dlgSettings.Show(); } private void btnAbout_Click(object sender, EventArgs e) { frmAbout frmAboutProgram = new frmAbout(); frmAboutProgram.ShowDialog(); } private void backupRestoreToolStripMenuItem_Click(object sender, EventArgs e) { } }

frmSettings.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace Access2Mssql
{
    public partial class frmSettings : Form
    {
        private string[] DefaultCollations = new string[]
        {

            "Chinese_PRC_BIN",
         "Chinese_PRC_BIN2",
         "Chinese_PRC_CI_AI",
         "Chinese_PRC_CI_AI_WS",
         "Chinese_PRC_CI_AI_KS",
         "Chinese_PRC_CI_AI_KS_WS",
         "Chinese_PRC_CI_AS", //Chinese_PRC_指標對大陸簡體字UNICODE的排序規則,_CI(CS) :是否區分大小寫,CI不區分,CS區分。_AI(AS) :是否區分重音,AI不區分,AS區分。
         "Chinese_PRC_CI_AS_WS",
         "Chinese_PRC_CI_AS_KS",
         "Chinese_PRC_CI_AS_KS_WS",
         "Chinese_PRC_CS_AI",
         "Chinese_PRC_CS_AI_WS",
         "Chinese_PRC_CS_AI_KS",
         "Chinese_PRC_CS_AI_KS_WS",
         "Chinese_PRC_CS_AS",
         "Chinese_PRC_CS_AS_WS",
         "Chinese_PRC_CS_AS_KS",
         "Chinese_PRC_CS_AS_KS_WS",
         "Chinese_Simplified_Stroke_Order_100_CI_AI_KS_WS",
         "Chinese_Simplified_Stroke_Order_100_CI_AS",
         "Chinese_Simplified_Stroke_Order_100_CI_AS_WS",
         "Chinese_Simplified_Stroke_Order_100_CI_AS_KS",
         "Chinese_Simplified_Stroke_Order_100_CI_AS_KS_WS",
         "Chinese_Simplified_Stroke_Order_100_CS_AI",
         "Chinese_Simplified_Stroke_Order_100_CS_AI_WS",
         "Chinese_Simplified_Stroke_Order_100_CS_AI_KS",
         "Chinese_Simplified_Stroke_Order_100_CS_AI_KS_WS",
         "Chinese_Simplified_Stroke_Order_100_CS_AS ",
         "Chinese_Simplified_Stroke_Order_100_CS_AS_WS ",
         "Chinese_Simplified_Stroke_Order_100_CS_AS_KS ",
         "Chinese_Simplified_Stroke_Order_100_CS_AS_KS_WS ",
            "Turkish_BIN",
            "Turkish_BIN2",
            "Turkish_CI_AI",
            "Turkish_CI_AI_WS",
            "Turkish_CI_AI_KS",
            "Turkish_CI_AI_KS_WS",
            "Turkish_CI_AS",
            "Turkish_CI_AS_WS",
            "Turkish_CI_AS_KS",
            "Turkish_CI_AS_KS_WS",
            "Turkish_CS_AI",
            "Turkish_CS_AI_WS",
            "Turkish_CS_AI_KS",
            "Turkish_CS_AI_KS_WS",
            "Turkish_CS_AS",
            "Turkish_CS_AS_WS",
            "Turkish_CS_AS_KS",
            "Turkish_CS_AS_KS_WS"
        };

        public frmSettings()
        {
            InitializeComponent();
        }

        private void PrepateExtendedSettings()
        {

            pnlDbSettings.Enabled = chkExistDb.Checked;
            pnlDbSettings.BackColor = (chkExistDb.Checked) ? Color.DarkSeaGreen : Color.DarkRed;
        }

        private void cbTextType_SelectedIndexChanged(object sender, EventArgs e)
        {
            txtVarSize.Enabled = !(cbTextType.SelectedIndex > 3);
        }

        private void txtVarSize_TextChanged(object sender, EventArgs e)
        {
            int size = 0;
            try
            {
                size = Convert.ToInt32(txtVarSize.Text);
            }
            catch
            {
                txtVarSize.Text = "0";
                size = 0;
            }

            if (size < 0 || size > 65536)
            {
                txtVarSize.Text = "0";
                size = 0;
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            StreamWriter setting_file = new StreamWriter("setting.ini");

            try
            {
                setting_file.WriteLine("collation={0}", cbCollation.Text);
                setting_file.WriteLine("text_type_def={0}", cbTextType.Text + "(" + txtVarSize.Text + ")");
                setting_file.WriteLine("auto_fix={0}", chkFix.Checked.ToString().ToLower());
                setting_file.WriteLine("new_database={0}", chkExistDb.Checked.ToString().ToLower());
                setting_file.WriteLine("sql_server_storage_path={0}", txtStoragePath.Text);
                setting_file.WriteLine("auto_detect_storage_path={0}", chkAutoDetect.Checked.ToString().ToLower());
                setting_file.WriteLine("compatible_level={0}", mnCompatibleLevel.Value);
                setting_file.WriteLine("mdf_max_size={0}", txtMdfMax.Text);
                setting_file.WriteLine("mdf_type={0}", cbMdfType.SelectedIndex);
                setting_file.WriteLine("ldf_max_size={0}", txtLdfMax.Text);
                setting_file.WriteLine("ldf_type={0}", cbLdfType.SelectedIndex);


                Program.GeneralAppSetting.Collation = cbCollation.Text;
                Program.GeneralAppSetting.TextType = cbTextType.Text;
                Program.GeneralAppSetting.Size = Convert.ToInt32(txtVarSize.Text);
                Program.GeneralAppSetting.NewDatabaseDoesNotExist = chkExistDb.Checked;
                Program.GeneralAppSetting.AutoFixTextPrimaryAttempt = chkFix.Checked;
                Program.GeneralAppSetting.DatabaseStoragePath = txtStoragePath.Text;
                Program.GeneralAppSetting.DatabaseLevel = (ushort)mnCompatibleLevel.Value;
                Program.GeneralAppSetting.MdfMaxSize = Convert.ToInt32(txtMdfMax.Text);
                Program.GeneralAppSetting.MdfSizeType = (FileSizeType)cbLdfType.SelectedIndex;
                Program.GeneralAppSetting.LdfMaxSize = Convert.ToInt32(txtLdfMax.Text);
                Program.GeneralAppSetting.LdfSizeType = (FileSizeType)cbLdfType.SelectedIndex;

            }
            catch
            {
                MessageBox.Show("Settings file corrupted!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);

            }

            setting_file.Close();
            this.Close();
        }

        private void frmSettings_Load(object sender, EventArgs e)
        {
            StreamReader collations;
            try
            {

                collations = new StreamReader(Application.StartupPath + "\\collations.cdb");
                do
                {
                    cbCollation.Items.Add(collations.ReadLine());
                } while (collations.EndOfStream != true);

                cbCollation.SelectedIndex = 0;

                collations.Close();
                collations.Dispose();

            }
            catch
            {
                MessageBox.Show("collations.cdb not found!");
                for (int i = 0; i < DefaultCollations.Length; i++)
                    cbCollation.Items.Add(DefaultCollations[i]);
                cbCollation.SelectedIndex = 0;

            }


            cbCollation.Text = Program.GeneralAppSetting.Collation;
            cbTextType.SelectedIndex = cbTextType.FindString(Program.GeneralAppSetting.TextType);
            txtVarSize.Text = Program.GeneralAppSetting.Size.ToString();
            chkExistDb.Checked = Program.GeneralAppSetting.NewDatabaseDoesNotExist;
            chkFix.Checked = Program.GeneralAppSetting.AutoFixTextPrimaryAttempt;

            txtStoragePath.Text = Program.GeneralAppSetting.DatabaseStoragePath;
            chkAutoDetect.Checked = Program.GeneralAppSetting.TryAutoDetectStoragePath;
            mnCompatibleLevel.Value = Program.GeneralAppSetting.DatabaseLevel;
            txtMdfMax.Text = Program.GeneralAppSetting.MdfMaxSize.ToString();
            cbMdfType.SelectedIndex = (int)Program.GeneralAppSetting.MdfSizeType;
            txtLdfMax.Text = Program.GeneralAppSetting.LdfMaxSize.ToString();
            cbLdfType.SelectedIndex = (int)Program.GeneralAppSetting.LdfSizeType;

            PrepateExtendedSettings();
        }


        private void chkExistDb_CheckedChanged(object sender, EventArgs e)
        {
            PrepateExtendedSettings();
        }

        private void chkFix_CheckedChanged(object sender, EventArgs e)
        {

        }
    }
}

執行結果如圖:

這裡寫圖片描述

這裡寫圖片描述

這裡寫圖片描述

access資料庫內容
這裡寫圖片描述

轉換後MSSQL的內容
這裡寫圖片描述

C# 未在本地計算機上註冊“Microsoft.Jet.OLEDB.4.0”提供程式

win7或win8 64位除錯程式,出現這樣的錯誤提示:未在本地計算機上註冊 Microsoft.Jet.OLEDB.4.0 提供程式

解決方法如下:

在專案–》*屬性—–》生成下面將目標平臺改成X86
這裡寫圖片描述

相關推薦

一個Access資料庫Mssql資料庫簡單工具

frmMain.cs public partial class frmMain : Form { private Thread TransferWorkerThread = null; public frmMain

前臺日期格式資料庫日期格式相對應,後臺java資料庫日期格式

前臺到後臺java時data日期型別的轉化 在實體類中用@DataTimeFormat,這樣設定即使傳過來是空的字串也是可以轉的,要和前面傳過來的格式一致,如 @XmlElement(name="BeginDate") @DateTimeFormat(pattern="yyyy-MM-dd")

指定圖片二進位制陣列,以及二進位制陣列儲存到資料庫中的方法

      在winform中點選選擇顯示圖片功能,我這邊是用的pictureBox控制元件,現在pictureBox控制元件中的雙擊事件中用以下方法,這邊是使用OpenFileDialog類開啟選擇

如何把ACCESSSQL資料庫

一,首先,我說的是在ACCESS2000,SQL2000之間轉換,其他的我也還沒有嘗試過,希望大家多多試驗,肯定是有辦法的; 二,轉換的方法 1,開啟”控制面板“下”管理工具“中的”資料庫源“; 2,按”新增“新增一個新的資料來源,在選擇欄裡選“Driver do microsoft Access (

StringBuffer練習 int[] arr = {34,12,89,68};一個int[]中元素字串 格式 [34,12,89,68]

練習:int[] arr = {34,12,89,68};將一個int[]中元素轉成字串  格式 [34,12,89,68] public static String toString_2(int[] arr) { StringBuffer sb = new StringBuffe

封裝一個ExcelHelper,方便Excel直接Datatable對象

oda pri 版本 elf for n) count emp help 1 public class ExcelHelper 2 { 3 /// <summary> 4 /// Excel轉換成Dat

怎麽string list 有特殊字符分開字符串

utils ted 特殊 step sta bsp api del over https://stackoverflow.com/questions/4021851/join-string-list-elements-with-a-delimiter-in-one-step

數字金額漢字大寫的

負數 big 漢字 all AD AR || git return /** 數字金額大寫轉換(可以處理整數,小數,負數) */ function smalltoBIG(n) { var fraction = [‘角

自定義EL表達式,對象json格式,關鍵代碼

地址 .com rar json common onf jsp頁面 conf 標簽 做javaweb開發的最常用的一個東西el表達式,這個東西是個很好用的東西,但有些時候我們處理復雜的字符串操作,就有些相形見絀了,這個時候就需要用自定義的方法去實現更多簡潔方便的事情。下面自

利用itexthtml頁面pdf(不模糊)

relative long ble wid ems map entity repl oat 1.maven項目進入依賴 <dependency> <groupId>org.xhtmlrenderer</groupId>

fastjsonLong型json精度丟失問題解決

writer cts 精度問題 丟失 ren json mat super 解決 這是jacksonpublic void extendMessageConverters(List<HttpMessageConverter<?>> converter

讀取數據庫時數據XML

lose code 取數據 pen ssr from command col for xml using (SqlConnection conn=new SqlConnection(config)) { conn.Open();

java用Json父類子類

將父類物件用 JSONObject.toJSONString(father)轉成String, 再用JSONObject.parseObject 轉成子類物件 Son son2= JSONObject.parseObject(JSONObject.toJSONString(father),S

cifar10資料圖片

#將cifar10轉成圖片 import numpy as np import matplotlib.image as plimg from PIL import Image import pickle as p def load_CIFAR_batch(filename):

mnist資料圖片

from tensorflow.examples.tutorials.mnist import input_data import scipy.misc import os # 注意儲存的路徑 mnist = input_data.read_data_sets("MNIST_DATA", o

英文列表中文

定義英文列舉 enum Week { Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday } 定義轉換方法 static string GetChineseW

Mysql資料庫切換Oracle資料庫,使用Mybatis+Oracle整合專案,Mysql與Oracle差別總結

最近公司給我一個任務,把現有專案由Mysql切換成Oracle,而且還要求相容Mysql和Oracle資料庫。網上有很多類似的資源,說了這兩個資料庫的差別,但是沒有進行完整的總結。在這個任務完成後,我在這裡總結一下。 1.原有的mysql的insert語句,沒有做空判斷,也沒有指定jdbc型別,

一個浮點數轉換人民幣讀法字串處理(xxxx.xx格式)

主要是加深對陣列的理解在,這個程式原來是《瘋狂java講義》低第103頁上的一個例子,但是例子並不完整,對0的處理和對末位包括小數點後面處理不完整,因此這裡進行了完善。考慮到人民幣通俗叫法,浮點數格式為(XXXX.XX),轉換為字串格式為(x千x百x十x元x角x分),當然還要考慮末位開始位和零的處理

itext7pdf模板pdf檔案

使用adobe Acrobat Pro DC製作pdf模板 這裡就略了,大家可以上網搜下,有很多方法 擼程式碼 在itext7第一節的基礎上在pom檔案新增junit依賴方便測試 <dependency> <groupId>juni

bigemap如何衛星影像80座標系?

                         七引數計算工具教程      注意:投影轉換成80座標系需要下