sydneyguy
asked on
sqlserver c# returnign the array from a function call
i am using the below code to open a connection and read the data from the table but what i woudl lke to do is to return the data set back to the calling location so i can do more things with it, so i need to know please how to set these two functions to return the data
thanks for you help
private void connectsqlserver_Click(obj ect sender, EventArgs e)
{
readrec("select * from tblSearchTerms");
}
private void readrec(string sqlstring)
{
SqlConnection connection1 = new SqlConnection(@"Data Source=DICKSMITHMIR-PC\SQL EXPRESS;In itial Catalog=PubMedExtract;Inte grated Security=SSPI;");
connection1.Open();
SqlCommand sqlComm = new SqlCommand("select * from tblSearchTerms", connection1);
SqlDataReader myCommand = sqlComm.ExecuteReader();
while (myCommand.Read())
{
MessageBox.Show(myCommand[ "SearchTer mStart"].T oString()) ;
}
myCommand.Close();
// this is were i need the rec source returned also the setup for the void and return function set up syntax
}
thanks for you help
private void connectsqlserver_Click(obj
{
readrec("select * from tblSearchTerms");
}
private void readrec(string sqlstring)
{
SqlConnection connection1 = new SqlConnection(@"Data Source=DICKSMITHMIR-PC\SQL
connection1.Open();
SqlCommand sqlComm = new SqlCommand("select * from tblSearchTerms", connection1);
SqlDataReader myCommand = sqlComm.ExecuteReader();
while (myCommand.Read())
{
MessageBox.Show(myCommand[
}
myCommand.Close();
// this is were i need the rec source returned also the setup for the void and return function set up syntax
}
ASKER
ReadRec("select * from tblSearchTerms");
calls the read the data source but how do i view the returned data when it is returned
calls the read the data source but how do i view the returned data when it is returned
example of returning a DataTable:-
private SqlDataReader readrec(string sqlstring)
{
SqlConnection connection1 = new SqlConnection(@"Data Source=DICKSMITHMIR-PC\SQLEXPRESS;Initial Catalog=PubMedExtract;Integrated Security=SSPI;");
SqlCommand sqlComm = new SqlCommand("select * from tblSearchTerms", connection1);
SqlDataAdapter sda = new SqlDataAdapter(sqlComm );
DataTable dtData = new DataTable();
sda.Fill(dtData);
return dtData;
}
to view the returned data you can bind the datatable to a gridview.
if you want to view the data while debugging follow this link:-
http://stackoverflow.com/q uestions/1 337084/how -to-view-a -datatable -while-deb uging
http://stackoverflow.com/q
another link which describes how to view data in a datatable at run time using DataTable Visualizer :-
http://msmvps.com/blogs/de borahk/arc hive/2009/ 07/10/dal- datatable- visualizer .aspx
http://msmvps.com/blogs/de
ASKER
not to view the data in a data drid what i wanted to see was the data in the debuger mode and look through the record set
have included a screen shot of what i want to access.
at the moment i am still not able to return the myCommand data
recordset.png
have included a screen shot of what i want to access.
at the moment i am still not able to return the myCommand data
recordset.png
ASKER
dont i have to call the read function
myCommand = ReadRec("select * from tblSearchTerms"); or something
myCommand = ReadRec("select * from tblSearchTerms"); or something
to view the data in debugger mode you should use a datatable and view the data in datatable visaulizer
as stated above.
as stated above.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
return dtData;
Cannot implicitly convert type 'System.Data.DataTable' to 'System.Data.SqlClient.Sql
ASKER
DataTable dt=New DataTable();
also throws up a
Error 1 ; expected
also throws up a
Error 1 ; expected
ASKER
DataTable dt=New DataTable();
also throws up a
Error 1 ; expected
should be new just a typo exscuse last post
also throws up a
Error 1 ; expected
should be new just a typo exscuse last post
ASKER
SqlConnection connection1 = new SqlConnection(@"Data Source=DICKSMITHMIR-PC\SQL EXPRESS;In itial Catalog=PubMedExtract;Inte grated Security=SSPI;");
SqlCommand sqlComm = new SqlCommand(sqlstring, connection1);
SqlDataAdapter sda = new SqlDataAdapter(sqlComm);
DataTable dtData = new DataTable();
sda.Fill(dtData);
return dtData;
throws up a Error 22 Cannot implicitly convert type 'System.Data.DataTable' to 'System.Data.SqlClient.Sql DataReader '
SqlCommand sqlComm = new SqlCommand(sqlstring, connection1);
SqlDataAdapter sda = new SqlDataAdapter(sqlComm);
DataTable dtData = new DataTable();
sda.Fill(dtData);
return dtData;
throws up a Error 22 Cannot implicitly convert type 'System.Data.DataTable' to 'System.Data.SqlClient.Sql
please use the below function i ve changed the return type from datareader to datatable :-
private DataTable readrec(string sqlstring)
{
SqlConnection connection1 = new SqlConnection(@"Data Source=DICKSMITHMIR-PC\SQLEXPRESS;Initial Catalog=PubMedExtract;Integrated Security=SSPI;");
SqlCommand sqlComm = new SqlCommand(sqlstring, connection1);
SqlDataAdapter sda = new SqlDataAdapter(sqlComm );
DataTable dtData = new DataTable();
sda.Fill(dtData);
return dtData;
}
ASKER
in the old way i could use the below code to cycle through the recs how can i do it using the DataTable please
while (dt.Read())
{
// Console.WriteLine(r["Searc hTermStart "].ToStrin g());
MessageBox.Show(dt["Search TermStart" ].ToString ());
}
while (dt.Read())
{
// Console.WriteLine(r["Searc
MessageBox.Show(dt["Search
}
ASKER
this is what i am trying
DataTable dt = new DataTable();
dt = readrec("select * from tblSearchTerms");
MessageBox.Show(dt.ToStrin g());
DataTable dt = new DataTable();
dt = readrec("select * from tblSearchTerms");
MessageBox.Show(dt.ToStrin
ASKER
i want to cycle through each record pulling certain data out of the fields and then call some other functions
ASKER
ok found it
foreach (DataRow dr in dt.Rows)
{
MessageBox.Show(dr["Search TermStart" ].ToString ());
}
foreach (DataRow dr in dt.Rows)
{
MessageBox.Show(dr["Search
}
ASKER
thanks for spending all the time that you did to get me up an running
garry
garry
Open in new window
you could also, instead, fill a dataset from the command (via a sqldataadapter), that would help to close the connection quickly.
you could also, instead, fill an array/list/dictionary/,...
it really depends on what you need to do with the data from the reader ...