0

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?

Amalpriya
  • 123
  • 1
  • 14
  • 3
    possible duplicate of [How to pass an array into a SQL Server stored procedure](http://stackoverflow.com/questions/11102358/how-to-pass-an-array-into-a-sql-server-stored-procedure) – horHAY Apr 20 '15 at 09:51
  • I already read that post but they passed the values as List. Should I convert my array into list? What is that and how do I do that? – Amalpriya Apr 20 '15 at 10:00
  • Also they are doing the database connection by using(conn). But I use a ADO.Net Entity object. Aren't both the connections different?? Sorry for my ignorance since I am new to the concept – Amalpriya Apr 20 '15 at 10:03
  • I personally have not used a ADO.Net Entity object, so I'm really not of great help here. The example connects to the SQL Server and passes a DataTable, not a list (I know the parameter is confusing). The parameter itself is a user-defined table type, and then you use a merge to insert the data into a database. – horHAY Apr 20 '15 at 10:13
  • @Amalpriya check this link [link](http://www.kodyaz.com/articles/t-sql-convert-split-delimeted-string-as-rows-using-xml.aspx) – Arun Gairola Apr 20 '15 at 10:19
  • Concerning the close votes (duplicate of...), I disagree partially: This question is about Entity Framework; the question of which this is supposed to be a duplicate is about plain T-SQL. There might result in different answers. – stakx - no longer contributing Apr 21 '15 at 12:36
  • @stakx is right. I tried to work out the solution for ADO.Net from another link but the main problem seems to exist that entity framework does not support table - valued parameters. So the problem is different from the post to which this is tagged as duplicate – Amalpriya Apr 22 '15 at 00:01

1 Answers1

0

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.

Fabio Iotti
  • 1,480
  • 1
  • 16
  • 20
Amalpriya
  • 123
  • 1
  • 14