我可以将 SqlParameterCollection 对象传递给 SQL 命令吗

发布于 2024-11-15 04:42:32 字数 850 浏览 2 评论 0原文

我目前有一个连接类来处理所有数据库连接。我想做的是在一页上构建一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

旧话新听 2024-11-22 04:42:32

基本上,在 ASP.NET 中,要在页面之间保留某些内容,您需要使用会话状态并将对象放在那里 - 所以您可以尝试这样的操作:

Page 1:

List<SqlParameter> newcollect = new List<SqlParameter>();
newcollect.Add(new SqlParameter("@Switch", 1));

Session["SqlParameters"] = newcollect;

Page 2 (连接类):

public void OpenReader(string sql)
{
    Conn = new SqlConnection(strConn);
    Conn.Open();

    Command = new SqlCommand(sql,Conn);

    List<SqlParameter> coll = null;
    if(Session["SqlParameters"] != null)
    {
       coll = (List<SqlParameter>)Session["SqlParameters"];
    }

    Command.Parameters.AddRange(coll.ToArray());
    Command.CommandTimeout = 300;
    // executes sql and fills data reader with data
    DR = Command.ExecuteReader(); 
}

这将起作用 - 如果您在 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:

List<SqlParameter> newcollect = new List<SqlParameter>();
newcollect.Add(new SqlParameter("@Switch", 1));

Session["SqlParameters"] = newcollect;

Page 2 (connection class) :

public void OpenReader(string sql)
{
    Conn = new SqlConnection(strConn);
    Conn.Open();

    Command = new SqlCommand(sql,Conn);

    List<SqlParameter> coll = null;
    if(Session["SqlParameters"] != null)
    {
       coll = (List<SqlParameter>)Session["SqlParameters"];
    }

    Command.Parameters.AddRange(coll.ToArray());
    Command.CommandTimeout = 300;
    // executes sql and fills data reader with data
    DR = Command.ExecuteReader(); 
}

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)

夏天碎花小短裙 2024-11-22 04:42:32

您可以通过以下方式实例化一个新的 SqlParameterCollection

var P = new SqlCommand().Parameters;

You can instantiate a new SqlParameterCollection by like that:

var P = new SqlCommand().Parameters;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文