Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 317
  • Last Modified:

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.
0
zhshqzyc
Asked:
zhshqzyc
1 Solution
 
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
 
Chinmay PatelEnterprise ArchitectCommented:
no it will get all the records anyways so you have to process them one by one.
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

A proven path to a career in data science

At Springboard, we know how to get you a job in data science. With Springboard’s Data Science Career Track, you’ll master data science  with a curriculum built by industry experts. You’ll work on real projects, and get 1-on-1 mentorship from a data scientist.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now