Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
SqlDataReader Data Class Question
Message
From
27/10/2004 13:48:53
 
General information
Forum:
ASP.NET
Category:
Web forms
Environment versions
Environment:
C# 1.1
OS:
Windows 2000 SP3
Network:
Windows 2000 Server
Database:
MS SQL Server
Miscellaneous
Thread ID:
00954923
Message ID:
00954949
Views:
12
Tom,

Perhaps Randy's suggestion to use a public DataReader is best. This is just off the top of my head, but perhaps something like this would work:
public class ScrapDB
{
	public SqlDataReader oReader;
	
	protected SqlConnection oConnection;

	// Constructor
	public ScrapDB()
	{
		oConnection = this.GetConnection();
	
	}

	public SqlConnection GetConnection()
	{
		string connectionString = ConfigurationSettings.AppSettings["connectionString"];
		
		return new SqlConnection(connectionString);
	
	}

	public void GetUsers()
	{
		string selectStatement = "SELECT UserID "
					+ "FROM UserInfo ";
			
		SqlCommand selectCommand = new SqlCommand(selectStatement, this.oConnection);
			
		this.oConnection.Open();
		this.oReader = selectCommand.ExecuteReader(CommandBehavior.CloseConnection);
		this.oReader.Read() ;
	}
}
A call to GetUsers would simply open your connection and set up the Reader. You would then use the public oReader in your UI to read in your data. You'd also need to use oReader.Close() to close the Reader and the Connection. Again, this is just off the top of my head and there may be some problems with this code, but I think it's enough to maybe get you going in this direction. Since I haven't used DataReaders, I may be off on a coupla things. =)

~~Bonnie


>Here is what I came up with and I have a question about GetUsers().
>
>public class ScrapDB
>	{
>
>		public static SqlConnection GetConnection()
>		{
>			string connectionString = ConfigurationSettings.AppSettings["connectionString"];
>		
>			return new SqlConnection(connectionString);
>	
>		}
>
>	public static SqlDataReader GetUsers()
>				{
>			// Use a SqlDataReader to fill ddlUsers...
>			SqlDataReader UsersReader = null ;
>				
>					SqlConnection UsersConnection = GetConnection() ;
>					
>					string selectStatement = "SELECT UserID "
>						+ "FROM UserInfo ";
>			
>					SqlCommand selectCommand = new SqlCommand(selectStatement, UsersConnection);
>					//SqlDataReader UsersReader;
>			
>					UsersConnection.Open();
>					UsersReader = selectCommand.ExecuteReader(CommandBehavior.CloseConnection);
>					UsersReader.Read() ;
>					return UsersReader ;
>				
>				
>				        UsersReader.Close() ;
>			
>		
>
>			}
>}
>
>
>
>
>
>Notice the line of code at the end of return UsersReader.
>
>UsersReader.Close;
>
>The puppy complies and runs fine and returns no errors. However, with the form running when I go to the data class I see a blue wavey line (problem). By placing the cursor on top of UsersReader.Close() I have the following message - "Unreachable code detected".
>
>Perhaps I am not there yet but I am close. Should I be concerned about this message? I just do not like it.
>
>Tom
Bonnie Berent DeWitt
NET/C# MVP since 2003

http://geek-goddess-bonnie.blogspot.com
Previous
Reply
Map
View

Click here to load this message in the networking platform