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 insert data in a sql database from datagrid column

How to insert data in a sql database from datagrid column

Scheduled Pinned Locked Moved C#
databasequestionsql-serversysadmintutorial
4 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.
  • C Offline
    C Offline
    choopie
    wrote on last edited by
    #1

    I have DataGridColumnStyle-s assigned to my datagrid. The columns are filled with data. How can I insert the data of a single column to a SQL Server database? I have inserted the data of a column into a SortedList, so I want to insert these values using the Insert statement to a CommandText property of the SqlCommand class??? :sigh:

    M 1 Reply Last reply
    0
    • C choopie

      I have DataGridColumnStyle-s assigned to my datagrid. The columns are filled with data. How can I insert the data of a single column to a SQL Server database? I have inserted the data of a column into a SortedList, so I want to insert these values using the Insert statement to a CommandText property of the SqlCommand class??? :sigh:

      M Offline
      M Offline
      mikone
      wrote on last edited by
      #2

      you could do it like this or by using a sql/odbc/oledb datadapter (e.g. system.data.oledb.oledbdataadapter) and set it up. this is a bit more complicated and i used it only once so you should go and google for it. the msdn has some nice explaination about how to use this thing. if you want to do it by yourself, you will have to have an array (or list or anything else) containg the values and the fieldname (a 2-dimensional array would do the job ;) if you got a table consisting of 3 columns (e.g. id, name, postdate) you would need an array like this: data[0,0] = "id"; data[0,1] = 0; data[1,0] = "name"; data[1,1] = "myname"; data[2,0] = "postdate"; data[2,1] = "2006-05-02"; (alternatively you could use 2 single array, one holding the column names and the other one holding the column values) it would be easy to generate the statement then - it could look like string sqlquery = "INSERT INTO tablename ("; string comma = ""; for (int i = 0; i < data.GetLength(0)-1; i++) { if (i > 0) comma = "," sqlquery = sqlquery + comma + data[i,0]; } sqlquery = sqlquery + ") VALUES ("; for (int i = 0; i < data.GetLength(0)-1; i++) { if (i > 0) comma = "," sqlquery = sqlquery + comma + "'" + data[i,1] + "'"; } sqlquery = sqlquery + ");"; this code would produce a query like INSERT INTO tablename (id,name,postdate) VALUES ('0','myname','2006-05-02'); since this can be a lot of work you should make yourself familiar with the dataadapters :)

      C T 2 Replies Last reply
      0
      • M mikone

        you could do it like this or by using a sql/odbc/oledb datadapter (e.g. system.data.oledb.oledbdataadapter) and set it up. this is a bit more complicated and i used it only once so you should go and google for it. the msdn has some nice explaination about how to use this thing. if you want to do it by yourself, you will have to have an array (or list or anything else) containg the values and the fieldname (a 2-dimensional array would do the job ;) if you got a table consisting of 3 columns (e.g. id, name, postdate) you would need an array like this: data[0,0] = "id"; data[0,1] = 0; data[1,0] = "name"; data[1,1] = "myname"; data[2,0] = "postdate"; data[2,1] = "2006-05-02"; (alternatively you could use 2 single array, one holding the column names and the other one holding the column values) it would be easy to generate the statement then - it could look like string sqlquery = "INSERT INTO tablename ("; string comma = ""; for (int i = 0; i < data.GetLength(0)-1; i++) { if (i > 0) comma = "," sqlquery = sqlquery + comma + data[i,0]; } sqlquery = sqlquery + ") VALUES ("; for (int i = 0; i < data.GetLength(0)-1; i++) { if (i > 0) comma = "," sqlquery = sqlquery + comma + "'" + data[i,1] + "'"; } sqlquery = sqlquery + ");"; this code would produce a query like INSERT INTO tablename (id,name,postdate) VALUES ('0','myname','2006-05-02'); since this can be a lot of work you should make yourself familiar with the dataadapters :)

        C Offline
        C Offline
        choopie
        wrote on last edited by
        #3

        Thanks many! :)

        1 Reply Last reply
        0
        • M mikone

          you could do it like this or by using a sql/odbc/oledb datadapter (e.g. system.data.oledb.oledbdataadapter) and set it up. this is a bit more complicated and i used it only once so you should go and google for it. the msdn has some nice explaination about how to use this thing. if you want to do it by yourself, you will have to have an array (or list or anything else) containg the values and the fieldname (a 2-dimensional array would do the job ;) if you got a table consisting of 3 columns (e.g. id, name, postdate) you would need an array like this: data[0,0] = "id"; data[0,1] = 0; data[1,0] = "name"; data[1,1] = "myname"; data[2,0] = "postdate"; data[2,1] = "2006-05-02"; (alternatively you could use 2 single array, one holding the column names and the other one holding the column values) it would be easy to generate the statement then - it could look like string sqlquery = "INSERT INTO tablename ("; string comma = ""; for (int i = 0; i < data.GetLength(0)-1; i++) { if (i > 0) comma = "," sqlquery = sqlquery + comma + data[i,0]; } sqlquery = sqlquery + ") VALUES ("; for (int i = 0; i < data.GetLength(0)-1; i++) { if (i > 0) comma = "," sqlquery = sqlquery + comma + "'" + data[i,1] + "'"; } sqlquery = sqlquery + ");"; this code would produce a query like INSERT INTO tablename (id,name,postdate) VALUES ('0','myname','2006-05-02'); since this can be a lot of work you should make yourself familiar with the dataadapters :)

          T Offline
          T Offline
          Tom Wright
          wrote on last edited by
          #4

          yeah and maybe take a look at concurrencymanager.

          Tom Wright tawright915@gmail.com

          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