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...
user1743737
  • 45
  • 1
  • 3
  • 5
  • You could get an idea based on this [post](http://stackoverflow.com/questions/2049109/how-to-import-sql-into-sqlite3) – bonCodigo Nov 12 '12 at 23:48

3 Answers3

0

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

CL.
  • 173,858
  • 17
  • 217
  • 259
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");  
Nikson Kanti Paul
  • 3,394
  • 1
  • 35
  • 51
-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");

        }
    }
}