473,326 Members | 2,588 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Dynamic parametized sql insert - is this a good idea?

I wanted to build an insert sql statement with an unknown number of values. Would this be better as a stored procedure? Is it possible to do this as a stored procedure?

Expand|Select|Wrap|Line Numbers
  1. public static string sqlInsert(string table, ArrayList values, int numValues)
  2. {
  3.     string result = String.empty;
  4.     string sqlValues = String.Empty;
  5.     for (int i = 1; i <= numValues; i++)
  6.     {
  7.         if (i < numValues)
  8.         {
  9.             sqlValues += "@val" + i + ",";
  10.         }
  11.         else
  12.         {
  13.             sqlValues += "@val" + i;
  14.         }
  15.     }
  16.     string sql = "insert into dbo." + table + " values (" + sqlValues + ")";
  17.  
  18.     SqlConnection conSql = new SqlConnection(ConnStr);            
  19.     SqlCommand sc = new SqlCommand(sql, conSql);
  20.  
  21.     int j = 1;
  22.     foreach (string value in values)
  23.     {
  24.         if (String.IsNullOrEmpty(value))
  25.         {
  26.             sc.Parameters.AddWithValue("@val" + j, DBNull.Value);
  27.         }
  28.         else
  29.         {
  30.             sc.Parameters.AddWithValue("@val" + j, value);
  31.         }
  32.         j++;
  33.     }
  34.  
  35.     try
  36.     {
  37.         sc.Connection.Open();
  38.         sc.ExecuteNonQuery();
  39.     }
  40.     catch (Exception ex)
  41.     {
  42.         result = ex.Message;
  43.     }
  44.     finally
  45.     {
  46.         sc.Connection.Close();
  47.     }
  48.     return result;
  49. }
Jan 25 '10 #1
0 1205

Sign in to post your reply or Sign up for a free account.

Similar topics

10
by: | last post by:
I am trying to improve the robustness and elegance of my parametized sql statements in ASP 3.0 as they get passed to the sql server SP. Could anyone tell me if there are weaknessess in the way I...
0
by: Shailesh | last post by:
If I'm not mistaken, C++ doesn't have support for dynamic class members. I'm considering if such a facility would be useful, and what method would make a good workaround. I have a generic...
19
by: Christian Fowler | last post by:
I have a VERY LARGE pile of geographic data that I am importing into a database (db of choice is postgres, though may hop to oracle if necessary). The data is strictly hierarchical - each node has...
0
by: Jason | last post by:
Hi I wanted to bounce an Idea off you guys. I'm not sure if it's possible or even how to implement it .. or possibly if I'm trying to re-invent the wheel. Our users currently use excel and a...
3
by: Mukesh | last post by:
sir, i am developing a database, which will store the users profile both personal and professional which includes the address, telephone, gender and etc. in my main table i have created a column...
23
by: sandy | last post by:
I need (okay, I want) to make a dynamic array of my class 'Directory', within my class Directory (Can you already smell disaster?) Each Directory can have subdirectories so I thought to put these...
9
by: pbd22 | last post by:
Hi. This is just a disaster management question. I am using XMLHTTP for the dynamic loading of content in a very crucial area of my web site. Same as an IFrame, but using XMLHTTP and a DIV. I...
1
by: MaryamSh | last post by:
Hi, I am creating a Dynamic Search in my application. I create a user control and in Page_load event I create a dynamic dropdownlist and 2 dynamic button (Add,Remove) By pressing Add button...
0
by: MaryamSh | last post by:
Create Dynamic Dropdownlist Controls and related event -------------------------------------------------------------------------------- Hi, I am creating a Dynamic Search in my application. I...
6
by: raylopez99 | last post by:
This thread is about how variables or parameters (including objects) are passed between forms (namely, using parameterized constructors, e.g., to pass an int between forms (e.g., a calling form and...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.