Click here to Skip to main content
15,891,851 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I have a stored procedure that returns the path of data and log file for a particular database.
The sp returns a single row and multiple columns.I have to read the data from each and every row.
SqlDataReader is not helping me or may be i have not understood it's complete implementation.

How can i read the required rows?
Any explanation is welcomed...

Code:
C#
string dataFilePath;
string logFilePath;
SqlCommand cmdspText = new SqlCommand("GetAllPhysicalPath", conn);
cmdspText.CommandType = CommandType.StoredProcedure;
cmdspText.Parameters.AddWithValue("@txtDatabaseName", userSpecifiedDBNameInTxtBox);
cmdspText.ExecuteNonQuery();
SqlDataReader reader = new SqlDataReader();
while(reader.Read())
{
//what should i write here
}
Posted
Updated 18-Jun-14 2:12am
v2
Comments
Herman<T>.Instance 18-Jun-14 5:37am    
what code can you share to see where you are stuck?
Anshumaan Chaturvedi 18-Jun-14 5:39am    
string dataFilePath;
string logFilePath;
SqlCommand cmdspText = new SqlCommand("GetAllPhysicalPath", conn);
cmdspText.CommandType = CommandType.StoredProcedure;
cmdspText.Parameters.AddWithValue("@txtDatabaseName", userSpecifiedDBNameInTxtBox);
cmdspText.ExecuteNonQuery();
SqlDataReader reader = new SqlDataReader();
while(reader.Read())
{
//what should i write here
}
Dr Drastic 18-Jun-14 5:40am    
I agree with digimanus,

If you post the code, we will be able to help you much easier.
Anshumaan Chaturvedi 18-Jun-14 5:40am    
string dataFilePath;
string logFilePath;
SqlCommand cmdspText = new SqlCommand("GetAllPhysicalPath", conn);
cmdspText.CommandType = CommandType.StoredProcedure;
cmdspText.Parameters.AddWithValue("@txtDatabaseName", userSpecifiedDBNameInTxtBox);
cmdspText.ExecuteNonQuery();
SqlDataReader reader = new SqlDataReader();
while(reader.Read())
{
//what should i write here
}
King Fisher 18-Jun-14 14:17pm    
use dataset instead of Readerr.

The code that you have provided in comments is wrong. If your stored procedure returns the rows then you should not use ExecuteNonQuery function to execute the procedure because ExecuteNonQuery function returns only the number of row affected while executing your stored procedure.

You have to use SqlDataAdapter or ExecuteReader, if your stored procedure returns some rows. Please refer the below code using ExecuteReader.

C#
 SqlCommand cmdspText = new SqlCommand("GetAllPhysicalPath", conn);
 cmdspText.CommandType = CommandType.StoredProcedure;
 cmdspText.Parameters.AddWithValue("@txtDatabaseName", userSpecifiedDBNameInTxtBox);
// Assign the results to SqlDataReader by executing the ExecuteReader function
 SqlDataReader reader = cmdspText.ExecuteReader();

// While loop will continue to execute until all records in SQLDataReader got read from it one by one. 
 while(reader.Read())
 {
   //write code to fetch results from data reader..
 }
// Close the object of SqlDataReader
reader.Close();


Happy Coding :)
 
Share this answer
 
v2
Comments
Kim Togo 18-Jun-14 8:20am    
Good answer, it is worth to mention that the code from OP does not work because SqlDataReader is created without any relationship with a SqlCommand object.

"SqlDataReader reader = new SqlDataReader()"
You can add you data to a datatable
C#
DataTable dt = new DataTable();
dt.Rows.Add("Mydata1", "Mydata2", "Mydata3", "Mydata4");


while read your loop will continue adding rows


And then you retreive your data,

C#
int MyCounter = 0

if (dt.Rows.Count > 0)
{

foreach (DataRow dr in dt.Rows)
{
{

string RetreiveData1 = dt.Rows[MyCounter].ItemArray["Mydata1"].ToString();
string RetreiveData2 = dt.Rows[MyCounter].ItemArray["Mydata2"].ToString();
MyCounter += 1;

}
}


You use MyCounter to move to next row.


I hope this helps!
 
Share this answer
 
v4

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900