我有一个Web应用程序,用户可以在其中查看某些测试数据。我有.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; } }
}
从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"];
}
我坚持的部分是,一旦从Test_Module获取数据,我需要使用该值从Items表中再次找到Item_Number。
例如: 组件1返回1284567899所以我需要在数据库中搜索该值。但是,我必须使用 - 在数据库中分离该值;我会在Items表中搜索1284-567-899,那么如何编写命令以便将组件1,2和3中的值分开?它应该在读取第一个(4个字符) - (3个字符) - (3个字符)后拆分值?
最后,它必须将这些值写入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>
答案 0 :(得分:0)
在你的第一个查询中再做一个像你一样的SqlCommand,但是使用另一个&#34; SqlText&#34;查询。
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);
}