OleDbDataReader作为参数 [英] OleDbDataReader as parameter

查看:48
本文介绍了OleDbDataReader作为参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我希望提供格式作为参数,我尝试过OleDbDataReader

但得到一个空参考。

就可以'似乎让这个工作起来。我认为这与

ExecuteReader有关。


似乎也不能用这个作为第一个婴儿车

OleDbDataReader reader = new OleDbDataReader()

ie

public static string Read(OleDbDataReader reader,string query,string

format )


如果可能的话,这就是我想要的参数。 "< p为H. +

reader [" Text"]。ToString()+"< / p>" +" \ n"

// returndata + =("< p> + reader [" Text"]。ToString()+"< / p>" + " \ n");

public static string Read(OleDbDataReader reader,string query,string

pram3)

{


OleDbConnection conn = null;

// OleDbDataReader reader = null;

string returndata = null;

尝试

{

conn = new OleDbConnection(MySite.GlobalSettings.ConnectionSt ring);

conn.Open();


OleDbCommand cmd = new OleDbCommand(query,conn);

cmd.CommandType = CommandType.StoredProcedure;

reader = cmd.ExecuteReader();


while(reader.Read())

{

// returndata + =("< p> + reader [" Text"]。ToString()+"< / p>" +" \ n");

}

}


// catch(例外e)

// {

// Response.Write(e.Message);

// Response.End();

//}

终于

{

if(reader!= null)reader.Close();

if(conn!= null)conn.Close();

}

返回returnndata;

}


谢谢


Dion


I wish to supply the formating as a parameter, I have tried OleDbDataReader
but get a null ref.
just can''t seem to get this to work. I think this has something to do with
ExecuteReader.

also can''t seem to use this as the first pram
OleDbDataReader reader = new OleDbDataReader()

i.e
public static string Read(OleDbDataReader reader, string query, string
format )

this is what I would like as a parameter if possible. "<p> +
reader["Text"].ToString() + "</p>" + "\n"
//returndata += ("<p> + reader["Text"].ToString() + "</p>" + "\n" );
public static string Read(OleDbDataReader reader, string query, string
pram3 )
{

OleDbConnection conn = null;
//OleDbDataReader reader = null;
string returndata = null;
try
{
conn = new OleDbConnection(MySite.GlobalSettings.ConnectionSt ring);
conn.Open();

OleDbCommand cmd = new OleDbCommand(query, conn);
cmd.CommandType = CommandType.StoredProcedure;
reader = cmd.ExecuteReader();

while( reader.Read() )
{
//returndata += ("<p> + reader["Text"].ToString() + "</p>" + "\n" );
}
}

// catch (Exception e)
// {
// Response.Write(e.Message);
// Response.End();
// }
finally
{
if (reader != null) reader.Close();
if (conn != null) conn.Close();
}
return returndata;
}

Thanks

Dion

推荐答案

为什么要将DataReader传递给方法?!?
Why do you want to pass the DataReader into the method?!?


Dion:


在你的例子中,你看起来没有得到任何东西从

传入它。如果你在当地宣布它回来了,你会在

的同一个地方。读者的问题和你使用它的方式是它需要一个开放的连接来迭代自己。除了抛出异常之外,不能与DB通话的读者不能为你做任何事情。所以

你可以从另一个类的方法中可以顺利地返回一个读者,并且

它所需要的一切都可能超出范围/相当,无法访问。


如果您使用相同的代码,并返回读者作为数据类型

你会在同一条船上,虽然我不认为任何一个都可以工作。


难道你不能只是遍历读者并在本地声明它,并且

只返回一个包含所有数据格式的字符串或字符串构建器?


我可能不太了解这种方法,但我们可能会得到

无论你想做什么工作。

干杯,


比尔

" Dion Heskett" < DH ****** @ llewellyn.co.uk>在留言中写道

新闻:uT ************** @ TK2MSFTNGP09.phx.gbl ...
Dion:

In your example, you It doesn''t look like you are getting anything from
passing it in. If you declared it locally and returned it, you''d be in the
same place. Problem with a Reader and the way you are using it is that it
needs an open connection to iterate through itself. A reader that can''t
talk to the DB can''t do anything for you other than throw exceptions. So
you could conciveably return a reader from a method in another class, and
everything it needs could be out of scope / rather, unaccessible.

If you used the same code though, and returned a reader as the Datatype
you''d be in the same boat, although I don''t think either will work.

Couldn''t you just iterate through the reader and declare it locally, and
just return a string or stringbuilder with all the data formatted?

I probably don''t totally understand the methodology, but we can probably get
whatever you want to do to work nonetheless.

Cheers,

Bill
"Dion Heskett" <dh******@llewellyn.co.uk> wrote in message
news:uT**************@TK2MSFTNGP09.phx.gbl...

我希望提供格式作为参数,我已经尝试过
OleDbDataReader,但得到一个空参考。
似乎无法让这个工作。我认为这与
ExecuteReader有关。

似乎也不能将它作为第一个婴儿车使用
OleDbDataReader reader = new OleDbDataReader()

即公共静态字符串读取(OleDbDataReader读取器,字符串查询,字符串格式)

这是我想要的参数,如果可能的话。 "< p为H. +
读者[" Text"]。ToString()+"< / p>" +" \ n"
// returndata + =("< p> + reader [" Text"]。ToString()+"< / p>" +" \ n");

public static string Read(OleDbDataReader reader,string query,string
pram3)


OleDbConnection conn = null;
OleDbDataReader reader = null;
string returndata = null;
尝试
{
conn = new OleDbConnection(MySite.GlobalSettings.ConnectionSt ring);
conn .Open();

OleDbCommand cmd = new OleDbCommand(query,conn);
cmd.CommandType = CommandType.StoredProcedure;
reader = cmd.ExecuteReader();
{returndata + =("< p> + reader [" Text"]。ToString()+" < / p>" +" \ n");
}
}
// catch(例外e)
// {
// Response.Write(e.Message);
// Response.End();
//}
最后
{
if(reader!= null)reader.Close();
if(conn! = null)conn.Close();
}
返回returnndata;
}
谢谢

Dion

I wish to supply the formating as a parameter, I have tried OleDbDataReader but get a null ref.
just can''t seem to get this to work. I think this has something to do with
ExecuteReader.

also can''t seem to use this as the first pram
OleDbDataReader reader = new OleDbDataReader()

i.e
public static string Read(OleDbDataReader reader, string query, string
format )

this is what I would like as a parameter if possible. "<p> +
reader["Text"].ToString() + "</p>" + "\n"
//returndata += ("<p> + reader["Text"].ToString() + "</p>" + "\n" );
public static string Read(OleDbDataReader reader, string query, string
pram3 )
{

OleDbConnection conn = null;
//OleDbDataReader reader = null;
string returndata = null;
try
{
conn = new OleDbConnection(MySite.GlobalSettings.ConnectionSt ring);
conn.Open();

OleDbCommand cmd = new OleDbCommand(query, conn);
cmd.CommandType = CommandType.StoredProcedure;
reader = cmd.ExecuteReader();

while( reader.Read() )
{
//returndata += ("<p> + reader["Text"].ToString() + "</p>" + "\n" );
}
}

// catch (Exception e)
// {
// Response.Write(e.Message);
// Response.End();
// }
finally
{
if (reader != null) reader.Close();
if (conn != null) conn.Close();
}
return returndata;
}

Thanks

Dion



如果这样做,那么我将需要在每个页面上创建这个函数,我将使用它,我在尝试为了避免这种情况。


我有一个适用于转发器/数据网格的重载版本。


我所做的就是声明一个转发器或数据网格对象并传入一个控件和

查询,检查它是数据网格还是转发器并绑定,然后关闭

数据库连接。

即Read(repeater1,MyQuery)


你还说它需要一个开放的连接,下面的代码打开了数据库


conn = new OleDbConnection(MySite.GlobalSettings.ConnectionSt ring);

conn.Open();

Dion


" William Ryan" <做******** @ nospam.comcast.net>在消息中写道

news:u4 ************** @ TK2MSFTNGP10.phx.gbl ...
It if do this then i will need to create this function on every page that i
use it, i''am trying to avoid this.

I have an overloaded version that works for a repeater/datagrid.

all i do is declare a repeater or datagrid object and pass in a control and
query, check if it''s a datagrid or repeater and bind, then close the
database connection.
ie Read(repeater1, "MyQuery")

You also say it needs an open connection, the below code opens the DB

conn = new OleDbConnection(MySite.GlobalSettings.ConnectionSt ring);
conn.Open();
Dion

"William Ryan" <do********@nospam.comcast.net> wrote in message
news:u4**************@TK2MSFTNGP10.phx.gbl...
Dion:

在你的例子中,你看起来没有得到任何东西从
传递进来。如果你在当地宣布它并返回它,你将在
一样的地方。阅读器的问题以及您使用它的方式是它需要一个开放的连接来迭代自己。除了抛出异常之外,不能与DB通信的读者不能为您做任何事情。所以
你可以从另一个类的方法中返回一个读者,并且
它所需要的一切都可能超出范围/相当,不可访问。

如果你使用相同的代码虽然,并且返回一个读者作为数据类型
你会在同一条船上,虽然我不认为任何一个会工作。

你不能只是迭代通过读者并在本地声明它,并且只返回一个字符串或字符串构建器,其中包含所有格式化的数据?

我可能不完全理解这种方法,但我们可能只需
得到你想做的任何工作。

干杯,

比尔
Dion Heskett < DH ****** @ llewellyn.co.uk>在消息中写道
新闻:uT ************** @ TK2MSFTNGP09.phx.gbl ...
Dion:

In your example, you It doesn''t look like you are getting anything from
passing it in. If you declared it locally and returned it, you''d be in the same place. Problem with a Reader and the way you are using it is that it
needs an open connection to iterate through itself. A reader that can''t
talk to the DB can''t do anything for you other than throw exceptions. So
you could conciveably return a reader from a method in another class, and
everything it needs could be out of scope / rather, unaccessible.

If you used the same code though, and returned a reader as the Datatype
you''d be in the same boat, although I don''t think either will work.

Couldn''t you just iterate through the reader and declare it locally, and
just return a string or stringbuilder with all the data formatted?

I probably don''t totally understand the methodology, but we can probably get whatever you want to do to work nonetheless.

Cheers,

Bill
"Dion Heskett" <dh******@llewellyn.co.uk> wrote in message
news:uT**************@TK2MSFTNGP09.phx.gbl...

我希望提供格式化为参数,我尝试过

I wish to supply the formating as a parameter, I have tried


OleDbDataReader


OleDbDataReader

但得到一个空参考。
似乎无法让这个工作。我认为这可以用ExecuteReader来做


似乎也不能用这个作为第一个婴儿车
OleDbDataReader reader = new OleDbDataReader()

即公共静态字符串读取(OleDbDataReader读取器,字符串查询,字符串格式)

这是我想要的参数,如果可能的话。 "< p为H. +
读者[" Text"]。ToString()+"< / p>" +" \ n"
// returndata + =("< p> + reader [" Text"]。ToString()+"< / p>" +" \ n");

public static string Read(OleDbDataReader reader,string query,string
pram3)


OleDbConnection conn = null;
OleDbDataReader reader = null;
string returndata = null;
尝试
{
conn = new OleDbConnection(MySite.GlobalSettings.ConnectionSt ring);
conn .Open();

OleDbCommand cmd = new OleDbCommand(query,conn);
cmd.CommandType = CommandType.StoredProcedure;
reader = cmd.ExecuteReader();
{returndata + =("< p> + reader [" Text"]。ToString()+" < / p>" +
" \ n"); }

// catch(例外e)
// {
// Response.Write(e.Message);
//回复.End();
//}
最后
{
if(reader!= null)reader.Close();
if(conn!= null) conn.Close();
}
返回returnndata;
}
谢谢

Dion
but get a null ref.
just can''t seem to get this to work. I think this has something to do with ExecuteReader.

also can''t seem to use this as the first pram
OleDbDataReader reader = new OleDbDataReader()

i.e
public static string Read(OleDbDataReader reader, string query, string
format )

this is what I would like as a parameter if possible. "<p> +
reader["Text"].ToString() + "</p>" + "\n"
//returndata += ("<p> + reader["Text"].ToString() + "</p>" + "\n" );
public static string Read(OleDbDataReader reader, string query, string
pram3 )
{

OleDbConnection conn = null;
//OleDbDataReader reader = null;
string returndata = null;
try
{
conn = new OleDbConnection(MySite.GlobalSettings.ConnectionSt ring);
conn.Open();

OleDbCommand cmd = new OleDbCommand(query, conn);
cmd.CommandType = CommandType.StoredProcedure;
reader = cmd.ExecuteReader();

while( reader.Read() )
{
//returndata += ("<p> + reader["Text"].ToString() + "</p>" + "\n" ); }
}

// catch (Exception e)
// {
// Response.Write(e.Message);
// Response.End();
// }
finally
{
if (reader != null) reader.Close();
if (conn != null) conn.Close();
}
return returndata;
}

Thanks

Dion




这篇关于OleDbDataReader作为参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆