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. select, insert, update, del with ADO.net with C#

select, insert, update, del with ADO.net with C#

Scheduled Pinned Locked Moved C#
csharptutorialdatabaseannouncement
3 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.
  • A Offline
    A Offline
    Azel Low
    wrote on last edited by
    #1

    Hi, is there anyone who can point or provide me with some samples of how to Select, Insert Update and delete using OLEDB in C# Preferably a simple basic example that doesn't use stored procedure. thanks.

    U 1 Reply Last reply
    0
    • A Azel Low

      Hi, is there anyone who can point or provide me with some samples of how to Select, Insert Update and delete using OLEDB in C# Preferably a simple basic example that doesn't use stored procedure. thanks.

      U Offline
      U Offline
      Uwe Keim
      wrote on last edited by
      #2

      I use something similar to this for INSERT an new record:

      OleDbConnection conn = new OleDbConnection( DB_STR );
      OleDbDataAdapter da = new OleDbDataAdapter( "SELECT TOP 1 * FROM Guestbook", conn );
      OleDbCommandBuilder cb = new OleDbCommandBuilder( da );
      cb.QuotePrefix = "[";
      cb.QuoteSuffix = "]";

      DataSet ds = new DataSet();
      da.Fill( ds );

      DataTable table = ds.Tables[0];
      DataRow row = table.NewRow();

      row["Date"] = DateTime.Now;
      row["Name"] = name;
      row["EMail"] = email;
      row["Text"] = text;
      table.Rows.Add( row );

      da.Update( ds );

      conn.Close();

      And for INSERT or UPDATE:

      OleDbConnection conn = new OleDbConnection( DB_STR );
      // do your query for a specific recordset.
      // if it returns none, you must create a new, otherwise modify the existing.
      OleDbDataAdapter da = new OleDbDataAdapter( "SELECT TOP 1 * FROM Guestbook WHERE ID="+ID, conn );
      OleDbCommandBuilder cb = new OleDbCommandBuilder( da );
      cb.QuotePrefix = "[";
      cb.QuoteSuffix = "]";

      DataSet ds = new DataSet();
      da.Fill( ds );

      DataTable table = ds.Tables[0];

      // create new.
      if ( table.Rows.Count==0 )
      {
      DataRow row = table.NewRow();

      row\["Date"\] = DateTime.Now;
      row\["Name"\] = name;
      row\["EMail"\] = email;
      row\["Text"\] = text;
      
      table.Rows.Add( row );
      

      }
      // modify existing.
      else
      {
      DataRow row = table.Rows[0];

      row\["Date"\] = DateTime.Now;
      row\["Name"\] = name;
      row\["EMail"\] = email;
      row\["Text"\] = text;
      

      }

      da.Update( ds );

      conn.Close();

      For the SELECT and DELETE I just use single-statement SQL-queries, which I format with string.Format(). -- - Free Windows-based CMS: www.zeta-software.de/enu/producer/freeware/download.html - See me: www.magerquark.de

      A 1 Reply Last reply
      0
      • U Uwe Keim

        I use something similar to this for INSERT an new record:

        OleDbConnection conn = new OleDbConnection( DB_STR );
        OleDbDataAdapter da = new OleDbDataAdapter( "SELECT TOP 1 * FROM Guestbook", conn );
        OleDbCommandBuilder cb = new OleDbCommandBuilder( da );
        cb.QuotePrefix = "[";
        cb.QuoteSuffix = "]";

        DataSet ds = new DataSet();
        da.Fill( ds );

        DataTable table = ds.Tables[0];
        DataRow row = table.NewRow();

        row["Date"] = DateTime.Now;
        row["Name"] = name;
        row["EMail"] = email;
        row["Text"] = text;
        table.Rows.Add( row );

        da.Update( ds );

        conn.Close();

        And for INSERT or UPDATE:

        OleDbConnection conn = new OleDbConnection( DB_STR );
        // do your query for a specific recordset.
        // if it returns none, you must create a new, otherwise modify the existing.
        OleDbDataAdapter da = new OleDbDataAdapter( "SELECT TOP 1 * FROM Guestbook WHERE ID="+ID, conn );
        OleDbCommandBuilder cb = new OleDbCommandBuilder( da );
        cb.QuotePrefix = "[";
        cb.QuoteSuffix = "]";

        DataSet ds = new DataSet();
        da.Fill( ds );

        DataTable table = ds.Tables[0];

        // create new.
        if ( table.Rows.Count==0 )
        {
        DataRow row = table.NewRow();

        row\["Date"\] = DateTime.Now;
        row\["Name"\] = name;
        row\["EMail"\] = email;
        row\["Text"\] = text;
        
        table.Rows.Add( row );
        

        }
        // modify existing.
        else
        {
        DataRow row = table.Rows[0];

        row\["Date"\] = DateTime.Now;
        row\["Name"\] = name;
        row\["EMail"\] = email;
        row\["Text"\] = text;
        

        }

        da.Update( ds );

        conn.Close();

        For the SELECT and DELETE I just use single-statement SQL-queries, which I format with string.Format(). -- - Free Windows-based CMS: www.zeta-software.de/enu/producer/freeware/download.html - See me: www.magerquark.de

        A Offline
        A Offline
        Azel Low
        wrote on last edited by
        #3

        thanks. I got a question. what is the following code for? cb.QuotePrefix = "["; cb.QuoteSuffix = "]"; thanks =)

        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