E
Earl
I want to retrieve a single row with only 2 columns, i.e, 2 cells. What is
the most efficient method?
the most efficient method?
Choose for a database that is able to do such short transactions fast.the most efficient method?
Earl comcast net> said:I want to retrieve a single row with only 2 columns, i.e, 2 cells. What is
the most efficient method?
Earl,
If performance is the key factor, consider a stored function that has the
two values as output parameters - and the requisite input parameters to do
the select.
Creating the DataReader and Data Adapter objects is substantial overkill,
especially if the fetched values are read only.
regards
roy fine
Miha Markic said:Hi,
Either adapter.Fill or using DataReader would do.
--
Miha Markic [MVP C#] - RightHand .NET consulting & development
miha at rthand com
www.rthand.com
Earl comcast net> said:I want to retrieve a single row with only 2 columns, i.e, 2 cells. What is
the most efficient method?
Roy Fine said:Earl
Here is a quick sample app that calls a stored procedure with one input
parameter and three output parameters - and there is no cursor involved on
the client side - i.e. not DataReader object will be constructed.
This sample is for Oracle (that's my domain these days) - and I see that
Miha Markic has provided a good example of same process using SqlServer.
here is the code - but first the disclaimer - there are no comments, there
is no exception handling, and the formatting of the code is a modification
from early Stroustrup C for the sake of screen/editor real estate
conservation. There is a lot to chew on here, but look especially at the
FetchScalars method of the DataManager class.
Here goes:
<rlfine code hack>
using System;
using System.Data;
using Oracle.DataAccess.Client;
using Oracle.DataAccess.Types;
/*
****************************************************************************
****
CREATE OR REPLACE PROCEDURE GET_SCALAR_VALUES(p_state IN VarChar2,
p_uniqZips OUT Number,
p_minZip OUT VarChar2,
p_maxZip OUT VarChar2) IS
CURSOR ABCD(c_state in VarChar2) IS
SELECT COUNT(*),min(zip_code),max(zip_code)FROM(
SELECT count(*),zip_code
FROM SMASTER.IMPORT_HOLDING
WHERE STATE=c_state
GROUP BY zip_code
);
BEGIN
OPEN ABCD(p_state);
FETCH ABCD INTO p_uniqZips,p_minZip,p_maxZip;
CLOSE ABCD;
END;
****************************************************************************
**** */
namespace ScalarCursorFetch{
//
// ---------------------------------------------
class Class1{
static void Main(string[] args){
using(DataManager dm = new DataManager("smaster","smaster")){
int uniqueCount;
string scanState = "NY";
string minZipcode;
string maxZipcode;
dm.FetchScalars(scanState,out uniqueCount, out minZipcode, out
maxZipcode);
Console.WriteLine("Scanning for state: {0}",scanState);
Console.WriteLine("Found {0} Unique zipcode",uniqueCount);
Console.WriteLine("Minimum zipcode in scan: {0}",minZipcode);
Console.WriteLine("Minimum zipcode in scan: {0}\n",maxZipcode);
}
}
}
//
// ---------------------------------------------
class DataManager:IDisposable{
private OracleConnection ocon;
private DataManager(){}
public void Dispose(){
Dispose(true);
}
void Dispose(bool explicitDispose){
ocon.Close();
if(explicitDispose) GC.SuppressFinalize(this);
}
~DataManager(){
Dispose(false);
}
public DataManager(string userid,string password){
ocon = new OracleConnection("user ID=SMASTER;Password=SMASTER;");
ocon.Open();
}
public bool FetchScalars(string p_state,out int p_uniqueCount,out string
p_minZipcode,out string p_maxZipcode){
string sqlCmd = "BEGIN GET_SCALAR_VALUES1,:2,:3,:4); END;";
p_uniqueCount = 0;
p_minZipcode = "";
p_maxZipcode = "";
using(OracleCommand ocmd = new OracleCommand(sqlCmd,ocon)){
OracleParameter oprm_state =
ocmd.Parameters.Add("state",OracleDbType.Varchar2,2,p_state,ParameterDirecti
on.Input);
OracleParameter oprm_ucnt =
ocmd.Parameters.Add("ucnt",OracleDbType.Int32,0,p_state,ParameterDirection.O
utput);
OracleParameter oprm_minz =
ocmd.Parameters.Add("minzip",OracleDbType.Varchar2,5,null,ParameterDirection
.Output);
OracleParameter oprm_maxz =
ocmd.Parameters.Add("maxzip",OracleDbType.Varchar2,5,null,ParameterDirection
.Output);
ocmd.ExecuteNonQuery();
p_uniqueCount = (int)oprm_ucnt.Value;
p_minZipcode = (string)(OracleString)oprm_minz.Value;
p_maxZipcode = (string)(OracleString)oprm_maxz.Value;
ocmd.Parameters.Clear();
}
return true;
}
}
}
</rlfine code hack>
toEarl comcast net> said:My question is also: How to get TWO output parameters?
Here is how this question came about: I needed a single value (SUM) from two
other tables, and I needed a date and amount value from the third table (the
one I referenced in my question). Because I needed set values for the first
two, and a single row for the third, a JOIN looked out of the question
(okay, maybe possible, but I didn't like what I was returning and didn't
want to fool around with expressions since I would rather do the math on the
server).
So for the two single summed values, I called ExecuteScalar on the command
object. Without using ADO classic, I'd like to do the same thing for the
single row with 2 values from one table (SQL Server). I'm now doing this
with a dataadapter and a datatable. Effective, but seems like overkill.
What