A question about accessing a database using ado.net

  • Thread starter Thread starter tony
  • Start date Start date
T

tony

Hello!

This method below works but how can it work when I don't have any open on
myOleDbConnection ?
As I have understood this is that there must exist an myOleDbConnection
..Open(); in my case
but it works just as good with one as without one.


public ArrayList GetAllPostNames()
{
// here I get the connection from a singleton in a class library dll
OleDbConnection myOleDbConnection =
MeltPracStorage.instance.utcasMspDbConnection;
DataSet dataSet = new DataSet();
ArrayList arrayList = new ArrayList();

OleDbDataAdapter myOleDbAdapter = new OleDbDataAdapter("select * "
+
"from MSP_COMP t1 " +
"where ACTIVE = 1 and " +
"SAVE_TIME = " +
"(select max(SAVE_TIME) " +
"from MSP_COMP " +
"where COMP_NAME = t1.COMP_NAME)
order by COMP_NAME", myOleDbConnection);

myOleDbAdapter.Fill( dataSet );

//loop through the dataset and add each COMP_NAME result to the
ArrayList and return it
foreach( DataRow row in dataSet.Tables[0].Rows)
arrayList.Add( row["COMP_NAME"] );

return arrayList;
}

//Tony
 
Hi Tony,

DataAdapter.Fill works in two ways.

1) If there is an open connection, this connection is used, and the connection is left untouched
2) If no connection is found. Connection.Open is called, and Connection.Close is called after Fill



Hello!

This method below works but how can it work when I don't have any openon
myOleDbConnection ?
As I have understood this is that there must exist an myOleDbConnection
.Open(); in my case
but it works just as good with one as without one.


public ArrayList GetAllPostNames()
{
// here I get the connection from a singleton in a class library dll
OleDbConnection myOleDbConnection =
MeltPracStorage.instance.utcasMspDbConnection;
DataSet dataSet = new DataSet();
ArrayList arrayList = new ArrayList();

OleDbDataAdapter myOleDbAdapter = new OleDbDataAdapter("select * "
+
"from MSP_COMP t1 " +
"where ACTIVE = 1 and " +
"SAVE_TIME = " +
"(select max(SAVE_TIME) " +
"from MSP_COMP " +
"where COMP_NAME = t1.COMP_NAME)
order by COMP_NAME", myOleDbConnection);

myOleDbAdapter.Fill( dataSet );

//loop through the dataset and add each COMP_NAME result to the
ArrayList and return it
foreach( DataRow row in dataSet.Tables[0].Rows)
arrayList.Add( row["COMP_NAME"] );

return arrayList;
}

//Tony
 
Hello!!

Do you recomment that a close should be used in my case or can I just leave
it as my method below is now.

//Tony


"Morten Wennevik" <[email protected]> skrev i meddelandet
Hi Tony,

DataAdapter.Fill works in two ways.

1) If there is an open connection, this connection is used, and the
connection is left untouched
2) If no connection is found. Connection.Open is called, and
Connection.Close is called after Fill



Hello!

This method below works but how can it work when I don't have any open on
myOleDbConnection ?
As I have understood this is that there must exist an myOleDbConnection
.Open(); in my case
but it works just as good with one as without one.


public ArrayList GetAllPostNames()
{
// here I get the connection from a singleton in a class library dll
OleDbConnection myOleDbConnection =
MeltPracStorage.instance.utcasMspDbConnection;
DataSet dataSet = new DataSet();
ArrayList arrayList = new ArrayList();

OleDbDataAdapter myOleDbAdapter = new OleDbDataAdapter("select * "
+
"from MSP_COMP t1 " +
"where ACTIVE = 1 and " +
"SAVE_TIME = " +
"(select max(SAVE_TIME) " +
"from MSP_COMP " +
"where COMP_NAME = t1.COMP_NAME)
order by COMP_NAME", myOleDbConnection);

myOleDbAdapter.Fill( dataSet );

//loop through the dataset and add each COMP_NAME result to the
ArrayList and return it
foreach( DataRow row in dataSet.Tables[0].Rows)
arrayList.Add( row["COMP_NAME"] );

return arrayList;
}

//Tony
 
You can leave it as it is

Hello!!

Do you recomment that a close should be used in my case or can I just leave
it as my method below is now.

//Tony


"Morten Wennevik" <[email protected]> skrev i meddelandet
Hi Tony,

DataAdapter.Fill works in two ways.

1) If there is an open connection, this connection is used, and the
connection is left untouched
2) If no connection is found. Connection.Open is called, and
Connection.Close is called after Fill



Hello!

This method below works but how can it work when I don't have any open on
myOleDbConnection ?
As I have understood this is that there must exist an myOleDbConnection
.Open(); in my case
but it works just as good with one as without one.


public ArrayList GetAllPostNames()
{
// here I get the connection from a singleton in a class library dll
OleDbConnection myOleDbConnection =
MeltPracStorage.instance.utcasMspDbConnection;
DataSet dataSet = new DataSet();
ArrayList arrayList = new ArrayList();

OleDbDataAdapter myOleDbAdapter = new OleDbDataAdapter("select * "
+
"from MSP_COMP t1 " +
"where ACTIVE = 1 and " +
"SAVE_TIME = " +
"(select max(SAVE_TIME) " +
"from MSP_COMP " +
"where COMP_NAME = t1.COMP_NAME)
order by COMP_NAME", myOleDbConnection);

myOleDbAdapter.Fill( dataSet );

//loop through the dataset and add each COMP_NAME result to the
ArrayList and return it
foreach( DataRow row in dataSet.Tables[0].Rows)
arrayList.Add( row["COMP_NAME"] );

return arrayList;
}

//Tony
 
Back
Top