ahmed elbarbary

ahmed elbarbary

  • NA
  • 1.6k
  • 275k

How to change return of function from string to generic list

Nov 22 2019 5:23 PM

problem

How to change return of function from string to return generic list by using csharp ?

I have stored procedure name getcompanies return list of companies id as following

  1. create proc getcompannies as select compnyid from companes where compnyid > 10  

so that result will be as following
11
12
13
14
15
etc...
so that i need to change function below from string to return generic list to be dynamically using

What I have tried:

  1. public static string ExecuteProcedureReturnString(string connString, string procName,  
  2.            params SqlParameter[] paramters)  
  3.        {  
  4.            string result = "";  
  5.            using (var sqlConnection = new SqlConnection(connString))  
  6.            {  
  7.                using (var command = sqlConnection.CreateCommand())  
  8.                {  
  9.                    command.CommandType = System.Data.CommandType.StoredProcedure;  
  10.                    command.CommandText = procName;  
  11.                    if (paramters != null)  
  12.                    {  
  13.                        command.Parameters.AddRange(paramters);  
  14.                    }  
  15.                    sqlConnection.Open();  
  16.                    var ret = command.ExecuteScalar();  
  17.                    if (ret != null)  
  18.                        result = Convert.ToString(ret);  
  19.                }  
  20.            }  
  21.            return result;  
  22.        }  

Answers (2)