我可以将 SqlParameterCollection 对象传递给 SQL 命令吗
我目前有一个连接类来处理所有数据库连接。我想做的是在一页上构建一个 SqlParameterCollection 对象,然后将该对象传递给我的连接类。可以这样做吗?我没有收到任何编译错误,但无法识别参数。这是我正在尝试做的事情:
Page 1:
string sql = null;
conn.strConn = connectionstring;
sql = "sqlstring";
SqlParameterCollection newcollect = null;
newcollect.Add("@Switch",1);
conn.OpenReader(sql, newcollect);
while (conn.DR.Read())
{
read data onto page here...
}
conn.CloseReader();
Page 2 (connection class) :
public void OpenReader(string sql, SqlParameterCollection collect)
{
Conn = new SqlConnection(strConn);
Conn.Open();
Command = new SqlCommand(sql,Conn);
Command.Parameters.Add(collect); <------This is the root of my question
Command.CommandTimeout = 300;
// executes sql and fills data reader with data
DR = Command.ExecuteReader();
}
I currently have a connection class that handles all of my database connectivity. What I am trying to do is build a SqlParameterCollection object on one page, then pass that object to my connection class. Is it possible to do this? I am not getting any compilation errors, but I can not get the parameters to be recognized. Here is what I am trying to do:
Page 1:
string sql = null;
conn.strConn = connectionstring;
sql = "sqlstring";
SqlParameterCollection newcollect = null;
newcollect.Add("@Switch",1);
conn.OpenReader(sql, newcollect);
while (conn.DR.Read())
{
read data onto page here...
}
conn.CloseReader();
Page 2 (connection class) :
public void OpenReader(string sql, SqlParameterCollection collect)
{
Conn = new SqlConnection(strConn);
Conn.Open();
Command = new SqlCommand(sql,Conn);
Command.Parameters.Add(collect); <------This is the root of my question
Command.CommandTimeout = 300;
// executes sql and fills data reader with data
DR = Command.ExecuteReader();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
基本上,在 ASP.NET 中,要在页面之间保留某些内容,您需要使用会话状态并将对象放在那里 - 所以您可以尝试这样的操作:
Page 1:
Page 2 (连接类):
这将起作用 - 如果您在 ASP.NET 站点上启用了会话状态。如果您的站点由于某种原因无法使用会话状态内存(例如您位于网络场并且无法使用 SQL Server 来获取会话状态),则此方法将不起作用
Basically, in ASP.NET, to persist something from page to page, you need to use session state and put your object there - so you could try something like this:
Page 1:
Page 2 (connection class) :
This will work - if you have Session state enabled on your ASP.NET site. This will not work if your site cannot use session state memory for some reason (like if you're on a webfarm and cannot use SQL Server for session state)
您可以通过以下方式实例化一个新的
SqlParameterCollection
:You can instantiate a new
SqlParameterCollection
by like that: