У меня есть две таблицы, один для клиента, а другой для поставщика. Я хочу извлечь данные из обеих таблиц, в зависимости от значения, вставленного в поле поиска. /> Ниже приведены мои коды для C# и SQL, и я также включил из-за идеи о том, что я делаю.
Код: Выделить всё
try
{
using(SqlConnection con = new SqlConnection(connectionString))
{
con.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "SearchCustSupplier";
if(Main_Form.TransactionType == "Purchase")
{
cmd.Parameters.AddWithValue("@type", "Supplier");
}
else
{
cmd.Parameters.AddWithValue("@type", "Customer");
}
cmd.Parameters.AddWithValue("@search" , "%" +txtSearchSC.Text+ "%");
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
if(dt.Rows.Count > 0)
{
DataRow dr = dt.Rows[0];
//cmd.Parameters.AddWithValue("@Customer_ID", Cust_ID);
txtSCName.Text = dr["CustName"].ToString();
txtEmail.Text = dr["CustEmail"].ToString();
txtPhone.Text = dr["CustPhone"].ToString();
txtMobile.Text = dr["CustMobile"].ToString();
txtAddress.Text = dr["CustAddress"].ToString();
//cmd.Parameters.AddWithValue("@Supplier_ID", Supp_ID);
txtSCName.Text = dr["Supp_Name"].ToString();
txtEmail.Text = dr["Supp_Email"].ToString();
txtPhone.Text = dr["Supp_Phone"].ToString();
txtMobile.Text = dr["Supp_Mobile"].ToString();
txtAddress.Text = dr["Supp_Address"].ToString();
}
}
}
catch (Exception ex)
{
MessageBox.Show("Customer/Supplier Not Found, Something Went Wrong", "Warning" , MessageBoxButtons.OK, MessageBoxIcon.Error);
}
< /code>
Вот код для хранимой процедуры: < /p>
Create Or Alter Procedure SearchCustSupplier
(@search varchar (300),
@type varchar (200)
--@Customer_ID Int,
--@Supplier_ID Int
)
As
Begin
Select
C.Cust_ID, C.CustName, C.CustEmail, C.CustPhone,
C.CustMobile, C.CustAddress,
S.Supp_ID, S.Supp_Name, S.Supp_Email,
S.Supp_Phone, S.Supp_Mobile, S.Supp_Address
From
Customer As C
Inner Join
Supplier As S ON C.Cust_ID = S.Supp_ID
Where
C.CustName Like @search
Or S.Supp_Name Like @search
End
Подробнее здесь: https://stackoverflow.com/questions/734 ... sql-tables