【发布时间】:2022-01-08 03:54:29
【问题描述】:
我的代码:
private void txtSearch_TextChanged(object sender, EventArgs e)
{
if (txtSearch.Text == "")
{
DGViewListItems.Rows.Clear();
populateTable();
}
else
{
if (byItemcode.Checked == true)
{
DGViewListItems.Rows.Clear();
using (SqlConnection con = db.Connect())
{
try
{
//these Messageboxes is just for testing. to test if the data is correct
MessageBox.Show('%' + STEntry.whseFr.Text.Trim() + '%');
MessageBox.Show('%' + txtSearch.Text.Trim() + '%');
SqlDataReader rd;
SqlCommand cmd = new SqlCommand("sp_WhseItemsList", db.Connect());
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@Action", "I");
switch (activeform.formname)
{
case "Issuance List":
//cmd.Parameters.AddWithValue("@WHSE", STEntry.whseFr.Text);
break;
case "Stocks Transfer List":
cmd.Parameters.AddWithValue("@WHSE", STEntry.whseFr.Text.Trim());
break;
case "Stocks Adjustment List":
cmd.Parameters.AddWithValue("@WHSE", SADJEntry.txtWhse.Text.Trim());
break;
}
cmd.Parameters.AddWithValue("@Desc", "");
cmd.Parameters.AddWithValue("@Itemcode", '%' + txtSearch.Text.Trim() + '%');
rd = cmd.ExecuteReader();
int i = 0;
if (rd.HasRows)
{
while (rd.Read())
{
DGViewListItems.Rows.Add();
DGViewListItems.Rows[i].Cells["itemcode"].Value = rd["itemcode"].ToString();
DGViewListItems.Rows[i].Cells["whsecode"].Value = rd["whsecode"].ToString();
DGViewListItems.Rows[i].Cells["description"].Value = rd["description"].ToString();
DGViewListItems.Rows[i].Cells["uom"].Value = rd["uom"].ToString();
DGViewListItems.Rows[i].Cells["quantity"].Value = rd["quantity"].ToString();
i++;
}
}
}
catch (Exception ex)
{
}
}
}
else if (byDescription.Checked == true)
{
}
}
}
这对我不起作用,因为它没有正确填充 dgv。我不认为查询是存储过程内部的问题,因为我手动尝试了查询并且它工作正常
我尝试的查询:
SELECT DISTINCT A.*, B.description, B.uom
FROM inventoryTable A
LEFT OUTER JOIN Items B
ON A.itemcode = B.itemcode WHERE (A.whsecode = 'WHSE1' AND A.itemcode LIKE '%S%');
输出:
这是 textchanged 事件中代码的输出:
这是存储过程内容供参考:
ALTER PROCEDURE [dbo].[sp_WhseItemsList]
@Action char(5) = '',
@WHSE char(15) = '',
@Desc varchar(50) = '',
@Itemcode char(15) = ''
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
IF @Action = 'A'
BEGIN
SELECT DISTINCT A.*, B.description, B.uom
FROM inventoryTable A
LEFT OUTER JOIN Items B
ON A.itemcode = B.itemcode WHERE A.whsecode = @WHSE;
END
IF @Action = 'I'
BEGIN
SELECT DISTINCT A.*, B.description, B.uom
FROM inventoryTable A
LEFT OUTER JOIN Items B
ON A.itemcode = B.itemcode WHERE (A.whsecode = @WHSE) AND (A.itemcode LIKE @Itemcode);
END
IF @Action = 'D'
BEGIN
SELECT DISTINCT A.*, B.description, B.uom
FROM inventoryTable A
LEFT OUTER JOIN Items B
ON A.itemcode = B.itemcode WHERE (A.whsecode = @WHSE) AND (B.description LIKE @Desc);
END
END
【问题讨论】:
标签: c# sql select stored-procedures datagridview