如何从2表中选择并使用mysqldatareader获取它们的值 [英] How to SELECT from 2 table and get their value using mysqldatareader

查看:249
本文介绍了如何从2表中选择并使用mysqldatareader获取它们的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

上午好女士和先生。我想从表1中选择原因并从table2中选择地址,其中unique_id = @uniqueid。两个表都有名为unique_id的主键。并使用DataReader获取它们的价值。



我尝试过:



Good day Ma'am and Sir. I want to select cause from table 1 and select address from table2 where unique_id = @uniqueid. Both tables have primary key called unique_id. and get the their value using DataReader.

What I have tried:

using(MySqlConnection con = new MySqlConnection(connString))
           {
               consp.Open();
               string clientid = "009";
               MySqlDataReader reader;
               MySqlCommand com = new MySqlCommand("SELECT FROM ",con);
               com.Parameters.AddWithValue("@uniqueid,"clientid);
               reader = com.ExecuteReader();
               while(reader.HasRows && reader.Read())
               {
                 string cause;
                 string address;
                 cause = reader.GetString(reader.GetOrdinal());
                 address = reader.GetString(reader.GetOrdinal());
               }

           }

推荐答案

为什么不使用JOIN语句?

注意:我没有测试过这个查询,打算将其发布在评论上但不起作用。

Why not use a JOIN statement?
Note: I had not tested this query, intended to post it on comment but not working.
MySqlCommand com = new MySqlCommand("SELECT clause, address FROM table1 JOIN table2 ON table1.unique_id = table2.unique_id WHERE table1.unique_id = @uniqueid",con);


这篇关于如何从2表中选择并使用mysqldatareader获取它们的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆