C# 根据条件放弃SQL结果行
我有以下数据:C# 根据条件放弃SQL结果行,c#,sql-server-2008,logic,C#,Sql Server 2008,Logic,我有以下数据: Ord_Act_Deliv_DateTime TrmnlGrp_Key Ord_CustLoc_Key Ord_Driver_Key Ord_Inv_No Credits 2016-04-01 11:00:00 3 429 93 INV-00055295 True 2016-04-01 11:00:00 3 135
Ord_Act_Deliv_DateTime TrmnlGrp_Key Ord_CustLoc_Key Ord_Driver_Key Ord_Inv_No Credits
2016-04-01 11:00:00 3 429 93 INV-00055295 True
2016-04-01 11:00:00 3 135 93 INV-00055295A False
2016-04-01 11:00:00 3 429 93 INV-00055295C False
我想做的事情如下
if credits = true
set @temp_invoice_no = Ord_inv_No + 'C'
SELECT * FROM Orders WHERE Ord_Inv_No NOT IN (original inv no with credit, inv no + 'C')
在我选择的日期范围内,我可能有许多类似的记录,我需要确保原始记录和信用记录不会进入最终结果集。从我到目前为止的研究来看,这似乎是大多数简单比较的建议,我认为这不适合这种情况,或者如果使用在语句中传递或声明的变量指定单独的select语句。也许我误解了CASE和IF的功能。请帮帮我,我卡住了
我也可以在C语言中进行探索,但我在C语言中还是相当生疏的。我在一个DataTable中得到了结果,但除了将每一行转换为一个字符串之外,我还不知道如何计算该表 因此,您有一组记录,其中要关注的列是Ord_Inv_No和Credits 第一点,您正在设置一个临时变量,您似乎什么都不做 至于select查询
SELECT * FROM Orders WHERE Ord_Inv_No NOT IN (original inv no with credit, inv no + 'C')
我在带有信用的原始inv no,inv no+‘C’中看到的唯一区别是,它的信用列值从第一位设置为True。如果是这种情况,此查询应适用于您:
SELECT * FROM Orders WHERE Credits=true
因此,您有一组记录,其中要关注的列是Ord_Inv_No和Credits 第一点,您正在设置一个临时变量,您似乎什么都不做 至于select查询
SELECT * FROM Orders WHERE Ord_Inv_No NOT IN (original inv no with credit, inv no + 'C')
我在带有信用的原始inv no,inv no+‘C’中看到的唯一区别是,它的信用列值从第一位设置为True。如果是这种情况,此查询应适用于您:
SELECT * FROM Orders WHERE Credits=true
我最终在C中完成了以下工作:
foreach(DataRow row in results.Rows)
{
credit = Convert.ToBoolean(row["Credits"].ToString());
if(credit)
{
if (String.IsNullOrWhiteSpace(selectString))
{
tempString = row["Ord_Inv_No"].ToString();
selectString = String.Format("'{0}', '{0}C'",tempString);
}
else
{
tempString = row["Ord_Inv_No"].ToString();
selectString += String.Format(", '{0}', '{0}C'", tempString);
}
}
}
// The following three lines are for troubleshooting, shows the items being selected out and the initial count.
// If DriverKey is 9 and dates are 5-2-16 - 5-8-16 String should show to INV numbers and count should be 19
if (!String.IsNullOrWhiteSpace(selectString))
MessageBox.Show(selectString);
MessageBox.Show(results.Rows.Count.ToString());
// only process if string is not empty, need code
if (!String.IsNullOrWhiteSpace(selectString))
{
try
{
var rows = results.Select("Ord_Inv_No IN (" + selectString + ")");
foreach (var row in rows)
row.Delete();
results.AcceptChanges();
// This is for troubleshooting, gets the final number of rows after edits
// If DriverKey is 9 and dates are 5-2-16 - 5-8-16 final count should be 17
MessageBox.Show(results.Rows.Count.ToString());
results.Columns.Remove("Credits");
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
我最终在C中完成了以下工作:
foreach(DataRow row in results.Rows)
{
credit = Convert.ToBoolean(row["Credits"].ToString());
if(credit)
{
if (String.IsNullOrWhiteSpace(selectString))
{
tempString = row["Ord_Inv_No"].ToString();
selectString = String.Format("'{0}', '{0}C'",tempString);
}
else
{
tempString = row["Ord_Inv_No"].ToString();
selectString += String.Format(", '{0}', '{0}C'", tempString);
}
}
}
// The following three lines are for troubleshooting, shows the items being selected out and the initial count.
// If DriverKey is 9 and dates are 5-2-16 - 5-8-16 String should show to INV numbers and count should be 19
if (!String.IsNullOrWhiteSpace(selectString))
MessageBox.Show(selectString);
MessageBox.Show(results.Rows.Count.ToString());
// only process if string is not empty, need code
if (!String.IsNullOrWhiteSpace(selectString))
{
try
{
var rows = results.Select("Ord_Inv_No IN (" + selectString + ")");
foreach (var row in rows)
row.Delete();
results.AcceptChanges();
// This is for troubleshooting, gets the final number of rows after edits
// If DriverKey is 9 and dates are 5-2-16 - 5-8-16 final count should be 17
MessageBox.Show(results.Rows.Count.ToString());
results.Columns.Remove("Credits");
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}