根据条件将一个表复制到另一个表

发布于 2024-11-27 00:29:47 字数 1667 浏览 1 评论 0原文

在下面的程序中,我将从 MBRHISTDETL 选择的每条记录写入数据表。但是,每当从 MBRHISTDTL 文件读取记录时,我想在将其写入数据表之前执行更多操作。但是,程序的编写方式是,当我只想写入特定记录时,它会将整个结果集写入数据表。我怎样才能改变这个?我想做的是:

  1. 从 MBRHISTDETL 中选择一条记录
  2. ,它是 BILLTYPE 09 并且是 BILLMOYR <> 9999?如果是,请继续。如果没有,请获取另一条记录。
  3. 使用 MBRHISTDETL 中的 MBRNUM 从 LOCINFODETL 中获取 LOCATION、DISTRICT 和 CYCLE。
  4. DISTRICT 和 CYCLE 是否与输入参数 cbDistrict 和 cbCycle 匹配?如果没有,则获取另一条记录。
  5. 填充数据表
  6. 除非文件结尾,否则返回到 1。

由于我的代码现在已编写,所以我只能完成步骤 1 到 3,因为我还没有找到从 MBRHISTDETL 获取记录并使用该信息从 LOCINDODETL 选择另一条记录的方法,以验证我之前的记录是否正确写入数据表。基本上,我试图从两个不同的数据库文件获取信息并将信息写入数据表。我以正确的方式处理这件事吗?

到目前为止,这是我的代码:

private void btnGo_Click(object sender, EventArgs e)
    {
        //get parameters
        string cycle = cbCycle.Text;
        string district = cbDistrict.Text;

        //create a connection to the database
        OdbcConnection DbConnection = new OdbcConnection("DSN=UPN2;uid=xxxx;pwd=xxxx");
        DbConnection.Open();

        //create a command to extract the required data and
        //assign it to the connection string
        OdbcCommand DbCommand = DbConnection.CreateCommand();
        DbCommand.CommandText = "SELECT * FROM CAV_MBRHISTDETL WHERE BILLTYPE = '09' " +
            "AND BILLMOYR <> '9999'";

        //Create a DataAdapter to run the command and fill the datatable
        OdbcDataAdapter da = new OdbcDataAdapter();
        da.SelectCommand = DbCommand;
        DataTable dt = new DataTable();

        //Put results into datatable.
        da.Fill(dt);


        tbOutput.Text = PrintDataTable(dt);

        DbCommand.Dispose();
        DbConnection.Close();

    }

In my program below, I'm writing every record I select from MBRHISTDETL to a datatable. However, whenever a record is read from the MBRHISTDTL file, I'd like to perform more actions before writing it to the datatable. The way the program is written, though, it will write the whole resultset to the datatable when I only want particular records being written. How can I change this? What I'd like to do is this:

  1. Select a record from MBRHISTDETL
  2. Is it BILLTYPE 09 and is BILLMOYR <> 9999? If yes, continue. If not, get another record.
  3. Use MBRNUM from MBRHISTDETL to get LOCATION, DISTRICT, and CYCLE from LOCINFODETL.
  4. Does DISTRICT and CYCLE match the input parameters cbDistrict and cbCycle? If no, get another record.
  5. Populate datatable
  6. Go back to 1 unless end of file.

As my code is written right now, I can only complete steps 1 through 3 because I haven't figured out a way to grab a record from MBRHISTDETL and use that info to select another record from LOCINDODETL to verify the record I have is correct before writing to the datatable. Basically, I'm trying to get information from two different database files and write the information to a datatable. Am I going about this the right way?

Here's my code so far:

private void btnGo_Click(object sender, EventArgs e)
    {
        //get parameters
        string cycle = cbCycle.Text;
        string district = cbDistrict.Text;

        //create a connection to the database
        OdbcConnection DbConnection = new OdbcConnection("DSN=UPN2;uid=xxxx;pwd=xxxx");
        DbConnection.Open();

        //create a command to extract the required data and
        //assign it to the connection string
        OdbcCommand DbCommand = DbConnection.CreateCommand();
        DbCommand.CommandText = "SELECT * FROM CAV_MBRHISTDETL WHERE BILLTYPE = '09' " +
            "AND BILLMOYR <> '9999'";

        //Create a DataAdapter to run the command and fill the datatable
        OdbcDataAdapter da = new OdbcDataAdapter();
        da.SelectCommand = DbCommand;
        DataTable dt = new DataTable();

        //Put results into datatable.
        da.Fill(dt);


        tbOutput.Text = PrintDataTable(dt);

        DbCommand.Dispose();
        DbConnection.Close();

    }

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

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

发布评论

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

评论(3

甜是你 2024-12-04 00:29:47

如果 MBRNUM 是 LOCINFODETL 中的外键,那么您只需要 MBRHISDETL 中所需的 MRBNUM。尝试子选择:

SELECT MBRNUM, LOCATION, DISTRICT, CYCLE
from
    CAV_LOCINFODETL
WHERE
    MBRNUM IN (SELECT MBRNUM
               FROM CAV_MBRHISTDETL
               WHERE BILLTYPE = '09' AND BILLMOYR <> '9999')

If MBRNUM is a foreign key in LOCINFODETL then you only need the required MRBNUM from MBRHISDETL. Try a subselect:

SELECT MBRNUM, LOCATION, DISTRICT, CYCLE
from
    CAV_LOCINFODETL
WHERE
    MBRNUM IN (SELECT MBRNUM
               FROM CAV_MBRHISTDETL
               WHERE BILLTYPE = '09' AND BILLMOYR <> '9999')
身边 2024-12-04 00:29:47

如果这些表位于同一数据库中,您可能(取决于数据库后端和配置)可以使用内部联接在单个查询中获取所有数据。例如:

DbCommand.CommandText = 
 @"SELECT HIST.Field1, HIST.Field2, LOCINFO.Field3 FROM CAV_MBRHISTDEL AS HIST
   INNER JOIN LOCINFODETL AS LOCINFO ON HIST.MBRNUM = LOCINFO.MBRNUM 
   WHERE LOCINFO.CYCLE = @CYCLE AND
         LOCINFO.DISTRICT = @DISTRICT AND
         HIST.BILLTYPE = '09' AND
         HIST.BOLLMOYR <> '9999'";

DbCommand.Parameters.AddWithValue("@CYCLE", cycle);
DbCommand.Parameters.AddWithValue("@DISTRICT", district);

这会处理您的步骤 1、2、3、4 和 6,这些步骤都与选择您需要的数据有关。步骤 5 只需执行命令来填充数据表。 (注:Field1、Field2等替换为您数据表实际需要的字段,LOCINFO.MBRNUM替换为LOCINFO。(LOCINFODETL中哪个字段对应MBRNUM即可)。)

如果SQL查询语法不清楚,您可以想要引用内连接参数化查询表别名 了解有关语法的更多信息。

If the tables are in the same database, you likely (depending on the database backend and configuration) could use an inner join to get all your data in a single query. For instance:

DbCommand.CommandText = 
 @"SELECT HIST.Field1, HIST.Field2, LOCINFO.Field3 FROM CAV_MBRHISTDEL AS HIST
   INNER JOIN LOCINFODETL AS LOCINFO ON HIST.MBRNUM = LOCINFO.MBRNUM 
   WHERE LOCINFO.CYCLE = @CYCLE AND
         LOCINFO.DISTRICT = @DISTRICT AND
         HIST.BILLTYPE = '09' AND
         HIST.BOLLMOYR <> '9999'";

DbCommand.Parameters.AddWithValue("@CYCLE", cycle);
DbCommand.Parameters.AddWithValue("@DISTRICT", district);

This takes care of your steps 1, 2, 3, 4, and 6, which all relate to selecting the data you need. Step 5 simply requires executing the command to fill a data table. (Note: replace Field1, Field2, etc. with the actual fields you need for the your data table, and LOCINFO.MBRNUM with LOCINFO.(whichever field in LOCINFODETL corresponds to MBRNUM).)

If the SQL query syntax is unclear, you may want to reference inner joins, parameterized queries, and table aliasing for more on the syntax.

忆沫 2024-12-04 00:29:47

基本上,我试图从两个不同的数据库获取信息
文件并将信息写入数据表

我假设数据库表位于同一个数据库中。我将在查询语句中使用联合将两个表链接到一个公共键。关于如何建立关系的例子有很多。

Basically, I'm trying to get information from two different database
files and write the information to a datatable

I assume that the database tables are in the same database. I would use a union in your query statement to link the two tables to a common key. there are many examples on how to create the relationship.

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