Solved

SP_HELP returning the values in .net

Posted on 2007-04-01
5
1,010 Views
Last Modified: 2008-01-09
OK, im trying to call  sp_help in a query using c# in .net 1.14 (2003). Unfortunetly I only get the first row back....how do I get all the other rows? Its like the reader only returns the first part of sp_help.....

'ConString' is the database connection string, 'table' is the table name

public static void SelectAnythingSPHelp(string ConString, string table ,ref ArrayList d1,ref ArrayList d2,ref ArrayList d3,ref ArrayList d4,ref ArrayList d5,ref ArrayList d6,ref ArrayList d7,ref ArrayList d8,ref ArrayList d9,ref ArrayList d10)
{
      SqlConnection myConnection = new SqlConnection(ConString);
      SqlCommand myCommand = new SqlCommand("sp_help "+table,myConnection);
      myConnection.Open();
      SqlDataReader myReader;
      myReader = myCommand.ExecuteReader();
      while (myReader.Read())
      {
            try{d1.Add(myReader[0].ToString());}catch{}
            try{d2.Add(myReader[1].ToString());}catch{}
            try{d3.Add(myReader[2].ToString());}catch{}
            try{d4.Add(myReader[3].ToString());}catch{}
            try{d5.Add(myReader[4].ToString());}catch{}
            try{d6.Add(myReader[5].ToString());}catch{}
            try{d7.Add(myReader[6].ToString());}catch{}
            try{d8.Add(myReader[7].ToString());}catch{}
            try{d9.Add(myReader[8].ToString());}catch{}
            try{d10.Add(myReader[9].ToString());}catch{}
      }
      myReader.Close();
      myConnection.Close();
      return;
}
0
Comment
Question by:Saber37886661
5 Comments
 
LVL 29

Expert Comment

by:QPR
ID: 18834414
sp_help returns multiple tables - run it in Query anayser to see.
My GUESS is that you are returning the last one only
0
 
LVL 39

Accepted Solution

by:
appari earned 300 total points
ID: 18834417
use NextResult method to check if more results exists,try adding another while loop as follows
**may be my syntax is wrong in c sharp
do{
while (myReader.Read())
      {
            try{d1.Add(myReader[0].ToString());}catch{}
            try{d2.Add(myReader[1].ToString());}catch{}
            try{d3.Add(myReader[2].ToString());}catch{}
            try{d4.Add(myReader[3].ToString());}catch{}
            try{d5.Add(myReader[4].ToString());}catch{}
            try{d6.Add(myReader[5].ToString());}catch{}
            try{d7.Add(myReader[6].ToString());}catch{}
            try{d8.Add(myReader[7].ToString());}catch{}
            try{d9.Add(myReader[8].ToString());}catch{}
            try{d10.Add(myReader[9].ToString());}catch{}
      }
}
while(myReader.NextResult())
0
 
LVL 30

Assisted Solution

by:nmcdermaid
nmcdermaid earned 200 total points
ID: 18834419
sp_help returns 7 rowsets. This example shows how to step through recordsets.


http://support.microsoft.com/kb/311274
0
 
LVL 29

Expert Comment

by:QPR
ID: 18834423
You could disect sp_help into an sp of your own?
Or pipe the sp_help results into a temp table and then return * from that.
0
 
LVL 3

Author Comment

by:Saber37886661
ID: 18834506
appari and nmcdermaid both got it right, thanks guys :)
appari got more points because he was first (by 2 minutes). Thanks heaps for the prompt help guys :)
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

910 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now