Is there a Visual COBOL equivalent to the C# "Using" statement?
... as shown In the following C# example, the "Using" statement (not the "Using" directive)... (as in the statement that says "Using (OleDbConnection..." ?
static void Main()
// The connection string assumes that the Access
// Northwind.mdb is located in the c:\Data folder.
// string connectionString =
// "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="
// + "c:\\Data\\Northwind.mdb;User Id=admin;Password=;";
// Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\Austin1\Documents\Northwind.mdb
string connectionString =
+ "c:\\Users\\Austin1\\Documents\\Northwind.mdb;User Id=admin;Password=;";
// Provide the query string with a parameter placeholder.
string queryString =
"SELECT ProductID, UnitPrice, ProductName from products "
+ "WHERE UnitPrice > ? "
+ "ORDER BY UnitPrice DESC;";
// Specify the parameter value.
int paramValue = 10;
// Create and open the connection in a using block. This
// ensures that all resources will be closed and disposed
// when the code exits.
using (OleDbConnection connection =
// Create the Command and Parameter objects.
OleDbCommand command = new OleDbCommand(queryString, connection);
// Open the connection in a try/catch block.
// Create and execute the DataReader, writing the result
// set to the console window.
OleDbDataReader reader = command.ExecuteReader();
reader, reader, reader);
catch (Exception ex)
And would anyone like to convert all of the above to Visual COBOL and post the Visual COBOL equivalent? 🙂
The equivalent of the C# "Using" statement in Visual COBOL would be $set ilusing
$set ilusing "System.Data"
No, as I pointed out in the above, I am looking for the equivalent of the Using statement, not the Using directive.
Sorry about that!
As you already know, "using" has always been a reserved word in Cobol, and I read from MSDN here that "The using statement ensures that Dispose is called even if an exception occurs while you are calling methods on the object. You can achieve the same result by putting the object inside a try block and then calling Dispose in a finally block; in fact, this is how the using statement is translated by the compiler. "
In Visual COBOL, you will then need to use the Try...Finally construction.
Below is an example:
perform using cn as type SqlConnection = new SqlConnection(connectString)
set cmd to cn::CreateCommand()
set cmd::CommandText to "select * from dbo.evantb"
perform using rdr as type SqlDataReader = cmd::ExecuteReader()
@MF_Fano - OK, well, as to there being no exact equivalent to the C# "using" statement, that confirms my suspicion, and I guess I can see why (there is already a using reserved word in COBOL, used for another purpose).
Now on to the 2nd question at the end of my original post...
@evank - How do these performs exit their loops? I see no tests for exiting the loops.
So, there really IS an equivalent! I will work with the code you provided to prove it to myself. Thanks!