简体   繁体   English

使用SQLite使用本地MySql(Workbench)数据库部署C#应用程序

[英]Deploying C# Application with Local MySql (Workbench) Database using SQLite

I'm currently preparing ac# application I've made using MySql workbench, Visual Studio, XAMPP and the MySQL connector. 我目前正在准备使用MySql工作台,Visual Studio,XAMPP和MySQL连接器制作的ac#应用程序。 I want to enable the use of this application on other computers without the use of so many programs/installations. 我想在其他计算机上启用此应用程序,而无需使用太多程序/安装。

After doing some research (and checking other questions here), I've concluded that SQLite is the best option. 经过研究(并在此处检查了其他问题),我得出结论说SQLite是最佳选择。 However, I'm not sure how to implement this appropriately. 但是,我不确定如何适当地实现这一点。 I've looked into several conversion plugins/scripts (.sql file to sqlite database) however I'm not able to get them to function properly. 我研究了几个转换插件/脚本(.sql文件到sqlite数据库),但是无法使它们正常运行。

If anyone has any links they can point me to or any ideas on how I can implement SQLite please do let me know! 如果任何人有任何指向他们的链接,或者对如何实现SQLite有任何想法,请告诉我!

Thanks in advance! 提前致谢!

UPDATE: Ok so I spent a day trying to make my application compatible with SQLite, however I was unsuccessful. 更新:好的,所以我花了一天的时间尝试使我的应用程序与SQLite兼容,但是我没有成功。 What would be the best way to deploy the application with the mysql database with the minimum amount of work by the end user? 用最终用户最少的工作量用mysql数据库部署应用程序的最佳方法是什么?

Thanks to everyone for their contribution! 感谢大家的贡献!

I've not had much luck with conversion plugin/scripts either. 我对转换插件/脚本也不太满意。 I ended up writing my own conversions for the small number of tables I needed. 我最终为需要的少量表编写了自己的转换。 Here is a rather simple example of one using one of my tables that you could easily modify for your own purposes: 这是一个使用我的表之一的简单示例,您可以根据自己的目的轻松对其进行修改:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SQLite;
using MySql.Data.MySqlClient;


namespace SampleConversion
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
    private void create_SQLite_database()
    {
        string connectionString = "Data Source=" + Application.StartupPath + "\\pthData.sqlite;Version=3;";

        try
        {
            SQLiteConnection.CreateFile("pthData.sqlite");
        }
        catch (SQLiteException ex)
        {
            MessageBox.Show("Unable to create the SQLite database\n" + ex.Message + "\nConnection string: " + connectionString, "Create SQLite file/database error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1, MessageBoxOptions.ServiceNotification);
        }
        catch (Exception ex)
        {
            MessageBox.Show("Unable to create the SQLitedatabase\n" + ex.Message + "\nConnection string: " + connectionString, "Create SQLite file/database error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1, MessageBoxOptions.ServiceNotification);
        }
        // file created, now create tables
        SQLiteConnection cn = new SQLiteConnection(connectionString);
        if (cn.State == ConnectionState.Closed)
            cn.Open();

        SQLiteCommand cmd;
        string commandString = "Create table if not exists USTimeZones\n";

        // create time zones file
        commandString += "(state nvarchar(30), city nvarchar(100), county nvarchar(50), timezone nvarchar(10), ";
        commandString += "timetype int, latitude nvarchar(10), longitude nvarchar(10),  ";
        commandString += "PRIMARY KEY(state, city, county, timezone, timetype))";
        cmd = new SQLiteCommand(commandString, cn);
        try
        {
            cmd.ExecuteNonQuery();
        }
        catch (SQLiteException sqlexception)
        {
            MessageBox.Show(sqlexception.Message + "\n Command string: " + commandString, "Error creating USTimeZones", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1, MessageBoxOptions.ServiceNotification);
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message + "\n Command string: " + commandString, "Error creating USTimeZoness", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1, MessageBoxOptions.ServiceNotification);
        }

    }


    private void btnMySql_Click(object sender, EventArgs e)
    {
        string cmd = "";
        int count = 0;

        create_SQLite_database(); // Create the SQLite database file and a table within it

        string MySQLconnectionString = "Server=MyServerAddress;Database=PTHData;uid=username;pwd=passwordhere;database=USTimeZones";
        string SQLiteconnectionString = "Data Source=" + Application.StartupPath + "\\pthData.sqlite;Version=3;";

        // open the input and output database
        SQLiteConnection SQLiteconnection = new SQLiteConnection(SQLiteconnectionString);
        try
        {
            SQLiteconnection.Open();
        }
        catch (SQLiteException ex)
        {
            string errorMessages = "A SQLite exception occurred on open.\n" + ex.Message;
            MessageBox.Show(errorMessages, "Convert");
            return;
        }
        MySqlConnection MySQLconnection = new MySqlConnection(MySQLconnectionString);
        try
        {
            MySQLconnection.Open();
        }
        catch (SqlException ex)
        {
            string errorMessages = "A MySQL exception occurred on open.\n" + ex.Message;
            MessageBox.Show(errorMessages, "Convert");
            return;
        }

        //Databases are not open, time to convert
        MySqlCommand cmdread = new MySqlCommand();
        cmdread.Connection = MySQLconnection;
        cmdread.CommandText = "Select * from USTimeZones";
        MySqlDataReader drread = null;

        SQLiteCommand cmdwrite = new SQLiteCommand();
        cmdwrite.Connection = SQLiteconnection;

        try
        {
            drread = cmdread.ExecuteReader();
            while (drread.Read())
            {
                drread["timezone"].ToString();
                cmd = "Insert into USTimeZones values ('" + drread["state"].ToString() + "','" +
                    drread["city"].ToString() + "','" + drread["county"].ToString() + "','" +
                    drread["timezone"].ToString() + "','" + drread["timetype"].ToString() + "','" +
                    drread["latitude"].ToString() + "','" + drread["longitude"].ToString() + "')";
                cmdwrite.CommandText = cmd;
                try
                {
                    cmdwrite.ExecuteNonQuery();
                    count++;
                }
                catch (SQLiteException ex)
                {
                    string errorMessages = "An SQL exception occurred on writing the SQLite record.\n" + ex.Message;
                    MessageBox.Show(errorMessages, "Convert");
                    SQLiteconnection.Close();
                    MySQLconnection.Close();
                    return;
                }

            }
        }
        catch (MySqlException ex)
        {
            string errorMessages = "A MySQL exception occurred reading records.\n" + ex.Message;
            MessageBox.Show(errorMessages, "Convert");
        }
        catch (Exception ex)
        {
            string errorMessages = "A General exception occurred reading records.\n" + ex.Message;
            MessageBox.Show(errorMessages, "Convert");
        }

        MessageBox.Show("Records written: " + count.ToString(), "Conversion complete");
        drread.Close();
        MySQLconnection.Close();
        SQLiteconnection.Close();

    }
}

} }

Also there is a "SQL Server Compact/SQLite Toolbox" you can load from the Visual Studio NuGet Package Manager that I have found to be quite useful as well. 另外还有一个“ SQL Server Compact / SQLite工具箱”,您可以从Visual Studio NuGet程序包管理器中加载它,我发现它也非常有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM