我正在使用ADO.Net + C#+ VSTS 2008 + ADO.Net连接到SQL Server 2008 Enterprise.我使用的是几乎相同的模式/示例 - 使用ADO.Net DataReader通过一个条目(行)检索一个条目(行)的数据.
http://msdn.microsoft.com/en-us/library/haa3afyz.aspx
我的问题是,如果我在此示例中设置SqlCommand超时,1.我认为超时适用于我们可以用多长时间来检索一个特定行,而不是整个数据逐条目的总超时环?
BTW:循环我的意思是,
while (reader.Read())
{
Console.WriteLine("{0}\t{1}", reader.GetInt32(0),
reader.GetString(1));
}
Run Code Online (Sandbox Code Playgroud)
2.此超时仅考虑从数据库检索数据条目所需的时间,并且此超时与我们处理每个条目的时间无关(例如,如果我们将超时设置为20秒,如果它需要1第二个从数据库中检索一个数据条目,我的应用程序逻辑需要30秒来操作数据条目,超时将永远不会发生).
正确理解?
我的网站使用的是企业库v 5.0.主要是DAAB.一些函数,如executioncalar,executionataset正在按预期工作.当我开始使用读者时会出现问题
我的include类中有这个函数:
Public Function AssignedDepartmentDetail(ByVal Did As Integer) As SqlDataReader
Dim reader As SqlDataReader
Dim Command As SqlCommand = db.GetSqlStringCommand("select seomthing from somewhere where something = @did")
db.AddInParameter(Command, "@did", Data.DbType.Int32, Did)
reader = db.ExecuteReader(Command)
reader.Read()
Return reader
End Function
Run Code Online (Sandbox Code Playgroud)
这是从我的aspx.vb中调用的,如下所示:
reader = includes.AssignedDepartmentDetail(Did)
If reader.HasRows Then
TheModule = reader("templatefilename")
PageID = reader("id")
Else
TheModule = "#"
End If
Run Code Online (Sandbox Code Playgroud)
这会在db.ExecuteReader行上出现以下错误:
无法将类型为"Microsoft.Practices.EnterpriseLibrary.Data.RefCountingDataReader"的对象强制转换为"System.Data.SqlClient.SqlDataReader".
任何人都可以了解我如何开展这项工作.通过entlib与读者打交道时,我是否会遇到问题?
string query = "select * from cfo_daily_trans_hist";
try
{
using (SqlConnection connection = new SqlConnection(
cnnString))
{
SqlCommand command = new SqlCommand(query);
command.Connection = connection;
connection.Open();
var result = command.ExecuteReader();
DataTable datatable = new DataTable();
datatable.Load(result);
connection.Close();
}
}
Run Code Online (Sandbox Code Playgroud)
因此,var result通过创建ExecuteReader();和HasRows是true,它显示领域的正确的金额.但是,DataTable我从它创建的是空的.
我究竟做错了什么?我99%肯定它正在获取数据,但我不知道如何通过该SqlDataReader对象找到它以确保.
谢谢.
我有一个SqlDataReader,但它永远不会进入Read()。
当我调试它时,它通过了循环while(readerOne.Read())。即使有数据,它也不会进入这个循环。
public static List<Pers_Synthese> Get_ListeSynthese_all(string codeClient, DateTime DateDeb, DateTime DateFin)\n{\n try\n {\n using (var connectionWrapper = new Connexion())\n {\n var connectedConnection = connectionWrapper.GetConnected();\n\n string sql_Syntax = Outils.LoadFileToString(Path.Combine(appDir, @"SQL\\Get_ListeSynthese_All.sql"));\n\n SqlCommand comm_Command = new SqlCommand(sql_Syntax, connectionWrapper.conn);\n comm_Command.Parameters.AddWithValue("@codeClioent", codeClient);\n comm_Command.Parameters.AddWithValue("@DateDeb", DateDeb);\n comm_Command.Parameters.AddWithValue("@DateFin", DateFin);\n\n List<Pers_Synthese> oListSynthese = new List<Pers_Synthese>();\n\n SqlDataReader readerOne = comm_Command.ExecuteReader();\n\n while (readerOne.Read())\n {\n Pers_Synthese oSyntehse = new Pers_Synthese();\n oSyntehse.CodeTrf = readerOne["CODE_TARIF"].ToString();\n oSyntehse.NoLV = readerOne["NOID"].ToString();\n oSyntehse.PrxUnitaire = readerOne["PRIX_UNITAIRE"].ToString();\n oSyntehse.ZoneId = readerOne["LE_ZONE"].ToString();\n oSyntehse.LeZone = …Run Code Online (Sandbox Code Playgroud) 我使用以下方法对数据库执行查询并读取数据:
using(SqlConnection connection = new SqlConnection("Connection string"))
{
connection.Open();
using(SqlCommand command = new SqlCommand("SELECT * FROM TableName", connection))
{
using (SqlDataReader reader = command.ExecuteReader())
{
// read and process data somehow (possible source of exceptions)
} // <- reader hangs here if exception occurs
}
}
Run Code Online (Sandbox Code Playgroud)
在读取和处理数据时,可能会发生一些异常.问题是当抛出异常时DataReader挂起Close()电话.你有什么想法吗??? 以及如何以正确的方式解决这个问题?当我在处理读卡器之前编写try..catch..finally块而不是using和调用时,问题就消失了.command.Cancel()finally
工作版本:
using(SqlConnection connection = new SqlConnection("Connection string"))
{
connection.Open();
using(SqlCommand command = new SqlCommand("SELECT * FROM TableName", connection))
{
SqlDataReader reader = …Run Code Online (Sandbox Code Playgroud) SqlDataReader有没有办法在连接关闭后访问a ?
或者是否有任何对象相当于SqlDataReader我可以将阅读器存储到其中并稍后处理对象?
我正在从服务器接收数据透视表,因此我无法使用普通类来处理此类数据,我的模型如下所示:
public class OneToNinetyNine
{
public List<Cities> listCities;
public string CityID;
public DateTime DateFrom;
public DateTime DateTo;
// this is the reader that I attempt to pass to the views
public SqlDataReader SqlReader;
}
Run Code Online (Sandbox Code Playgroud) 从SqlDataReader检索结果集时,可以使用GetSchemaTable方法获取该结果集的大部分元数据。然而,缺少的一件事是字符串字段的排序规则(即CHAR, VARCHAR, NCHAR, NVARCHAR, SQL_VARIANT– 如果包含字符串类型 – 甚至不推荐使用的TEXT和NTEXT)。有什么办法可以得到这些信息吗?
虽然在技术上可以调用具有和属性的GetSqlString方法,但这些属性返回与数据库的默认排序规则相关的值,这可能是也可能不是任何特定字段的排序规则(即使大多数人似乎只是假设它是相同的)或表达式(由于Collation Precedence)。这些属性仅在源数据类型为 时才提供准确信息,这并不是很有帮助。LCIDSqlCompareOptionsSQL_VARIANT
没有这些额外信息(至少相当于 Locale 的“LCID”)的问题是,虽然所有字符,无论源编码如何,都可以在 .NET 中表示而不会丢失(因为 .NET 字符串是 UTF- 16 Little Endian),在将结果集中的字段与其他字符串进行比较时,无法确定要使用的 Locale。
每个字符串字段的排序规则信息绝对是通过 TDS 流从 SQL Server 发送到客户端的结果集元数据的一部分。它用于:
LCID通过类的GetLocaleId方法公开SqlDataReader。但是该方法已被标记,internal因此我无权访问它。它似乎只在一个地方使用:SqlBulkCopy。SqlPipe用于 SQLCLR 存储过程和触发器。encoding根据情况在 的GetTextReader()方法中设置适当的SqlDataReader。但是,虽然该方法是public …我遇到了一个奇怪的问题,我可以通过调用存储过程返回结果,但代码追溯失败.
public IEnumerable<T> ExecuteStoredProcedure<T>(string storedProcedureName, IDataMapper<T> mapper, IDictionary<string, object> parameters)
{
using (var connection = new SqlConnection(connectionString))
{
using (var cmd = new SqlCommand(storedProcedureName, connection))
{
cmd.CommandType = CommandType.StoredProcedure;
foreach (var key in parameters.Keys)
{
cmd.Parameters.AddWithValue(key, parameters[key]);
}
connection.Open();
SqlDataReader reader = cmd.ExecuteReader();
//return MapRecordsToDTOs(reader, mapper);
//let's test:
IEnumerable<T> result = MapRecordsToDTOs(reader, mapper);
var x = (new List<T>(result)).Count;
System.Diagnostics.Debug.WriteLine(x);
return result;
}
}
}
private static IEnumerable<T> MapRecordsToDTOs<T>(SqlDataReader reader, IDataMapper<T> mapper)
{
if (reader.HasRows)
{
while (reader.Read())
{
System.Diagnostics.Debug.WriteLine(reader["Id"]); //what's …Run Code Online (Sandbox Code Playgroud) 我有一个奇怪的问题。在 GetValue() 函数上使用 System.Data.SqlClient.SqlDataReader 读取数据时,进程被冻结/卡住。我正在使用 WinDbg 分析进程转储。我用SOS命令一样!dlk,!SyncBlk,!analyze -v -hang等,但他们没有表示任何死锁。
调用堆栈上的最后一次调用是
000000001a98e8a8 0000000076febd7a [InlinedCallFrame: 000000001a98e8a8] .**SNIReadSyncOverAsync**(SNI_ConnWrapper*, SNI_Packet**, Int32)
000000001a98e8a8 000007fee9e8bca1 [InlinedCallFrame: 000000001a98e8a8] .SNIReadSyncOverAsync(SNI_ConnWrapper*, SNI_Packet**, Int32)
000000001a98e880 000007fee9e8bca1 DomainBoundILStubClass.IL_STUB_PInvoke(SNI_ConnWrapper*, SNI_Packet**, Int32)
000000001a98e950 000007fee9e7254f SNINativeMethodWrapper.SNIReadSyncOverAsync(System.Runtime.InteropServices.SafeHandle, IntPtr ByRef, Int32)
000000001a98e9c0 000007fee9e7226e System.Data.SqlClient.TdsParserStateObject.ReadSniSyncOverAsync()
000000001a98ea70 000007fee9e72180 System.Data.SqlClient.TdsParserStateObject.TryReadNetworkPacket()
000000001a98eab0 000007fee9e72950 System.Data.SqlClient.TdsParserStateObject.TryPrepareBuffer()
000000001a98eae0 000007fee9e728d8 System.Data.SqlClient.TdsParserStateObject.TryReadByteArray(Byte[], Int32, Int32, Int32 ByRef)
000000001a98eb50 000007feea4c0adc System.Data.SqlClient.TdsParser.TryReadSqlValue(System.Data.SqlClient.SqlBuffer, System.Data.SqlClient.SqlMetaDataPriv, Int32, System.Data.SqlClient.TdsParserStateObject)
000000001a98ec10 000007fee9e85cb5 System.Data.SqlClient.SqlDataReader.TryReadColumnInternal(Int32, Boolean)
000000001a98ecb0 000007fee9e85af0 System.Data.SqlClient.SqlDataReader.TryReadColumn(Int32, Boolean, Boolean)
000000001a98ed20 000007fee9e85a0b System.Data.SqlClient.SqlDataReader.GetValueInternal(Int32)
000000001a98ed60 000007fee9e859b7 System.Data.SqlClient.SqlDataReader.GetValue(Int32)
Run Code Online (Sandbox Code Playgroud)
在这里进一步调试的其他途径是什么?有人在使用 SqlDataReader …
我来自这里的这个问题,但我有一个不同的案例。我需要我的结果DataTable,我有两种可能的方法:
public static DataTable SelectDataTable(string query, string ConnectionString)
{
using (SqlConnection myConnection = new SqlConnection(ConnectionString))
{
using (SqlDataAdapter myDataAdapter = new SqlDataAdapter(query, myConnection))
{
DataTable dt = new DataTable();
myDataAdapter.Fill(dt);
return dt;
}
}
}
Run Code Online (Sandbox Code Playgroud)
和
public static DataTable SelectDataTable(string query, string ConnectionString)
{
using (SqlConnection myConnection = new SqlConnection(ConnectionString))
{
using (SqlCommand cmd = new SqlCommand(query, myConnection))
{
myConnection.Open();
DataTable dt = new DataTable();
dt.Load(cmd.ExecuteReader(CommandBehavior.CloseConnection));
return dt;
}
}
}
Run Code Online (Sandbox Code Playgroud)
所以我的问题是:两者之间有区别吗
SqlDataAdapter+Fill()
和 …
sqldatareader ×10
c# ×7
.net ×5
sql-server ×2
ado.net ×1
asp.net-mvc ×1
collation ×1
daab ×1
datatable ×1
deadlock ×1
dynamic ×1
idisposable ×1
sql ×1
sqlclr ×1
using ×1
windbg ×1