|
-
Sep 30th, 2003, 12:11 PM
#1
Thread Starter
Frenzied Member
Cannot Convert Type
Error Message:
DatabaseClass.cs(102,12): error CS0029: Cannot implicitly convert type 'System.Data.DataSet' to 'System.Data.SqlClient.SqlDataReader'
Code:
public DataSet db_query(string SqlStatement, bool isStoredProc)
{
DataSet dSet = new DataSet();
try
{
if(SqlStatement == "")
{
MessageBox.Show("SQL Statment is empty.");
}
if(isStoredProc == true)
{
String strQuery = "EXEC " + SqlStatement.Trim();
SqlDataAdapter sAdapter = new SqlDataAdapter(strQuery,myConnection);
sAdapter.Fill(dSet,SqlStatement);
}
else
{
string strQuery = SqlStatement.Trim();
SqlDataAdapter sAdapter = new SqlDataAdapter(strQuery,myConnection);
sAdapter.Fill(dSet,SqlStatement);
}
}
catch(Exception exp)
{
MessageBox.Show("Error fetching records for the query:- " + SqlStatement + ". Possible cause:- " + exp.ToString() );
}
return(dSet);
}
public static void Main()
{
string myConnectionString = ("server=localhost;database=pubs;user id=steve;password=jensen;");
Database myDB = new Database();
SqlDataReader reader;
db_open(myConnectionString);
reader = myDB.db_query("SELECT * FROM authors", false);
while(reader.Read())
{
Console.WriteLine(reader["au_lname"]);
}
db_close();
}
Obviously the issue is in reader = myDB.db_query, since db_query returns a DataSet and I am trying to read the returned DataSet with the SqlReader...Any ideas on how to do this???
Last edited by Memnoch1207; Sep 30th, 2003 at 12:19 PM.
Being educated does not make you intelligent.
Need a weekend getaway??? Come Visit
-
Sep 30th, 2003, 05:06 PM
#2
Frenzied Member
Why would you want to do that? If you are going to read the data with a datareader might as well just return a datareader object.
-
Sep 30th, 2003, 07:54 PM
#3
PowerPoster
Why not this:
Code:
public static void Main()
{
string myConnectionString = ("server=localhost;database=pubs;user id=steve;password=jensen;");
Database myDB = new Database();
DataSet myDS = new DataSet();
db_open(myConnectionString);
myDS = myDB.db_query("SELECT * FROM authors", false);
foreach(DataRow dr in myDS.Tables(0).Rows)
{
Console.WriteLine(dr["au_lname"]);
}
db_close();
}
-
Oct 2nd, 2003, 09:29 AM
#4
Thread Starter
Frenzied Member
instead of "public DataSet db_query", I just changed it to public "SqlReader db_query"...that way I am not running into the conversion issue...everything seems to be working properly now...It opens the connection, queries the database, returns the records, and closes the connection.
Being educated does not make you intelligent.
Need a weekend getaway??? Come Visit
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|