如何从两个不同的表中获取数据 [英] How to fetch data from two different table

查看:68
本文介绍了如何从两个不同的表中获取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码在下面

{

my code is below
{

SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=F:\New_Project\App_Data\Database.mdf;Integrated Security=True;User Instance=True");
        SqlCommand cmd = new SqlCommand("select * from Purchase_Raw_Material where Item_Code = '" + txticode.Text + "' ", con);
        SqlCommand cmd1 = new SqlCommand("select * from Sales where Item_Code = '" + txticode.Text + "' ", con);
        con.Open();
        SqlDataReader DR1 = cmd.ExecuteReader();
        if (DR1.Read())
        {

            TextBox1.Text = DR1.GetValue(2).ToString();
            TextBox2.Text = DR1.GetValue(2).ToString();
            GridView1.DataBind();
            con.Close();
        }
        
        
        
    }

推荐答案

根据您的代码,为第二个表结果集创建另一个SqlDataReader。或者您也可以使用DataAdapter和DataSet。
Based on your code, Create another SqlDataReader for 2nd table resultset. Or you could use DataAdapter & DataSet too.


您可以编写单个查询以使用join从这两个不同的表中获取记录,并尝试执行该单个查询。
You can write single query for fetching records from these two different tables using join, and try to execute that single query.


选择Purchase_Raw_Material。*

来自Purchase_Raw_Material r内部联接销售

on Purchase_Raw_Material.Item_Code = Sales.Item_Code

其中Item_Code = '+ txticode.Text +'



你可以从任何表中选择那些列中的select语句,因为我选择了Purchase_Raw_Materialable中的所有列
select Purchase_Raw_Material.*
from Purchase_Raw_Material r inner join Sales
on Purchase_Raw_Material.Item_Code = Sales.Item_Code
where Item_Code = '" + txticode.Text + "'

you can select those column from any table with select statement from any table as i select all columns from Purchase_Raw_Materialable


这篇关于如何从两个不同的表中获取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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