?
Solved

Using ExecuteReader

Posted on 2012-04-13
5
Medium Priority
?
310 Views
Last Modified: 2012-04-16
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
Comment
Question by:zhshqzyc
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 37

Expert Comment

by:momi_sabag
ID: 37842729
what do you need to do ?
what is missing from the code?
0
 
LVL 27

Expert Comment

by:Chinmay Patel
ID: 37842900
Are you looking for something like this?

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

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

Open in new window

0
 

Author Comment

by:zhshqzyc
ID: 37843796
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
 
LVL 27

Accepted Solution

by:
Chinmay Patel earned 2000 total points
ID: 37843840
no it will get all the records anyways so you have to process them one by one.
0
 
LVL 10

Expert Comment

by:adriankohws
ID: 37848564
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

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
I have a large data set and a SSIS package. How can I load this file in multi threading?
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question