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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. Visual Studio
  4. reading a string from a txt file & insert it in a database table?

reading a string from a txt file & insert it in a database table?

Scheduled Pinned Locked Moved Visual Studio
questioncsharpdatabase
2 Posts 2 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.
  • S Offline
    S Offline
    starist
    wrote on last edited by
    #1

    plz how can i read a string from a txt file then insert it in a database table using C# in visualstudio2005 ?

    L 1 Reply Last reply
    0
    • S starist

      plz how can i read a string from a txt file then insert it in a database table using C# in visualstudio2005 ?

      L Offline
      L Offline
      Lutoslaw
      wrote on last edited by
      #2

      using (System.Data.SqlClient.SqlConnection connection = new System.Data.SqlClient.SqlConnection("Here goes your connection string")) {

      // First get DataSet
      System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand("Here goes your query string");
      connection.Open();
      System.Data.SqlClient.SqlDataReader dbReader = cmd.ExecuteReader();
      DataSet ds = new DataSet();
      ds.Load(dbReader, LoadOption.OverwriteChanges, "Your table name");
      dbReader.Close();
      
      // Now read txt
      System.IO.StreamReader txtReader = System.IO.File.OpenText("Path of TXT file");
      // Here read data from TXT file, e.g.:
      string someDataForColumn1 = txtReader.ReadLine();
      
      // Edit DataSet
      DataRow newRow = ds.Tables\[0\].DefaultView.AddNew().Row;
      newRow\["Column1"\] = someDataForColumn1;
      
      // Save DB
      ds.AcceptChanges();
      // Close connection (end of "using" block")
      

      }

      I cannot test it now, so if any error appears please try to experiment a bit.

      Greetings - Gajatko Portable.NET is part of DotGNU, a project to build a complete Free Software replacement for .NET - a system that truly belongs to the developers.

      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