简体   繁体   中英

How to pass a string array into a stored procedure in SQL Server from c# via ADO.NET entity model

I am currently developing an application in c# where I have a string array NameArray and recorded the number of values in the array as count. I need to pass this to my sql server database where I will store each of the value of the array as individual records(names) into a Table. How do I write a stored procedure for doing such a operation.

Furthermore the name of the entity connection string is MyEntities and I have created its object as Entityobj.

The code I am trying to execute in c# is something like this

    public void Method1(string[] NameArray, int count)
     {
      Entityobj.CallSproc(NameArray,count);// Here I am passing the values to stored procedure
     }

How do I make the stored procedure receive the values and store them as individual records?

Anyways I worked out the problem myself with the help of using this link http://www.c-sharpcorner.com/UploadFile/78607b/using-table-valued-parameters-in-entity-framework/

The link shows how to pass simple values to Table value parameters using Entity object. I edited the code to pass an by adding each value of the array into a single row(through iteration). I don't know whether this is a efficient way to do it but it serves my purpose well and code is working pretty fine.

Also there was a warning in my code as Entity Framework does not support table as datatype(table valued parameters) but the code worked fine anyway.

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