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. creating insert statement using arrays

creating insert statement using arrays

Scheduled Pinned Locked Moved C#
data-structureshelp
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.
  • D Offline
    D Offline
    DownBySpj
    wrote on last edited by
    #1

    i want to create a insert statement for inserting data in a table. Here the column names are stored in one array and the values are stored in another array. Any idea will be of great help for me. thanx in advance all ur help

    G Z 2 Replies Last reply
    0
    • D DownBySpj

      i want to create a insert statement for inserting data in a table. Here the column names are stored in one array and the values are stored in another array. Any idea will be of great help for me. thanx in advance all ur help

      G Offline
      G Offline
      Guffa
      wrote on last edited by
      #2

      Create a query with parameters that have the same name as the fields, like: "insert into aTable (Some, Other) values (@Some, @Other)" Create a command object with the query, and add parameter objects for the parameters. Do you have the data type for the fields?

      --- b { font-weight: normal; }

      1 Reply Last reply
      0
      • D DownBySpj

        i want to create a insert statement for inserting data in a table. Here the column names are stored in one array and the values are stored in another array. Any idea will be of great help for me. thanx in advance all ur help

        Z Offline
        Z Offline
        zhengdong jin
        wrote on last edited by
        #3

        public string[] GenInsertCmd(DataTable table) { string inscmd = "insert into " + table.TableName.Trim().ToLower() + " ("; foreach(DataColumn col in table.Columns) { inscmd += col.ColumnName + ","; } inscmd = inscmd.Substring(0,inscmd.Length - 1) + ") values ("; string[] sqlcmd = new string[table.Rows.Count]; int i = 0; foreach(DataRow row in table.Rows) { sqlcmd[i] = ""; foreach(DataColumn col in table.Columns) { switch (col.DataType.Name.Trim().ToLower()) { case "string": try { sqlcmd[i] += "'" + ((string)row[col]).Trim().Replace("'","\"") + "',"; } catch { sqlcmd[i] += "null,"; } break; case "datetime": try { sqlcmd[i] += "'" + ((DateTime)row[col]).ToString("yyyy-MM-dd hh:mm:ss.sss") + "',"; } catch { sqlcmd[i] += "null,"; } break; case "boolean": try { sqlcmd[i] += ((bool)row[col] == true ? "1," : "0,"); } catch { sqlcmd[i] += "null,"; } break; case "byte[]": sqlcmd[i] += "null" + ","; break; default: if (row[col].ToString().Trim() == "") sqlcmd[i] += "null,"; else sqlcmd[i] += row[col].ToString().Trim() + ","; break; } } sqlcmd[i] = inscmd + sqlcmd[i].Substring(0,sqlcmd[i].Length - 1) + ")"; i += 1; } return sqlcmd; }

        G 1 Reply Last reply
        0
        • Z zhengdong jin

          public string[] GenInsertCmd(DataTable table) { string inscmd = "insert into " + table.TableName.Trim().ToLower() + " ("; foreach(DataColumn col in table.Columns) { inscmd += col.ColumnName + ","; } inscmd = inscmd.Substring(0,inscmd.Length - 1) + ") values ("; string[] sqlcmd = new string[table.Rows.Count]; int i = 0; foreach(DataRow row in table.Rows) { sqlcmd[i] = ""; foreach(DataColumn col in table.Columns) { switch (col.DataType.Name.Trim().ToLower()) { case "string": try { sqlcmd[i] += "'" + ((string)row[col]).Trim().Replace("'","\"") + "',"; } catch { sqlcmd[i] += "null,"; } break; case "datetime": try { sqlcmd[i] += "'" + ((DateTime)row[col]).ToString("yyyy-MM-dd hh:mm:ss.sss") + "',"; } catch { sqlcmd[i] += "null,"; } break; case "boolean": try { sqlcmd[i] += ((bool)row[col] == true ? "1," : "0,"); } catch { sqlcmd[i] += "null,"; } break; case "byte[]": sqlcmd[i] += "null" + ","; break; default: if (row[col].ToString().Trim() == "") sqlcmd[i] += "null,"; else sqlcmd[i] += row[col].ToString().Trim() + ","; break; } } sqlcmd[i] = inscmd + sqlcmd[i].Substring(0,sqlcmd[i].Length - 1) + ")"; i += 1; } return sqlcmd; }

          G Offline
          G Offline
          Guffa
          wrote on last edited by
          #4

          Ouch. Don't use a try statement to handle the null values, use an if statement instead. Exceptions should not be used for normal program flow.

          --- b { font-weight: normal; }

          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