简体   繁体   English

如何使用多个表从数据库中查找值

[英]How to look up values from the database by using multiple tables

I have a web application where the user can view certain test data. 我有一个Web应用程序,用户可以在其中查看某些测试数据。 I have .cs page where it looks up the values in the sql database from table 1. 我有.cs页,它在表1中查找sql​​数据库中的值。

public class Test_Module : TestData
{
internal string m_fComponent1;
public string Component1 { get { return m_fComponent1; } set { m_fComponent1 = value; } }

internal string m_fComponent2;
public string Component2 { get { return m_fComponent2; } set { m_fComponent2 = value; } }

internal string m_fComponent3;
public string Component3 { get { return m_fComponent3; } set { m_fComponent3 = value; } }
}

From the TestId it finds the data related to that Item 从TestId中找到与该项目相关的数据

 public static List<Test_Module> getTestData(int nTestId)
{
    SqlCommand cmd = new SqlCommand("SELECT Item.Test_Id AS TestId, Test_Number, "
        + "Component1, Component2, Component3 FROM Items INNER JOIN Test_Module ON "
        + "Items.Test_Number = Test_Module.Item_Name "
        + "WHERE (Test_ID = @Item)");
    cmd.Parameters.Add("@Item", SqlDbType.Int).Value = nTestID;


    DataTable dt = InternalSalesDB.Instance.query(cmd).Tables[0];

    List<TestEE_Module> lstData = new List<TestEE_Module>();
    foreach (DataRow dr in dt.Rows)
    {
        TestEE_Module oData = new TestEE_Module();

        fillTest_Module(oData, dr);
        oData.Item = (string)dr["Test_Number"];
        lstData.Add(oData);
    }
    return lstData;
}

 internal static void fillTest_Module(Test_Module oData, DataRow dr)
 {
   oData.m_fComponent1 = (string)dr["Component1"];

   oData.m_fComponent2 = (string)dr["Component2"];

   oData.m_fComponent3 = (string)dr["Component3"];
 }

The part I'm stuck on is that once it gets the data from Test_Module, I need to use that value to find Item_Number again from Items table. 我坚持的部分是,一旦它从Test_Module获取数据,我就需要使用该值从Items表中再次找到Item_Number。

For example: Component 1 returns 1284567899 so I need to search that value in the database. 例如:组件1返回1284567899,因此我需要在数据库中搜索该值。 But, I have to separate that value using - so in the database; 但是,我必须使用-分隔数据库中的值; I would search for 1284-567-899 in Items table so how do I write the command so it splits up the values from Component 1,2 and 3? 我将在Items表中搜索1284-567-899,所以我该如何编写命令以使其拆分组件1,2和3中的值? It should split the values after reading the first (4 characters) - (3 characters) - (3 character)? 在读取第一个(4个字符)-(3个字符)-(3个字符)后,应该拆分值吗?

Lastly, it has to write those vales to the webpage of .aspx in Gridview 最后,它必须将这些值写入Gridview中的.aspx网页

 <asp:GridView ID="TestModule" runat="server" AutoGenerateColumns="False"     BackColor="White"
 BorderColor="#DEDFDE" BorderStyle="None" BorderWidth="1px" CellPadding="4"
        DataSourceID="dsrcGetTestData" Font-Size="0.65em" ForeColor="Black"   GridLines="Vertical" DataKeyNames="TestID">
        <FooterStyle BackColor="#CCCC99" />
        <Columns>
            <asp:CommandField ShowDeleteButton="True" />

            <asp:BoundField DataField="Item" HeaderText="Module" SortExpression="Item">  <ItemStyle Wrap="False" /> </asp:BoundField>             

             <asp:BoundField DataField="Component1"  SortExpression="Component1"   />    
             <asp:BoundField DataField="Component2"  SortExpression="Component2" />    
             <asp:BoundField DataField="Component3"  SortExpression="Component3"  />   
      </Columns>
        <RowStyle BackColor="#F7F7DE" />
        <SelectedRowStyle BackColor="#CE5D5A" Font-Bold="True" ForeColor="White" />
        <PagerStyle BackColor="#F7F7DE" ForeColor="Black" HorizontalAlign="Right"  />
        <HeaderStyle BackColor="#6B696B" Font-Bold="True" ForeColor="White" />
        <AlternatingRowStyle BackColor="White"  />
       </asp:GridView>     

Just do another SqlCommand like you're doing in your first query, but use a different "SqlText" query. 只需像在第一个查询中一样执行另一个SqlCommand,但是使用另一个“ SqlText”查询。

foreach (var testEEModule in lstData)
{
var itemNumber = testEEModule.Item;

var formattedItemNumber = FormatItemNumber(itemNumber); // new function to add your dashes in the appropriate spots

var sql = "Select Item_Number From Items Where Item_Number = '" + formattedItemNumber + "'";

var results = cmd.ExecuteScalar(sql);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将多个值输入具有组合表的数据库表中? - How do I input multiple values into database tables with composite tables? 如何使用 EF DbContext model 查找表 - How to model look-up tables with EF DbContext 如何通过使用SSIS将数据从多个表插入到多个表? - How to insert data from multiple tables to multiple tables by using SSIS? 如何在运算符之间构建多个整数键索引(快速查找对象)(val&gt; =&val &lt;=) - How to build multiple integer key index (fast look up object) for using between operator (val >= & val <=) 使用ViewModel使用多个表中的值加载页面 - Loading a page with values from multiple tables using a ViewModel DataGridView和来自多个表的值? - DataGridView and values from multiple tables? 如何从多个多对多数据库表中检索数据 - How to retrieve data from multiple Many-To-Many database tables 如何从连接的多个表中仅获取不同的值 - How to get only distinct values from joined multiple tables 如何从NHibernate的多个表中获取和保存单个联接值? - How to get and save individual joined values from multiple tables in NHibernate? 如何从数据库中添加十进制值 - How do i add up decimal values from a database
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM