A question about accessing a database using ado.net

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
 
M

Morten Wennevik

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
 
T

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
 
M

Morten Wennevik

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top