C#'dbConn.ServerVersion'引发了类型'System.InvalidOperationException'

时间:2018-10-25 14:32:51

标签: c# sql

发生错误“ dbConn.ServerVersion”引发了类型为“ System.InvalidOperationException”的异常,但是VisualStudio不会暂停程序并将异常抛出给我。这是代码:private void BTN_NA_Click(对象发送者,EventArgs e)         {

        if (TXT_NAUN.Text != "" && TXT_NAPW.Text != "" && TXT_NAPW2.Text != "")
        {
            if (TXT_NAPW.Text == TXT_NAPW2.Text) 
            {
                string input = TXT_NAPW.Text;
                int hash = 0;
                int output = 0;
                foreach (char chara in input)
                {
                    int temp = 0;
                    temp = System.Convert.ToInt32(chara);
                    output = output + temp;
                    output = output * 2;
                }
                hash = output % 1000;
                OleDbConnection dbConn = new System.Data.OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=BHShooterProjectDB.accdb");
                string sql = "SELECT Accounts.PlayerID FROM Accounts ORDER BY Accounts.PlayerID DESC "; 
                                                                                                                                                                                                                                                                          ///string comm = "SELECT Accounts.PlayerID from Accounts";
                                                                                                                                                                                                                                                                          /// INNER JOIN Leaderboard ON Leaderboard.PlayerID = Accounts.PlayerID WHERE Accounts.PlayerUsername = @ip";
                OleDbCommand cmd = new OleDbCommand(sql, dbConn);
                string result = "";
                dbConn.Open();
                OleDbDataReader reader = cmd.ExecuteReader();
                while (reader.Read())
                {
                    result = reader[0].ToString(); 
                }
                dbConn.Close();

                {
                    string comm = "INSERT INTO Accounts (PlayerUsername, PlayerPassword, PlayerID, PlayerInvID) VALUES (@NAUN, @HPW, @NAPI, @NAPI)";
                    OleDbCommand command = new OleDbCommand(comm, dbConn);
                    command.Parameters.AddWithValue("@NAUN", TXT_NAUN.Text);
                    command.Parameters.AddWithValue("@HPW", hash);
                    foreach (char chara in result)
                    {
                        int temp = 0;
                        temp = System.Convert.ToInt32(chara);
                        result = result + temp;
                    }
                        result = result + 1;
                        command.Parameters.AddWithValue("@NAPI", result);
                        command.Parameters.AddWithValue("@NAPI", result);
                        dbConn.Open();
                        int rowsAffected = cmd.ExecuteNonQuery(); ///error appears here
                        dbConn.Close();
                    }
            }
        }

    }

关于解决方案的任何建议,已经尝试了很多,这是我最后的希望!

谢谢

黑爪_

1 个答案:

答案 0 :(得分:0)

在发生错误的那一行,您正在使用cmd(select命令)。我认为您想使用命令(插入命令)。