2012-11-12 2 views
1

내 코드로 sqlite 데이터베이스 파일을 만들 수 있습니다. 외부 ramin.sql 파일을 사용하여 C#으로 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(); 
    } 

그래서 내 질문 : 전 C와 같은 외부 SQL 파일의 읽기 데이터와 데이터베이스를 생성하려면 : //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... 
+0

당신 수이 [포스트]를 기반으로 아이디어를 (얻을 http://stackoverflow.com/questions/2049109/how-to -import-sql-into-sqlite3) – bonCodigo

답변

0

열기 텍스트 파일로 .SQL 파일의 모든 라인을 읽기 및 SQLiteCommand에 그 연결합니다.

0

사용 CommmandText에서 모든 SQL 텍스트를 설정할 수 있습니다 System.IO.File.ReadAllText

SQLiteCommand command = new SQLiteCommand(connection); 
command.CommandText = System.IO.File.ReadAllText(@"file.sql"); 
0
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"); 

     } 
    } 
} 
관련 문제