Solved

Using ExecuteReader

Posted on 2012-04-13
5
306 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 500 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

Free eBook: Backup on AWS

Everything you need to know about backup and disaster recovery with AWS, for FREE!

Question has a verified solution.

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

Suggested Solutions

For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

730 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