1

使用我的代码,我可以创建一个 sqlite 数据库文件。

public static void sqlite(string sqlite_file)
    {
        SQLiteConnection.CreateFile(sqlite_file);
        string str = "CREATE TABLE IF NOT EXISTS `aliases` (`key` varchar(255) NOT NULL,`value` varchar(255) NOT NULL);.... "          
        SQLiteConnection connection = new SQLiteConnection {
            ConnectionString = "Data Source=" + sqlite_file
        };
        connection.Open();
        SQLiteCommand command = new SQLiteCommand(connection) {
            CommandText = str
        };
        command.ExecuteNonQuery();
        command.Dispose();
        connection.Close();
        connection.Dispose();
    }

所以我的问题:我想创建一个从外部 sql 文件(如 c://mysql/ramin.sql)读取数据的数据库,我必须在我的代码中做些什么更改?!

ramin.sql 文件上的数据格式为:

CREATE TABLE IF NOT EXISTS `aliases` (`key` varchar(255) NOT NULL,`value` varchar(255) NOT NULL);
CREATE TABLE IF NOT EXISTS `badnames` (`badname` varchar(255) NOT NULL);
CREATE TABLE IF NOT EXISTS `badwords` (`badword` varchar(255) NOT NULL);
and evey command in every line...
4

3 回答 3

0

将 .sql 文件作为文本文件打开,读取每一行,然后将其插入SQLiteCommand.

于 2012-11-13T07:58:38.853 回答
0

CommmandText您可以在使用中设置所有 sql 文本System.IO.File.ReadAllText

SQLiteCommand command = new SQLiteCommand(connection);  
command.CommandText = System.IO.File.ReadAllText(@"file.sql");  
于 2012-11-13T08:11:25.230 回答
-1
privat string _dataSource = @"H:\Ik.db";
private SQLiteConnection _connection;
private SQLiteCommand _command;

private void connectToSQLite()
{
    using (SQLiteConnection _connection = new SQLiteConnection())
    {
        if (File.Exists(@"H:\Ik.db"))
        {
            _connection.ConnectionString = $"Data Source={_dataSource};Version=3";
            _connection.Open();
            using (SQLiteCommand _command = new SQLiteCommand())
            {
                _command.Connection = _connection;
                   _command.CommandText = "INSERT INTO Customer(id, Lastname,firstname,Code,City) " +
                    $"VALUES(NULL, '{txt_Lastname.Text}', '{txt_name.Text}', '{ txt_code.Text}', '{ txt_city.Text}')";
                try
                {
                    _command.ExecuteNonQuery();
                    MessageBox.Show($"You Connected to local Data Base under {_dataSource} Sucssefuly");
                }
                catch (Exception)
                {

                    throw;
                }
            }
        }
        else
        {
            SQLiteConnection.CreateFile(@"H:\Ik.db");

        }
    }
}
于 2017-10-11T09:28:40.787 回答