【问题标题】:create sqlite database with c# by using external ramin.sql file使用外部 ramin.sql 文件使用 c# 创建 sqlite 数据库
【发布时间】:2012-11-12 23:43:08
【问题描述】:

使用我的代码,我可以创建一个 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...

【问题讨论】:

  • 你可以根据这个post得到一个想法

标签: c# sql sqlite


【解决方案1】:

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

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

      【解决方案3】:
      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");
      
              }
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-07-26
        • 1970-01-01
        • 2018-09-09
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多