C# 在存储过程中使用内部联接时,在c中显示来自多个表的datagrid数据
我正在用C编写代码,希望从多个具有关系的表中选择数据网格中的数据。在这里,我有一个客户机&Item_配置作为父表,Item_顺序作为子表,它有一个指向客户机和Item_配置表的外键。我只想从所有三个表中获取数据并显示在datagrid上 我的存储过程是:C# 在存储过程中使用内部联接时,在c中显示来自多个表的datagrid数据,c#,sql-server,join,stored-procedures,datagridview,C#,Sql Server,Join,Stored Procedures,Datagridview,我正在用C编写代码,希望从多个具有关系的表中选择数据网格中的数据。在这里,我有一个客户机&Item_配置作为父表,Item_顺序作为子表,它有一个指向客户机和Item_配置表的外键。我只想从所有三个表中获取数据并显示在datagrid上 我的存储过程是: ALTER PROC [dbo].[Full_SP] @clientName varchar(50) = null, @itemName varchar(50) = null, @clientId_FK varchar(
ALTER PROC [dbo].[Full_SP]
@clientName varchar(50) = null,
@itemName varchar(50) = null,
@clientId_FK varchar(50) = null,
@operation int
AS
BEGIN
SET NOCOUNT ON;
IF @operation = 2
BEGIN
SELECT
Client.clientName, Item_Configuration.itemName,
Item_Order.orderId, Item_Order.orderDate,
Item_Order.Quantity, Item_Order.status, Item_Order.totalPrice
FROM
Item_Order
INNER JOIN
Client ON Item_Order.clientId_FK = Client.clientId
JOIN
Item_Configuration ON Item_Order.itemId_FK = Item_Configuration.itemId
END
END
我对数据网格的搜索功能是C.i.e
private void btnSrchFull_Click(object sender, EventArgs e)
{
SqlConnection conn1 = new SqlConnection();
try
{
conn1.ConnectionString = "server=.\\ms2k5;database=Info_Connect;Trusted_Connection=true";
conn1.Open();
SqlCommand selectFull = new SqlCommand("Full_SP", conn1);
selectFull.CommandType = CommandType.StoredProcedure;
selectFull.Parameters.Add("@operation", SqlDbType.VarChar);
selectFull.Parameters["@operation"].Value = 2;
SqlDataReader myReader = selectFull.ExecuteReader();
List<FullFill> list = new List<FullFill>();
while (myReader.Read())
{
if (myReader.HasRows)
{
FullFill fullfill = new FullFill();
fullfill = MapFullfill(myReader, fullfill);
list.Add(fullfill);
}
}
myReader.NextResult();
foreach (FullFill ffll in list)
{
if (myReader.Read() && myReader.HasRows)
{
MapClint(myReader, ffll);
}
}
myReader.NextResult();
foreach (FullFill ffll1 in list)
{
if (myReader.Read() && myReader.HasRows)
{
MapItem(myReader, ffll1);
}
}
dataGridView1.DataSource = list;
double totPrice = 0;
for (int i = 0; i < dataGridView1.RowCount; i++)
{
totPrice = totPrice +
Convert.ToDouble(dataGridView1.Rows[i].Cells[5].Value);
totCost.Text = totPrice.ToString();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.StackTrace + MessageBoxIcon.Error);
}
finally
{
if (conn1.State != ConnectionState.Closed)
{
conn1.Close();
}
}
}
private FullFill MapItem(SqlDataReader myReader, FullFill itemName)
{
itemName.ItemName =myReader["itemName"].ToString();
return itemName;
}
private FullFill MapClient(SqlDataReader myReader, FullFill clientName)
{
clientName.ClientName = myReader["clientName"].ToString();
return clientName;
}
private FullFill MapFullfill(SqlDataReader myReader, FullFill fullfill)
{
fullfill.OrderNo = myReader["orderId"].ToString();
fullfill.OrderDate = Convert.ToDateTime(myReader["orderDate"]);
fullfill.Quantity = Convert.ToInt32(myReader["Quantity"]);
fullfill.Status = myReader["status"].ToString();
fullfill.TotalPrice = Convert.ToDouble(myReader["totalPrice"]);
return fullfill;
}
问题是,我只能从子表Item_中查找数据,而不是从父表中获取数据。也许在SQL中创建一个并运行存储过程更容易,选择它,您可以轻松地显示所有内容。我只是在函数MapFullFill上编辑一些代码。使用我在MapClint和MapItem中编写的代码,因为那里只有一个查询返回所有记录,因此不需要nextResult函数。但当我执行查询时……它会返回我想要的相同结果,但在使用c代码的应用程序中,它不会给出理想的结果,只显示来自Item_Orderchild表,既不来自Item_配置也不来自Clintparents表..您是否已尝试将查询中的tablename添加到代码中?例如:myReader[Clint.clintName]并将其添加到MapFulfill?如果这不起作用,我个人应该研究视图,在视图中创建一个包含所有内部联接值的表,等等,然后只需在该视图上进行选择,就可以将结果映射为MapFulfill
class FullFill
{
public string orderNo;
public string clientName;
public DateTime orderDate;
public string itemName;
public int quantity;
public double totCost;
public string status;
public string OrderNo
{
get { return orderNo; }
set { orderNo = value; }
}
public string ClientName
{
get { return clientName; }
set { clientName = value; }
}
public DateTime OrderDate
{
get { return orderDate; }
set { orderDate = value; }
}
public string ItemName
{
get { return itemName; }
set { itemName = value; }
}
public int Quantity
{
get { return quantity; }
set { quantity = value; }
}
public double TotalPrice
{
get { return totCost; }
set { totCost = value; }
}
public string Status
{
get { return status; }
set { status = value; }
}
}