Highlighted
Absent Member.
Absent Member.
1167 views

.Net Cobol SqlDataReader

Jump to solution

This is just sample code, but I wanted to know what would be the equivalent .net cobol to the while(dr.read) and populate to a list.

 

public class Student
        {
            public int ID { get; set; }
            public string Name { get; set; }
            public DateTime DateOfBirth { get; set; }

        }
after that
            List<Student> student = new List<Student>();
            SqlConnection conn = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=D:\Prac\TestAssembly\ForumsProjectCSharp\App_Data\Studetnt.mdf;Integrated Security=True;User Instance=True");
            SqlCommand cmd = new SqlCommand("select * from Student", conn);
            SqlDataReader dr;
            try
            {
                conn.Open();
                dr = cmd.ExecuteReader();
                while (dr.Read())
                {
                    student.Add(new Student()
                    {
                        ID = dr.GetInt32(dr.GetOrdinal("ID")),
                        Name = dr.GetString(dr.GetOrdinal("Name")),
                        DateOfBirth = dr.GetDateTime(dr.GetOrdinal("DateOfBirth"))
                    });
                    
                }
                dr.Close();
            }
            catch (Exception exp)
            {

                throw;
            }
0 Likes
1 Solution

Accepted Solutions
Highlighted
Absent Member.
Absent Member.

RE: .Net Cobol SqlDataReader

Jump to solution
I am coming from a C#/VB.Net and old Cobol program language user. I am teaching myself .net cobol. What do you mean by cobol.net portable list syntax?

View solution in original post

0 Likes
5 Replies
Highlighted
Micro Focus Expert
Micro Focus Expert

RE: .Net Cobol SqlDataReader

Jump to solution

Untested, off the top of my head:

perform until not dr::Read
    declare aStudent = new Student
    set aStudent::ID to dr::GetInt32(dr::GetOrdinal("ID"))
    set aStudent::Name to dr::GetString(dr::GetOrdinal("Name"))
    set aStudent::DateOfBirth to dr::GetDateTime(dr::GetOrdinal("DateOfBirth"))
    invoke student::Add(aStudent)
end-perform

That's not how I'd actually implement this method (I'd use the COBOL.NET portable list syntax and I'd refactor the GetOrdinal invocations out of the loop), but it ought to work.

0 Likes
Highlighted
Absent Member.
Absent Member.

RE: .Net Cobol SqlDataReader

Jump to solution
I am coming from a C#/VB.Net and old Cobol program language user. I am teaching myself .net cobol. What do you mean by cobol.net portable list syntax?

View solution in original post

0 Likes
Highlighted
Absent Member.
Absent Member.

RE: .Net Cobol SqlDataReader

Jump to solution
I'd stay with something a little more tradition for the data access with the intention of being able to reuse the code with minimal modification in 10-50 years time (by which time we may well have had another alphabet soup of APIs and everyone else will have had to rewrite their code a few times), or right now with DB2, Oracle, SQL Server or PostgreSQL on Windows, .NET or Linux using ODBC, ADO.NET or JDBC, so something like this (I used .NET types for the data, the code will work with either .NET, JVM or traditional COBOL types, it just depends on how portable you want to be).

$set sql
working-storage section.
exec sql include sqlca end-exec.
01 Ident binary-long.
01 Name string.
01 DateOfBirth type System.DateTime.
procedure division.
exec sql whenever sqlerror go to sql-error end-exec
exec sql connect to studentDB end-exec
exec sql
declare c cursor for select * from Student
end-exec
exec sql open c end-exec
perform until exit
exec sql
fetch c into :Ident, :Name, :DateOfBirth
end-exec
if sqlcode = 100 exit perform end-if
*> process the student ...
end-perform
exec sql close c end-exec
goback.

sql-error.
display "SQL Error " sqlerrmc
goback.
0 Likes
Highlighted
Absent Member.
Absent Member.

RE: .Net Cobol SqlDataReader

Jump to solution
Thanks. I ended up with this which worked perfectly.

perform using conn as type MySqlConnection = new MySqlConnection(gm::GetConnection)
invoke conn:: Open()
perform using cmd as type MySqlCommand = new MySqlCommand("UserRights", conn)
set cmd::CommandType to type CommandType::StoredProcedure
perform using dr as type MySqlDataReader = cmd::ExecuteReader
perform until not dr::Read
set trObj::_userrights = dr['Rights']
invoke userList::Add(trObj)
end-perform
end-perform
end-perform
end-perform
0 Likes
Highlighted
Micro Focus Expert
Micro Focus Expert

RE: .Net Cobol SqlDataReader

Jump to solution

[quote user="aadowns2"]I am coming from a C#/VB.Net and old Cobol program language user. I am teaching myself .net cobol. What do you mean by cobol.net portable list syntax?[/quote]

For a while now (I don't remember which release introduced this), COBOL.NET has supported this syntax for lists:

define student as list[Student]
create student
write student from aStudent

This is perhaps a bit more self-documenting, and it's portable to COBOL for JVM as well. Some people like it; some don't care for it.

There's similar support for dictionaries. You can find more information here:
http://documentation.microfocus.com:8080/help/topic/com.microfocus.eclipse.infocenter.enterprisedeveloper.eclipsewin/GUID-5ACBB846-3B81-45EE-B6B5-45E103397C04.html

See in particular the topic comparing COBOL.NET with other .NET languages and the reference topics for the create, read, and write statements. I think there may also be samples of the portable syntax available in the Samples Browser.

[Edited to fix horrible styling imposed by the new site stylesheets. Why can't we provide a stylesheet and editor geared toward entering source code? You know, the thing we're generally talking about here?]

0 Likes
The opinions expressed above are the personal opinions of the authors, not of Micro Focus. By using this site, you accept the Terms of Use and Rules of Participation. Certain versions of content ("Material") accessible here may contain branding from Hewlett-Packard Company (now HP Inc.) and Hewlett Packard Enterprise Company. As of September 1, 2017, the Material is now offered by Micro Focus, a separately owned and operated company. Any reference to the HP and Hewlett Packard Enterprise/HPE marks is historical in nature, and the HP and Hewlett Packard Enterprise/HPE marks are the property of their respective owners.