RPG 与 C# 和 SQL 的比较

发布于 2024-08-28 23:24:42 字数 628 浏览 6 评论 0原文

在 RPG 程序(AS/400 上的 IBM 语言之一)中,我可以“链接”到一个文件以查看文件中是否存在记录(例如,某个客户记录)。如果是这样,那么我可以立即用新数据更新该记录。如果记录不存在,我可以写一条新记录。代码看起来像这样:

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

由于不太熟悉 SQL/C#,我想知道是否有一种方法可以从文件中进行随机检索(这就是 RPG 中“链”的作用)。基本上我想看看是否存在记录。如果是,请使用一些新信息更新记录。如果没有,那么我想写一个新的记录。我确信这是可能的,但不太确定如何去做。任何建议将不胜感激。

In an RPG program (One of IBM's languages on the AS/400) I can "chain" out to a file to see if a record (say, a certain customer record) exists in the file. If it does, then I can update that record instantly with new data. If the record doesn't exist, I can write a new record. The code would 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 real familiar with SQL/C#, I'm wondering if there is a way to do a random retrieval from a file (which is what "chain" does in RPG). Basically I want to see if a record exists. If it does, update the record with some new information. If it does not, then I want to write a new record. I'm sure it's possible, but not quite sure how to go about doing it. Any advice would be greatly appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

绮烟 2024-09-04 23:24:42

RPG 具有与系统上的数据库表的内在连接。这使得编写这样简洁的操作变得很容易。

另一方面,C# 要求您实现自己的数据库例程(或使用 LINQ 等框架)。

如果我这样做,我将创建一个负责使用 System.OLEDB 对象进行数据库操作的类。

有些方法可能是(一般想法,而不是实际代码):

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
}

然后你的主要方法世界读起来像

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

RPG has intrinsic connectivity to the database tables on the system. That makes it easy to write such succinct operations.

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

If I was doing this, I would create a class responsible for database manipulation using the 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 read something like

If (CheckExists("CustTable", "CustId", CustID)) {
    UpdateCredit(CustID, 10000)
} else {
    CreateCredit(CustId, 10000)
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文