sydneyguy
asked on
using odbc datareader and calling a function and returning the datareader results c#
i am wishing to split my code so that i call the function that opens the OdbcDataReader and then returns the results of the found data. i have shown the code that is running in the fist module
where all the code exists on one function,
private void build_Accounts_Click(objec t sender, EventArgs e)
what i want to do is to split this function into two
1)
the main start and calling function
private void build_Accounts_Click(objec t sender, EventArgs e)
2)
the reader and with the returning results
private static SqlDataReader AccessDB(string sql, string ODBCDB)
i am not sure how to structure the header for the
private static ************ AccessDB(string sql, string ODBCDB)
and the return of the datareader results
thanks for any help you can give me
****************** working code ************************** ***
private void build_Accounts_Click(objec t sender, EventArgs e)
{
int setrogo = testforminput();
if (setrogo == 0)
{
}
else
{
string BusinessType = BusinssType.Text;
int recnum = 0;
string LeadName = "";
OdbcConnection DbConnection = new OdbcConnection("DSN=newvti ger22-11v2 ");
// Now open that OdbcConnection object:
DbConnection.Open();
// Now using the OdbcConnection, we will create an OdbcCommand:
OdbcCommand DbCommand = DbConnection.CreateCommand ();
// Using that command, we can issue a Query and create an OdbcDataReader:
DbCommand.CommandText = "SELECT account_no, accountid, accountname FROM vtiger_account" ;
OdbcDataReader DbReader = DbCommand.ExecuteReader();
// Now we have an OdbcDataReader, we can find the number and names of the columns in the result set, and display this on the console.
OdbcDataReader DbReader = AccessDB("SELECT account_no, accountid, accountname FROM vtiger_account", "newvtiger22-11v2");
int fCount = DbReader.FieldCount;
Console.Write(":");
for (int i = 0; i < fCount; i++)
{
String fName = DbReader.GetName(i);
// String wName = DbReader.GetString(i);
Console.Write(fName + ":");
}
Console.WriteLine();
//# Now we can read each row from the result set, and read each column in that row and display its value.
string[] recinfo; // ARRAY TO HOLD THE REC INFO FOR POSTING OFF
while (DbReader.Read())
{
String col = "";
Console.Write(":");
// THIS GOES THROUGH THE BITS THAT ARE IN THE ARRAY
recinfo = new string[fCount]; // numbers is a fCount-element array
for (int i = 0; i < fCount; i++)
{
Console.Write(col + ":");
}
}
}
MessageBox.Show("Process Finished");
}
************************** ********** ********** ********** ****
************* new code that i wish to split *********************
private void build_Accounts_Click(objec t sender, EventArgs e)
{
int setrogo = testforminput();
if (setrogo == 0)
{
}
else
{
string BusinessType = BusinssType.Text;
int recnum = 0;
string LeadName = "";
OdbcDataReader DbReader = AccessDB("SELECT account_no, accountid, accountname FROM vtiger_account", "newvtiger22-11v2");
int fCount = DbReader.FieldCount;
Console.Write(":");
for (int i = 0; i < fCount; i++)
{
String fName = DbReader.GetName(i);
// String wName = DbReader.GetString(i);
Console.Write(fName + ":");
}
Console.WriteLine();
}
}
catch (Exception ex)
{
// MessageBox.Show("cannot access and up date contactlinkedin");
// IF NO VALUE DO NOTHING AND LET IT GLOSS OVER
}
Console.Write(col + ":");
}
private static SqlDataReader AccessDB(string sql, string ODBCDB)
{
OdbcConnection DbConnection = new OdbcConnection("DSN=newvti ger22-11v2 ");
// Now open that OdbcConnection object:
DbConnection.Open();
// Now using the OdbcConnection, we will create an OdbcCommand:
OdbcCommand DbCommand = DbConnection.CreateCommand ();
// Using that command, we can issue a Query and create an OdbcDataReader:
DbCommand.CommandText = "SELECT account_no, accountid, accountname FROM vtiger_account"; //WHERE ((vtiger_account.prosch)=' " + BusinessType + "') and ((contactlinkedin.done)=0) ";
OdbcDataReader DbReader = DbCommand.ExecuteReader();
return DbReader;
}
where all the code exists on one function,
private void build_Accounts_Click(objec
what i want to do is to split this function into two
1)
the main start and calling function
private void build_Accounts_Click(objec
2)
the reader and with the returning results
private static SqlDataReader AccessDB(string sql, string ODBCDB)
i am not sure how to structure the header for the
private static ************ AccessDB(string sql, string ODBCDB)
and the return of the datareader results
thanks for any help you can give me
****************** working code **************************
private void build_Accounts_Click(objec
{
int setrogo = testforminput();
if (setrogo == 0)
{
}
else
{
string BusinessType = BusinssType.Text;
int recnum = 0;
string LeadName = "";
OdbcConnection DbConnection = new OdbcConnection("DSN=newvti
// Now open that OdbcConnection object:
DbConnection.Open();
// Now using the OdbcConnection, we will create an OdbcCommand:
OdbcCommand DbCommand = DbConnection.CreateCommand
// Using that command, we can issue a Query and create an OdbcDataReader:
DbCommand.CommandText = "SELECT account_no, accountid, accountname FROM vtiger_account" ;
OdbcDataReader DbReader = DbCommand.ExecuteReader();
// Now we have an OdbcDataReader, we can find the number and names of the columns in the result set, and display this on the console.
OdbcDataReader DbReader = AccessDB("SELECT account_no, accountid, accountname FROM vtiger_account", "newvtiger22-11v2");
int fCount = DbReader.FieldCount;
Console.Write(":");
for (int i = 0; i < fCount; i++)
{
String fName = DbReader.GetName(i);
// String wName = DbReader.GetString(i);
Console.Write(fName + ":");
}
Console.WriteLine();
//# Now we can read each row from the result set, and read each column in that row and display its value.
string[] recinfo; // ARRAY TO HOLD THE REC INFO FOR POSTING OFF
while (DbReader.Read())
{
String col = "";
Console.Write(":");
// THIS GOES THROUGH THE BITS THAT ARE IN THE ARRAY
recinfo = new string[fCount]; // numbers is a fCount-element array
for (int i = 0; i < fCount; i++)
{
Console.Write(col + ":");
}
}
}
MessageBox.Show("Process Finished");
}
**************************
************* new code that i wish to split *********************
private void build_Accounts_Click(objec
{
int setrogo = testforminput();
if (setrogo == 0)
{
}
else
{
string BusinessType = BusinssType.Text;
int recnum = 0;
string LeadName = "";
OdbcDataReader DbReader = AccessDB("SELECT account_no, accountid, accountname FROM vtiger_account", "newvtiger22-11v2");
int fCount = DbReader.FieldCount;
Console.Write(":");
for (int i = 0; i < fCount; i++)
{
String fName = DbReader.GetName(i);
// String wName = DbReader.GetString(i);
Console.Write(fName + ":");
}
Console.WriteLine();
}
}
catch (Exception ex)
{
// MessageBox.Show("cannot access and up date contactlinkedin");
// IF NO VALUE DO NOTHING AND LET IT GLOSS OVER
}
Console.Write(col + ":");
}
private static SqlDataReader AccessDB(string sql, string ODBCDB)
{
OdbcConnection DbConnection = new OdbcConnection("DSN=newvti
// Now open that OdbcConnection object:
DbConnection.Open();
// Now using the OdbcConnection, we will create an OdbcCommand:
OdbcCommand DbCommand = DbConnection.CreateCommand
// Using that command, we can issue a Query and create an OdbcDataReader:
DbCommand.CommandText = "SELECT account_no, accountid, accountname FROM vtiger_account"; //WHERE ((vtiger_account.prosch)='
OdbcDataReader DbReader = DbCommand.ExecuteReader();
return DbReader;
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
accessdb was a poor choice of terms as it is used on mysql but thats fine
works like it should thanks for the extra eyes
have a good christmas
garry