Enitity Framework中的Sqldatareader [英] Sqldatareader in Enitity Framework
问题描述
Hi Team,
我是SQL和Entity Framework的新手。我的代码如下。基本上我使用DbDatareader加载值。
I am new to SQL and Entity Framework. I have my code below. Basically I am loading values using DbDatareader.
我不知道这段代码的性能。而且我还需要明确打开和关闭连接,无论如何我们都不必明确打开连接。
I am not aware about the performance of this code. And also I need to explicitly open and close the connection, Is there anyway where we donot have to explicitly open the connection.
private static Dictionary < string ,
ArchiveType > LoadArchiveTypeDictionary()
privatestaticDictionary<string, ArchiveType> LoadArchiveTypeDictionary()
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; {
{
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;
字典 < string ,
ArchiveType > archiveTypes
= new 字典 < string ,
ArchiveType >( StringComparer 。InvariantCultureIgnoreCase);
Dictionary<string, ArchiveType> archiveTypes = newDictionary<string, ArchiveType>(StringComparer.InvariantCultureIgnoreCase);
使用 ( var dbContext
= new DbContext ( " ; DNNAME" 温泉n> ))
using(vardbContext = newDbContext("DNNAME"))
  ;&NBSP;&NBSP;&NBSP; {
{
dbContext.Database.Connection.Open();
dbContext.Database.Connection.Open();
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP ;&NBSP;
试用
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; {
{
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; &NBSP;
var cmdIns = dbContext.Database.Connection.CreateCommand();
varcmdIns = dbContext.Database.Connection.CreateCommand();
  ;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; cmdIns.CommandText =
cmdIns.CommandText =
" SELECT [ID],[NAME]" +
"SELECT [Id],[NAME] "+
  ;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;
"FROM [DBNAME]
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;
使用 (System.Data.Common。 DbDataReader reader
= cmdIns.ExecuteReader())
using(System.Data.Common.DbDataReaderreader = cmdIns.ExecuteReader())
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; {
{
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;
while (reader.Read())
while(reader.Read())
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; {
{
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;
IDataRecord record =( IDataRecord )阅读器;
IDataRecordrecord = (IDataRecord)reader;
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP ;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;
ArchiveType archiveType =
new ArchiveType (记录);
ArchiveTypearchiveType = newArchiveType(record);
  ;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;& NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; archiveTypes.Add(archiveType.Name,archiveType);
archiveTypes.Add(archiveType.Name, archiveType);
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; }
}
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; &NBSP; }
}
}
}
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP ;&NBSP;&NBSP;
catch ( 异常 ex)
catch(Exceptionex)
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; &NBSP;&NBSP;&NBSP;&NBSP; {
{
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; &NBSP;
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP ;&NBSP; }
}
最后
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; {
{
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; &NBSP; dbContext.Database.Connection.Close();
dbContext.Database.Connection.Close();
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP ;&NBSP; }
}
return archiveTypes;
returnarchiveTypes;
  ;&NBSP; }
}
}
}
谢谢
Sushil
推荐答案
首先,如果你想发布代码,那么工具栏图标就在右边HTML图标是代码格式化工具。
First off, if you want to post code, then the toolbar icon to the right of the HTML icon is a code formatting tool.
如果您使用的是EF 6,那么您可以执行链接中的内容以通过EF连接获取连接, 。使用ADO.NET,SQL命令对象,parmterized直列T-SQL或parmterized SPROC,一个DataReader并使用DTO图案
If you are using EF 6, then you can do what is in the link to get the connection through the EF connection, use ADO.NET, SQL Command objects, parmterized inline T-SQL or parmterized sproc, a datareader and use the DTO pattern.
&NBSP; HTTPS://docs.microsoft.com/en -us / ef / ef6 / modeling / designer / advanced / multiple-results-sets
https://docs.microsoft.com/en-us/ef/ef6/modeling/designer/advanced/multiple-result-sets
如果您使用的是EF的早期版本,则必须执行链接中的操作。
If you are using prior versions of EF, then you have to do what is in the link.
https://blogs.msdn.microsoft.com/alexj/2009/11/07/tip-41-how-to-execute-t-sql-directly-against-the-数据库/
https://blogs.msdn.microsoft.com/alexj/2009/11/07/tip-41-how-to-execute-t-sql-directly-against-the-database/
https://www.codeproject.com/Articles/1050468/Data-Transfer-Object-Design-Pattern-in-Csharp
https://www.codeproject.com/Articles/1050468/Data-Transfer-Object-Design-Pattern-in-Csharp
这篇关于Enitity Framework中的Sqldatareader的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!