Using ExecuteReader

I have stored procedures, which like
SELECT * FROM settings ORDER BY [name]

Open in new window

or
SELECT [Value] FROM settings WHERE [Name]=@name

Open in new window

I wrote the code for the generic cases below but unfinished.
Thanks for advice.
public string ExecuteReader(string storedProcedure)
        {
            return ExecuteReader(storedProcedure, null);
        }

        public string ExecuteReader(string storedProcedure, Dictionary<string, object> parameters)
        {
            string temp = string.Empty;
            using (SqlConnection conn = new SqlConnection(_connectionString))
            {
                conn.Open();
                using (SqlCommand cmd = new SqlCommand(storedProcedure, conn))
                {
                    cmd.CommandType = CommandType.StoredProcedure;
                    if (parameters != null)
                    {
                        foreach (string parameter in parameters.Keys)
                        {
                            cmd.Parameters.AddWithValue(parameter, parameters[parameter] ?? DBNull.Value);
                        }
                    }
                    using (SqlDataReader reader = cmd.ExecuteReader())
                    {
                        reader.Read();
                        temp=reader.GetValue[
                    }
                   
                }
                conn.Close();
            }
            return temp;
        }

Open in new window

Thanks.
zhshqzycAsked:
Who is Participating?
 
Chinmay PatelConnect With a Mentor Enterprise ArchitectCommented:
no it will get all the records anyways so you have to process them one by one.
0
 
momi_sabagCommented:
what do you need to do ?
what is missing from the code?
0
 
Chinmay PatelEnterprise ArchitectCommented:
Are you looking for something like this?

 temp1=reader.GetValue["column1"].ToString();

 temp2=reader.GetValue["column2"].ToString();

Open in new window

0
 
zhshqzycAuthor Commented:
Yes. Do we need pass the column name?
And also if I used
select * from table

Open in new window

.
Then the program will return a list instead of a string.
We have to override the code?
0
 
adriankohwsCommented:
It's something special you need to do? Why are you putting the values of all the results into a string and not manipulating the data?
0
All Courses

From novice to tech pro — start learning today.