【发布时间】:2020-06-28 11:53:36
【问题描述】:
我想检查表A 中的一条记录以检查它是否存在于表B 中。
我计算表B中输入了多少数据。
例子:
A B
------------------------------
John John
Joon Jorge
Jorge Joon
Elizabet Elizabet
Suzan Suzan
Elizabet
Joon
Suzan
John
Elizabet
答案应该是这样的:
John = 2
Joon = 2
jorge = 1
Elizabet = 3
Suzan = 2
我尝试过的:
using (ImportInvoiceMasterForm.ApplicationSqlConnection2 = new SqlConnection(MainForm.ApplicationDataBase))
{
ImportInvoiceMasterForm.ApplicationSqlConnection2.Open();
using (ImportInvoiceMasterForm.ApplicationSqlCommand1 = new SqlCommand("SELECT * FROM TBL_Stock_Item", ImportInvoiceMasterForm.ApplicationSqlConnection2))
using (ImportInvoiceMasterForm.ApplicationSqlDataReader1 = ImportInvoiceMasterForm.ApplicationSqlCommand1.ExecuteReader())
{
while (ImportInvoiceMasterForm.ApplicationSqlDataReader1.Read())
{
ImportInvoiceMasterForm.GetImportQuantity = 0;
#region Get Import Quantity
using (ImportInvoiceMasterForm.ApplicationSqlConnection1 = new SqlConnection(MainForm.ApplicationDataBase))
{
ImportInvoiceMasterForm.ApplicationSqlConnection1.Open();
using (ImportInvoiceMasterForm.ApplicationSqlCommand = new SqlCommand("SELECT * FROM TBL_Import_Items WHERE ImportItemName='" + ImportInvoiceMasterForm.ApplicationSqlDataReader1.GetString(1) + "'", ImportInvoiceMasterForm.ApplicationSqlConnection1))
using (ImportInvoiceMasterForm.ApplicationSqlDataReader = ImportInvoiceMasterForm.ApplicationSqlCommand.ExecuteReader())
{
while (ImportInvoiceMasterForm.ApplicationSqlDataReader.Read())
{
GetImportQuantity += double.Parse(ImportInvoiceMasterForm.ApplicationSqlDataReader.GetValue(4).ToString());
}
}
}
#endregion Get Import Quantity
}
}
}
【问题讨论】:
-
你为什么不只在查询中这样做?我建议你使用存储过程并在那里编写逻辑