简体   繁体   中英

How to bind array data to SqlCommand.Parameter?

It's possible to perform this operation without "foreach" ???

I would understand if there are library functions to associate a parameter a list of data and perform N insert all together

 String SQL_DETAIL = "INSERT INTO [ImportDetail] " +
                                                      " ([idMaster] " +
                                                      " ,[operation] " +
                                                      " ,[data]) " +
                                                 " VALUES " +
                                                      " (@a,@b,@c) ";

foreach (ImportDetail imp in this.lstImportDetail )
{

     SqlCommand dettCommand = new SqlCommand(SQL_DETAIL, myTrans.Connection);
     dettCommand.Transaction = myTrans;

     dettCommand.Parameters.Add("a", SqlDbType.Int).Value = imp.IdMaster;
     dettCommand.Parameters.Add("b", SqlDbType.NVarChar).Value = imp.Operation;
     dettCommand.Parameters.Add("c", SqlDbType.NVarChar).Value = imp.Data;


     i =i+ (int)dettCommand.ExecuteNonQuery();
 }

Thank for help

If you were feeling frisky, you could create a user-defined table type and put the data from all your ImportDetail objects into a DataTable . This wouldn't totally alleviate the need for a loop, though -- it just makes it more efficient since you're only transforming data in memory instead of passing it to a remote database. Once you have your DataTable populated to match the schema of your table type, you can pass it as a parameter to a stored procedure to handle your INSERT s. The procedure can be made transactional if you'd like, and since your input data will already be in a table (of sorts), the SQL for the stored procedure should be pretty simple, as well.

For more info on user-defined table types and how they're used in .NET, check out this link: http://msdn.microsoft.com/en-us/library/bb675163.aspx

Going a little further, if this is something you have to do quite often, it's not out of the realm of possibility to turn this into some sort of extension method on IEnumerable to keep things DRY.

Is this the sort of solution you were looking for? It's a little hard for me to tell given the wording of the question.

You can define two array list: the first will have the name of the parameter and the second its value:

        ArrayList arrayName = new ArrayList { };
        ArrayList arrayValue = new ArrayList { };

        arrayName.Add("@Parameter1");
        arrayName.Add("@Parameter2");
        arrayValue.Add(value1);
        arrayValue.Add(value2);

        for (int i = 0; i < arrayName.Count; i++)
        {
        cmd.Parameters.AddWithValue(arrayName[i].ToString(), arrayValue[i]);
        }

Then execute command

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM