zhshqzyc
asked on
Using ExecuteReader
I have stored procedures, which like
Thanks for advice.
SELECT * FROM settings ORDER BY [name]
orSELECT [Value] FROM settings WHERE [Name]=@name
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;
}
Thanks.
Are you looking for something like this?
temp1=reader.GetValue["column1"].ToString();
temp2=reader.GetValue["column2"].ToString();
ASKER
Yes. Do we need pass the column name?
And also if I used
Then the program will return a list instead of a string.
We have to override the code?
And also if I used
select * from table
.Then the program will return a list instead of a string.
We have to override the code?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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?
what is missing from the code?