DataSet是否具有Contain或In功能?
问题描述:
我只想知道数据库中是否存在某些数据。DataSet是否具有Contain或In功能?
Normaly我用SqlDataReader
,采取循环SqlDataReader的投入变量数组或列表, 和业务层再次循环数组或列表上,并与X的数据进行比较,看它是否是列表或数组。
SqlDataReader readerOne = comm_SelectOne.ExecuteReader();
while (readerOne.Read())
{
...
}
我觉得这是没有效率的,有两个回路(数据访问层收集和业务层进行比较)
有另一种方式与一个数据集来做到这一点?
答
否有is'nt“In
”或“Contains
”在DataSet
因为DataSet
本身就是DataTable
容器和数据被保存在与任何特定的数据表相关联DataRow
功能。
检查数据库中是否存在数据的最简单方法是编写SQL Count
语句,例如, SELECT COUNT(columnName) FROM tableName WHERE columnName = 'some value'
。如果数据库中不存在“总和值”,它将返回0,否则返回计数。
答
基本上DataSet只是DataTable的一个容器。如果您想了解DataSet实例中的DataTable实例中的特定数据,可以从DataSet中获取DataTable实例,并且有一个名为“Select”方法(使用参数调用它)的实例方法从DataTable实例查询特定数据。
答
,我发现在互联网上参考:
我Bussines层:
public List<string> CompareInsee(string TheZone, List<object> InseList)
{
try
{
List<string> TempDict = new List<string>();
RempliClientInseExtracted(TheZone, ref NomTable);
DataTable TempDS = oClInse.Get_All_Inse(NomTable);
DataRow drFound;
DataColumn[] dcPk = new DataColumn[1];
// Set Primary Key
dcPk[0] = TempDS.Columns["NO_INSEE"];
TempDS.PrimaryKey = dcPk;
// Find the Row specified in txtFindArg
foreach (var oItem in InseList)
{
drFound = TempDS.Rows.Find(oItem);
if (drFound != null) TempDict.Add(oItem.ToString());
}
return TempDict;
}
catch (Exception excThrown)
{
if (!excThrown.Message.StartsWith("Err_")) { throw new Exception("Err_BL_ReadAllClientInsee", excThrown); }
else { throw new Exception(excThrown.Message, excThrown); }
}
}
数据艾策斯层:
public DataTable Get_All_Inse(string NomTable)
{
try
{
using (var connectionWrapper = new Connexion())
{
var connectedConnection = connectionWrapper.GetConnected();
string sql_SelectAll = "SELECT * FROM " + NomTable;
SqlCommand comm_SelectAll = new SqlCommand(sql_SelectAll, connectionWrapper.conn);
SqlDataAdapter adapt_SelectAll = new SqlDataAdapter();
adapt_SelectAll.SelectCommand = comm_SelectAll;
DataTable dSet_SelectAll = new DataTable();
adapt_SelectAll.Fill(dSet_SelectAll);
dSet_SelectAll.Dispose();
adapt_SelectAll.Dispose();
return dSet_SelectAll;
}
}
catch (Exception excThrown)
{
if (!excThrown.Message.StartsWith("Err_")) { throw new Exception("Err_GetAllUsrClient", excThrown); }
else { throw new Exception(excThrown.Message, excThrown); }
}
}
所以,现在我只有1循环 - >就在我的Bussines图层,不在DAL中。
感谢大家
你在这里谈论的检查,如果一个对象(非列表)为对象的列表或者你想知道,如果对象列表中的对象的另一列表中找到? –
你可以添加更多的细节?你的意思是*任何*数据,一些*特定的*数据,或一定数量的行?还是你更一般地说话? –