1

with my code i can create a sqlite data base file.

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();
    }

so my question : i want create a database with read data from external sql file like c://mysql/ramin.sql what change i must do in my code?!

and data format on ramin.sql file is :

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...
1
  • You could get an idea based on this post Commented Nov 12, 2012 at 23:48

3 Answers 3

0

Open the .sql file as text file, read every line, and plug that into an SQLiteCommand.

Sign up to request clarification or add additional context in comments.

Comments

0

you can set all sql text in CommmandText using System.IO.File.ReadAllText

SQLiteCommand command = new SQLiteCommand(connection);  
command.CommandText = System.IO.File.ReadAllText(@"file.sql");  

Comments

-2
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");

        }
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.