类中存在多个 SqlDataReader 和 SqlCommand 的问题

发布于 2024-11-14 20:40:24 字数 2377 浏览 2 评论 0原文

我在使用 C# 时遇到问题,我有一个类,其中包含 SqlDataReader 的函数和 SqlCommand 的另一个函数(第一个类仅用于从数据库读取值,第二个类用于同一数据库中的 INSERT、UPDATE、DELETE ... )。

问题是,对于代码的第一部分(登录),我必须从 Active Directory 中搜索值(它有效),然后我必须查看用户在我自己的数据库中是否有用户名和密码(它有效) ,然后,如果用户不在数据库中,那么我必须创建它并获取 ID,如果它已经创建,那么我只需要获取 ID。

问题是我收到此消息:

InvalidOperationException 是 用户代码未处理

已经存在一个 Open DataReader 与此命令相关的,谁作为 先关闭。

有代码:

Class.cs:

    private static string MyConnectionString = "THIS IS MY CONNECTION";
    private SqlConnection MyConnection = new SqlConnection(MyConnectionString);
    public SqlCommand MyCommand = new SqlCommand();
    public SqlDataReader MyReader = null;

    public void DBMyReader(String SqlQuery)
    {
        if (MyConnection.State != ConnectionState.Open)
            MyConnection.Open();

        MyCommand.Connection = MyConnection;
        MyCommand.CommandText = SqlQuery;
        MyReader = MyCommand.ExecuteReader(CommandBehavior.CloseConnection);
    }

    public void DBMyUpdate(String SqlQuery)
    {
        if (MyConnection.State != ConnectionState.Open)
            MyConnection.Open();

        var cmdTest = new SqlCommand();

        cmdTest.Connection = MyConnection;
        cmdTest.CommandText = SqlQuery;
        cmdTest.ExecuteNonQuery();
    }

    public void DBMyInsert(String SqlQuery)
    {
        DBMyUpdate(SqlQuery);
    }

** Login.aspx.cs:**

                    MyClass.DBMyReader("SELECT util_codi,util_logi,util_nome FROM Tgep_util WHERE util_logi='"
                            + Session["username"].ToString() + "'");
            MyClass.MyReader.Read();

            if (!MyClass.MyReader.HasRows)
            {
                MyClass.MyReader.Close();
                MyClass.DBMyInsert("INSERT INTO Tgep_util(util_logi,util_nome) "
                            + "VALUES ('" + Session["username"].ToString() + "','" + Session["nome"].ToString() + "')");
            }

            MyClass.DBMyReader("SELECT util_codi,util_logi,util_nome FROM Tgep_util WHERE util_logi='"
            + Session["username"].ToString() + "'");

            MyClass.MyReader.Read();

            Session["user_id"] = MyClass.MyReader["util_codi"].ToString();

            Response.Redirect("FRM_Principal.aspx");

编辑:更新代码(目前有效)

I have a problem with C#, I have a Class with a function for SqlDataReader and another for SqlCommand (the first one is just for read values from a DataBase and the second one is for INSERT, UPDATE, DELETE ... in the same DB).

The problem is, for the first part of the code (the login), I have to search the values from an Active Directory (it works), then I must see if the user has username and password in my own DB (it works), and then, if the user is not in the DB then I have to create it and get the ID, if it is already created then I just have to get the ID.

The problem is that I get this message :

InvalidOperationException was
unhandled by user code

There already exist an Open DataReader
associated with this Command, who as
to be closed first.

There is the code :

Class.cs :

    private static string MyConnectionString = "THIS IS MY CONNECTION";
    private SqlConnection MyConnection = new SqlConnection(MyConnectionString);
    public SqlCommand MyCommand = new SqlCommand();
    public SqlDataReader MyReader = null;

    public void DBMyReader(String SqlQuery)
    {
        if (MyConnection.State != ConnectionState.Open)
            MyConnection.Open();

        MyCommand.Connection = MyConnection;
        MyCommand.CommandText = SqlQuery;
        MyReader = MyCommand.ExecuteReader(CommandBehavior.CloseConnection);
    }

    public void DBMyUpdate(String SqlQuery)
    {
        if (MyConnection.State != ConnectionState.Open)
            MyConnection.Open();

        var cmdTest = new SqlCommand();

        cmdTest.Connection = MyConnection;
        cmdTest.CommandText = SqlQuery;
        cmdTest.ExecuteNonQuery();
    }

    public void DBMyInsert(String SqlQuery)
    {
        DBMyUpdate(SqlQuery);
    }

** Login.aspx.cs: **

                    MyClass.DBMyReader("SELECT util_codi,util_logi,util_nome FROM Tgep_util WHERE util_logi='"
                            + Session["username"].ToString() + "'");
            MyClass.MyReader.Read();

            if (!MyClass.MyReader.HasRows)
            {
                MyClass.MyReader.Close();
                MyClass.DBMyInsert("INSERT INTO Tgep_util(util_logi,util_nome) "
                            + "VALUES ('" + Session["username"].ToString() + "','" + Session["nome"].ToString() + "')");
            }

            MyClass.DBMyReader("SELECT util_codi,util_logi,util_nome FROM Tgep_util WHERE util_logi='"
            + Session["username"].ToString() + "'");

            MyClass.MyReader.Read();

            Session["user_id"] = MyClass.MyReader["util_codi"].ToString();

            Response.Redirect("FRM_Principal.aspx");

Edit : Update Code (Works for now)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

空心空情空意 2024-11-21 20:40:24

该错误的含义正如其所言。您已加载 SQLCommand 并开始读取行,现在正尝试执行插入。您需要先关闭该读取器,或使用新命令。

在 DBMyUpdate 函数中,您可以创建一个新命令:

public void DBMyUpdate(String SqlQuery)
{
    if (MyConnection.State != ConnectionState.Open)
        MyConnection.Open();
    var cmdUpdate = new SqlCommand();

    cmdUpdate.Connection = MyConnection;
    cmdUpdate.CommandText = SqlQuery;
    cmdUpdate.ExecuteNonQuery(CommandBehavior.CloseConnection);
}

编辑:根据对此答案的注释,它需要使用单独的连接,这似乎很奇怪/不正确。

The error means exactly what it says.. You have loaded a SQLCommand and started reading rows, and are now trying to do an insert. You need to close out that reader first, or use a new command.

in the DBMyUpdate function you could just create a new command:

public void DBMyUpdate(String SqlQuery)
{
    if (MyConnection.State != ConnectionState.Open)
        MyConnection.Open();
    var cmdUpdate = new SqlCommand();

    cmdUpdate.Connection = MyConnection;
    cmdUpdate.CommandText = SqlQuery;
    cmdUpdate.ExecuteNonQuery(CommandBehavior.CloseConnection);
}

edit: based on comments to this answer, it required using separate connections which seems odd/incorrect.

赢得她心 2024-11-21 20:40:24

您不能将用于插入的相同 DataReader 用于 insert/update。在将其他命令与 DataReader 关联之前,您必须先关闭 DataReader

You can't use the same DataReader for insert/update that you used for insertion. You have to close the DataReader first before associating some other command with the DataReader.

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