返回一个SqlDataReader [英] Returning a SqlDataReader
问题描述
我有这个code:
公共静态SqlDataReader的GetGeneralInformation(INT RecID)
{
使用(VAR康恩=新的SqlConnection(GetConnectionString()))
使用(VAR CMD = conn.CreateCommand())
{
conn.Open();
cmd.CommandText =
@SELECT cs.Status,cs.Completed
从NC_Steps小号
INNER JOIN NC_ClientSteps CS
ON cs.RecID = s.RecID
WHERE cs.ClientID = 162
与s.RecID = @value;
cmd.Parameters.AddWithValue(@value,RecID);
使用(VAR读卡器= cmd.ExecuteReader())
{
如果(reader.Read())
{
返回阅读器;
}
返回null;
}
}
}
我如何参考呢?
我试过,但它不能正常工作。
SqlDataReader的读卡器= GeneralFunctions.GetGeneralInformation();
另外我如何从读者阅读?
Reader.GetString(reader.GetOrdinal(状态))
修改
我现在得到以下错误:
异常详细信息:System.NullReferenceException:未将对象引用 设置为一个对象的一个实例。
下面是更新code:
公共静态的IEnumerable< IDataRecord> GetGeneralInformation(INT客户端ID)
{
使用(VAR康恩=新的SqlConnection(GetConnectionString()))
使用(VAR CMD = conn.CreateCommand())
{
conn.Open();
cmd.CommandText =
@SELECT i.GoLiveDate,i.FirstBonusRun,i.TechFName,i.TechLName,i.TechEmail,i.TechPhone,i.WebISPFName,i.WebISPLName,
i.WebISPEmail,i.WebISPPhone,i.FullFillFName,i.FullFillLName,i.FullFillEmail,i.FullFillPhone,d.FName,
d.LName,d.HomePhone,d.Email
从NC_Information我
INNER JOIN经销商ð
ON d.DistID = i.ClientID
WHERE clientID的= @value;
cmd.Parameters.AddWithValue(@value,ClientID的);
使用(VAR读卡器= cmd.ExecuteReader())
{
而(reader.Read())
{
得到回报读者;
}
产量返回NULL;
}
}
}
保护无效的Page_Load(对象发件人,EventArgs的)
{
IEnumerable的< IDataRecord>读卡器= GeneralFunctions.GetGeneralInformation((int)的会话[DistID]);
//字符串结果= GeneralFunctions.GetGeneralInformation(Globals.GeneralInformation)。首先()状态]的ToString();
}
现在的问题是,离开的功能(通过return语句)踢你出的使用
块,因此SqlDataReader的,并且您使用SqlConnections布置。为了解决这个问题,尝试改变函数签名这样的:
公共静态的IEnumerable< IDataRecord> GetGeneralInformation(INT RecID)
和然后更新这样的函数的中间
使用(VAR读卡器= cmd.ExecuteReader())
{
而(reader.Read())
{
得到回报读者;
}
}
对于最后的我如何读取它?的一部分,它可能是这样的:
字符串结果= reader.First()状态]的ToString()。
I have this code:
public static SqlDataReader GetGeneralInformation ( int RecID )
{
using ( var conn = new SqlConnection( GetConnectionString() ) )
using ( var cmd = conn.CreateCommand() )
{
conn.Open();
cmd.CommandText =
@"SELECT cs.Status, cs.Completed
FROM NC_Steps s
INNER JOIN NC_ClientSteps cs
ON cs.RecID = s.RecID
WHERE cs.ClientID = 162
AND s.RecID = @value";
cmd.Parameters.AddWithValue( "@value", RecID );
using ( var reader = cmd.ExecuteReader() )
{
if ( reader.Read() )
{
return reader;
}
return null;
}
}
}
How do I reference this?
I tried this but it does not work.
SqlDataReader reader = GeneralFunctions.GetGeneralInformation();
Also how would I read from the reader?
Reader.GetString( reader.GetOrdinal( "Status" ) )
Edit:
I am now getting the following error:
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
Here is the updated code:
public static IEnumerable<IDataRecord> GetGeneralInformation ( int ClientID )
{
using ( var conn = new SqlConnection( GetConnectionString() ) )
using ( var cmd = conn.CreateCommand() )
{
conn.Open();
cmd.CommandText =
@"SELECT i.GoLiveDate, i.FirstBonusRun, i.TechFName, i.TechLName, i.TechEmail, i.TechPhone, i.WebISPFName, i.WebISPLName,
i.WebISPEmail, i.WebISPPhone, i.FullFillFName, i.FullFillLName, i.FullFillEmail, i.FullFillPhone, d.FName,
d.LName, d.HomePhone, d.Email
FROM NC_Information i
INNER JOIN Distributor d
ON d.DistID = i.ClientID
WHERE clientID = @value";
cmd.Parameters.AddWithValue( "@value", ClientID );
using ( var reader = cmd.ExecuteReader() )
{
while ( reader.Read() )
{
yield return reader;
}
yield return null;
}
}
}
protected void Page_Load(object sender, EventArgs e)
{
IEnumerable<IDataRecord> reader = GeneralFunctions.GetGeneralInformation( (int)Session[ "DistID" ] );
//string result = GeneralFunctions.GetGeneralInformation( Globals.GeneralInformation ).First()[ "Status" ].ToString();
}
The problem is that leaving the function (via the return statement) kicks you out of the using
blocks, and so the SqlDataReader and SqlConnections you are using are disposed. To get around the problem, try changing the function signature like this:
public static IEnumerable<IDataRecord> GetGeneralInformation ( int RecID )
and then update the middle of the function like this:
using ( var reader = cmd.ExecuteReader() )
{
while ( reader.Read() )
{
yield return reader;
}
}
For the final "How do I read from it?" part, it might look like this:
string result = reader.First()["Status"].ToString();
这篇关于返回一个SqlDataReader的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!