我只想知道数据库中是否存在某些数据。
通常我使用SqlDataReader,在数组或列表中循环SqlDataReader put变量,在业务层中再次循环数组或列表,并与X数据进行比较,看看它是否在列表或数组中。
SqlDataReader readerOne = comm_SelectOne.ExecuteReader();
while (readerOne.Read())
{
...
}我认为这效率不高,有两个循环(在数据访问层收集和在业务层比较)
有没有其他方法可以用DataSet做到这一点?
发布于 2011-10-05 19:24:45
不,DataSet中没有'In‘或'Contains’函数,因为DataSet本身是DataTable的容器,数据保存在与任何特定DataRow关联的DataTable中。
在not上检查数据库中是否存在数据的最简单方法是编写一条SQL Count语句,例如SELECT COUNT(columnName) FROM tableName WHERE columnName = 'some value'。如果'sum value‘在数据库中不存在,它将返回0,否则返回计数。
发布于 2011-10-06 01:52:21
基本上,DataSet只是一个DataTable(s)容器。如果您想在DataSet实例中了解DataTable实例中的特定数据,可以从DataSet获取DataTable实例,有一个名为"Select“方法(带参数调用)的实例方法可以查询DataTable实例中的特定数据。
发布于 2011-10-06 04:08:20
我在互联网上找到了以下内容:
Stack和Find Data
我的业务层:
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); }
}
}所以现在我只有一个循环-->只是在我的Bussines层,而不是DAL。
谢谢大家
https://stackoverflow.com/questions/7658008
复制相似问题