[英]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.