Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. How to copy DataTable contents into SQLite table?

How to copy DataTable contents into SQLite table?

Scheduled Pinned Locked Moved C#
databasehelpsqlitetoolstutorial
8 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Alex Dunlop
    wrote on last edited by
    #1

    Hi, My DataTable has 40 columns. I have created a Database and a table with 40 columns for SQLite programmatically. I want to copy all my contents in DataTable into SQLite table. I used this class code:

    public class SQLiteFunction
    {
    //Path of database
    public string databaseName { get; set; }
    public string dataTableName { get; set; }

        private DataTable dt = new DataTable();
        public SQLiteFunction()
        {
            dt.TableName = dataTableName;
        }
    
        public void Create\_db()
        {
            if (!File.Exists(databaseName))
            {
                using (var sqlite = new SQLiteConnection(@"Data Source=" + databaseName))
                {
                    sqlite.Open();
                    string script = File.ReadAllText(@"CreateTable.sql");
                    SQLiteCommand command = new SQLiteCommand(script, sqlite);
                    command.ExecuteNonQuery();
                    sqlite.Close();
                }
            }
        }
        public void InsertData()
        {
            SQLiteConnection con = new SQLiteConnection(@"Data Source=" + databaseName);
            con.Open();
            SQLiteDataAdapter dAdapter = new SQLiteDataAdapter("SELECT FROM MyTable", con);
            SQLiteCommandBuilder cmdBuilder = new SQLiteCommandBuilder(dAdapter);
            dAdapter.Fill(dt);
        }
    }
    

    When I use InserData() method, there is no runtime error but my SQLite table is still empty. Please help me.

    OriginalGriffO 1 Reply Last reply
    0
    • A Alex Dunlop

      Hi, My DataTable has 40 columns. I have created a Database and a table with 40 columns for SQLite programmatically. I want to copy all my contents in DataTable into SQLite table. I used this class code:

      public class SQLiteFunction
      {
      //Path of database
      public string databaseName { get; set; }
      public string dataTableName { get; set; }

          private DataTable dt = new DataTable();
          public SQLiteFunction()
          {
              dt.TableName = dataTableName;
          }
      
          public void Create\_db()
          {
              if (!File.Exists(databaseName))
              {
                  using (var sqlite = new SQLiteConnection(@"Data Source=" + databaseName))
                  {
                      sqlite.Open();
                      string script = File.ReadAllText(@"CreateTable.sql");
                      SQLiteCommand command = new SQLiteCommand(script, sqlite);
                      command.ExecuteNonQuery();
                      sqlite.Close();
                  }
              }
          }
          public void InsertData()
          {
              SQLiteConnection con = new SQLiteConnection(@"Data Source=" + databaseName);
              con.Open();
              SQLiteDataAdapter dAdapter = new SQLiteDataAdapter("SELECT FROM MyTable", con);
              SQLiteCommandBuilder cmdBuilder = new SQLiteCommandBuilder(dAdapter);
              dAdapter.Fill(dt);
          }
      }
      

      When I use InserData() method, there is no runtime error but my SQLite table is still empty. Please help me.

      OriginalGriffO Offline
      OriginalGriffO Offline
      OriginalGriff
      wrote on last edited by
      #2

      Your InsertData method is reading data from the DEL - a SELECT query - instead of trying to write data to the DB - an INSERT query. Have a look here: DataAdapter.Update(DataSet) Method (System.Data.Common) | Microsoft Docs[^]

      "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt AntiTwitter: @DalekDave is now a follower!

      "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
      "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

      A 2 Replies Last reply
      0
      • OriginalGriffO OriginalGriff

        Your InsertData method is reading data from the DEL - a SELECT query - instead of trying to write data to the DB - an INSERT query. Have a look here: DataAdapter.Update(DataSet) Method (System.Data.Common) | Microsoft Docs[^]

        "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt AntiTwitter: @DalekDave is now a follower!

        A Offline
        A Offline
        Alex Dunlop
        wrote on last edited by
        #3

        I changed the InserData() method:

            public void InsertData()
            {
                SQLiteConnection con = new SQLiteConnection(@"Data Source=" + databaseName);
                con.Open();
                SQLiteDataAdapter dAdapter = new SQLiteDataAdapter("INSERT INTO MyTable", con);
                SQLiteCommandBuilder cmdBuilder = new SQLiteCommandBuilder();
                cmdBuilder.DataAdapter = dAdapter;
                dAdapter.Update(dt);
                con.Close();
            }
        

        It still doesn't work. Note that DataTable headers and column names in SQLite table are the same. What query should I use?

        1 Reply Last reply
        0
        • OriginalGriffO OriginalGriff

          Your InsertData method is reading data from the DEL - a SELECT query - instead of trying to write data to the DB - an INSERT query. Have a look here: DataAdapter.Update(DataSet) Method (System.Data.Common) | Microsoft Docs[^]

          "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt AntiTwitter: @DalekDave is now a follower!

          A Offline
          A Offline
          Alex Dunlop
          wrote on last edited by
          #4

          Is there any way to transfer them from DataTable to SQLite table at once and quickly? I can use For loop but my data is huge (40 columns and around 30000 rows).

          OriginalGriffO 1 Reply Last reply
          0
          • A Alex Dunlop

            Is there any way to transfer them from DataTable to SQLite table at once and quickly? I can use For loop but my data is huge (40 columns and around 30000 rows).

            OriginalGriffO Offline
            OriginalGriffO Offline
            OriginalGriff
            wrote on last edited by
            #5

            Yes. And if you follow the link I gave you it tells you how to!

            "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt AntiTwitter: @DalekDave is now a follower!

            "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
            "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

            A 2 Replies Last reply
            0
            • OriginalGriffO OriginalGriff

              Yes. And if you follow the link I gave you it tells you how to!

              "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt AntiTwitter: @DalekDave is now a follower!

              A Offline
              A Offline
              Alex Dunlop
              wrote on last edited by
              #6

              In the Link you provided, there is a string parameter for queryString. I need to INSERT data. What query should I use for transferring all cell information into SQLite?

              Richard Andrew x64R 1 Reply Last reply
              0
              • OriginalGriffO OriginalGriff

                Yes. And if you follow the link I gave you it tells you how to!

                "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt AntiTwitter: @DalekDave is now a follower!

                A Offline
                A Offline
                Alex Dunlop
                wrote on last edited by
                #7

                Solved. Thanks/ ;)

                1 Reply Last reply
                0
                • A Alex Dunlop

                  In the Link you provided, there is a string parameter for queryString. I need to INSERT data. What query should I use for transferring all cell information into SQLite?

                  Richard Andrew x64R Offline
                  Richard Andrew x64R Offline
                  Richard Andrew x64
                  wrote on last edited by
                  #8

                  It looks to me like the query string should be the SELECT query that pulls the data you want to transfer.

                  The difficult we do right away... ...the impossible takes slightly longer.

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups