一个将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

时间: 2024-10-01 08:02:47

一个将Access数据库转成Mssql数据库的简单工具的相关文章

Sql server2008的数据库转换成SQL2005数据库

最近在帮一个Q友改网站的时候,我本地备份的数据库是SQL08的,而他的.NET服务器上装的是SQL Server05的,所以要把我备份的数据库转换为05的.过程如下:首先进入SQL Server Management Studio,登录服务器然后 1 如果你要转换的数据库在"对象资源管理器"中,那么右键点击将要转换的数据库(如图中的dy14数据库) 2 如果你要转换的数据库不在"对象资源管理器中",那么如下图操作: 3 完成后,在弹出的"脚本向导"

spring配置ibatis oracle数据库改成mysql数据库遇到数据库方言问题

问题描述 spring配置ibatisoracle数据库改成mysql数据库遇到数据库方言问题<!--spring配置ibatis--><beanid="sqlMapClient"class="org.framework.ibatis.spring.SqlMapClientFactoryBean"><!--Mysql,Sqlserver通用ibatis配置文件-->propertyname="configLocation

将Access数据库转换成Mssql案例方法

http://www.aliyun.com/zixun/aggregation/11208.html">Microsoft Office Access(前名 Microsoft Access)是由微软发布的关联式数据库管理系统.它结合了 Microsoft Jet Database Engine 和 图形用户界面两项特点,是 Microsoft Office的成员之一. Access能够存取 Access/Jet.Microsoft SQL Server.Oracle(甲骨文软件公司),或

MSSQL数据库备份的方法和注意事项

中介交易 http://www.aliyun.com/zixun/aggregation/6858.html">SEO诊断 淘宝客 云主机 技术大厅 MSSQL数据库正被越来越多的站长使用 现在随着.NET架构网站的流行,越来越多的站长选择了.NET+MSSQL的程序建站,和以往大多个人站长选择开源PHP+MYSQL数据库不同,MSSQL数据库在图形界面方面做的更好,但是在使用起来差别非常大,以往操作MYSQL数据库的经验完全不能移植到MSSQL数据库上,所以很多站长感觉使用起来很头痛,一

MSSQL数据库备份最常见的方法

备份数据库是我们做网站必须要掌握的一个基础性知识,只有经常做好网站http://www.aliyun.com/zixun/aggregation/14344.html">数据备份才能防范于未然,备份数据库大家都会根据以往的经验选择自己熟悉的方法,常见的有远程命令备份.程序自带备份功能备份.进入数据库直接操作,MSSQL数据库也是如此,这几种方法都存在,但并不是都那么好用,所以和建站代码网(http://www.yuanmaw.net )一起来看看MSSQL数据库备份的方法吧. 1.程序自带

DBImport v3.0 中文版发布:支持各大数据库数据互导(IT人员必备工具)

事隔一年,做为进军海外的CYQ.DBImport 数据库工具,终于发布新版本:DBImport V3.0. 为了广大国内同胞用的舒心,本次发布中文版本(V1.V2版本为英文版),同时去掉了个人商标CYQ前缀. 另外,本版本内置未面世版CYQ.Data V5内核.   DBImport 适合人群:IT人士.站长.及和数据库打交首的相关相关人员. 只要你是开发者,难免会与数据库打交道,而且,随着项目的不同,接触的数据库也会不同, 不管是你项目需要支持多种数据库切换.还是项目需要进行平台的移值,或者是

T-sql语句修改SQL Server数据库逻辑名、数据库名、物理名的方法_MsSql

本文实例讲述了T-sql语句修改SQL Server数据库逻辑名.数据库名.物理名的方法.分享给大家供大家参考,具体如下: 更改MSSQL数据库物理文件名Sql语句的写法 注意:要在活动监视器里面确保没有进程连接你要改名的数据库!!!!!!!!!!!!!!!!!!!! Sql语句如下 USE master --改逻辑名 ALTER DATABASE YQBlog MODIFY FILE(NAME='YQBlogAA',NEWNAME='YQBlog') -- GO ALTER DATABASE

虚拟主机ACCESS转换成MSSQL完全攻略(图文教程)_数据库其它

许多用ASP开发的系统也可以支持MS SQL数据库.但有不少网友不知道如何将原来的ACCESS数据库转换成MS SQL数据库.根据笔者转换MAXZ的电影系统的经历,特此转换方法公布出来,让大家共享. 一.基本前提1.您使用的虚拟主机支持MS SQL.请必须先从虚拟主机提供商处了解到您所购买的虚拟主机是否支持MS SQL.主机提供商一般都会在您购买了虚拟主机后告诉您诸如下述的MS SQL信息:数据库的主机IP: 218.5.XX.XXX数据库名称: XXXXXX数据库的用户名: XXXXXX数据库

vs2010开发的窗体程序 数据库使用access如何打包成安装文件

问题描述 vs2010开发的窗体程序数据库使用access如何打包成安装文件,我尝试了多次但是都是不成功,求大神文档 解决方案 解决方案二: 解决方案三:引用1楼guwei4037的回复:access数据库直接加在到里面就可以了是吗?解决方案四:access不是直接一个文件就行了嘛