Comparing RPG with C # and SQL

In an RPG program (one of the IBM languages ​​on the AS / 400), I can "connect" to a file to see if an entry (say, a specific client entry) exists in the file. If so, then I can update this record instantly with new data. If the entry does not exist, I can write a new entry. The code will look like this:

Customer  Chain CustFile   71 ;turn on indicator 71 if not found
          if    *in71         ;if 71 is "on"
          eval  CustID = Customer;
          eval  CustCredit = 10000;
          write CustRecord
          else                ;71 not on, record found.
          CustCredit = 10000;
          update CustRecord
          endif

      

Not being familiar with SQL / C #, I'm wondering if there is a way to do a random search from a file (which is what "chaining" is in RPG). Basically, I want to see if the entry exists. If this happens, update the record with the new information. If not, I want to write a new entry. I'm sure this is possible, but not really sure how. Any advice would be greatly appreciated.

+2


a source to share


1 answer


RPG has built-in ability to connect to database tables on the system. This makes it easier to record such concise operations.

C #, on the other hand, requires you to implement your own database routines (or use a framework like LINQ).

If I were to do this, I would create a class responsible for manipulating the database using System.OLEDB objects.

Some methods might be (general idea, not actual code):



public boolean CheckExists(string TableName, string ColumnName, int ID) {
  //Connect to database
  // Create Command with query  "SELECT COUNT(1) FROM " + TableName.Replace(";","") + " WHERE " + ColumnName.Replace(";","") + " = " + ID 

  Return int.Parse(myQuery.ExecuteScalar) > 0

 //disconnect

}


 public boolean UpdateCredit(int CustID, int newCredit) {
  //Connect to database
  // Create Command with query  "UPDATE CustTable SET CustCredit = " + newCredit.ToString() + " WHERE = CustId = " + CustID 

  myQuery.ExecuteNonQuery

//disconnect

}

public boolean CreateCredit(int CustID, int newCredit) {
  //Connect to database
  // Create Command with query  "INSERT INTO CustTable (CustID, CustCredit) VALUES (" + CustId.ToString + ", " + newCredit.ToString + ")"

  myQuery.ExecuteNonQuery

  //disconnect
}

      

Then your main method world reads something like

If (CheckExists("CustTable", "CustId", CustID)) {
    UpdateCredit(CustID, 10000)
} else {
    CreateCredit(CustId, 10000)
}

      

+5


a source







All Articles