根据条件将一个表复制到另一个表
在下面的程序中,我将从 MBRHISTDETL 选择的每条记录写入数据表。但是,每当从 MBRHISTDTL 文件读取记录时,我想在将其写入数据表之前执行更多操作。但是,程序的编写方式是,当我只想写入特定记录时,它会将整个结果集写入数据表。我怎样才能改变这个?我想做的是:
- 从 MBRHISTDETL 中选择一条记录
- ,它是 BILLTYPE 09 并且是 BILLMOYR <> 9999?如果是,请继续。如果没有,请获取另一条记录。
- 使用 MBRHISTDETL 中的 MBRNUM 从 LOCINFODETL 中获取 LOCATION、DISTRICT 和 CYCLE。
- DISTRICT 和 CYCLE 是否与输入参数 cbDistrict 和 cbCycle 匹配?如果没有,则获取另一条记录。
- 填充数据表
- 除非文件结尾,否则返回到 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:
- Select a record from MBRHISTDETL
- Is it BILLTYPE 09 and is BILLMOYR <> 9999? If yes, continue. If not, get another record.
- Use MBRNUM from MBRHISTDETL to get LOCATION, DISTRICT, and CYCLE from LOCINFODETL.
- Does DISTRICT and CYCLE match the input parameters cbDistrict and cbCycle? If no, get another record.
- Populate datatable
- 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果 MBRNUM 是 LOCINFODETL 中的外键,那么您只需要 MBRHISDETL 中所需的 MRBNUM。尝试子选择:
If MBRNUM is a foreign key in LOCINFODETL then you only need the required MRBNUM from MBRHISDETL. Try a subselect:
如果这些表位于同一数据库中,您可能(取决于数据库后端和配置)可以使用内部联接在单个查询中获取所有数据。例如:
这会处理您的步骤 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:
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.
我假设数据库表位于同一个数据库中。我将在查询语句中使用联合将两个表链接到一个公共键。关于如何建立关系的例子有很多。
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.