[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Using ExecuteReader

Posted on 2012-04-13
5
Medium Priority
?
314 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
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

Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

Question has a verified solution.

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

One of the most important things in an application is the query performance. This article intends to give you good tips to improve the performance of your queries.
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Suggested Courses

825 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