asp.net到MySql的问题。试图从输入中返回信息

问题描述 投票:1回答:1

当我尝试使用asp.net(例如e7af78997ef220a557c97a1a4c11e0c2)调用字符串时,返回总是返回null。我知道它不是空的(所有ID都是那种格式或IP格式)。它在我输入int列时有效。

所以我的问题是返回始终为null,Visual Studio说

“PresentationFramework.dll中发生了'System.FormatException'类型的未处理异常

附加信息:输入字符串的格式不正确。“

我知道它与MySqlCommand SelectCommand = new MySqlCommand("select * from a3bans.bans where GUID = '" + ban.GuidOrIP + "' ; ", conDataBase);有关,我几乎肯定它与它有关

'" + ban.GuidOrIP + "' ; ", conDataBase);`

我想我已经尝试了一百万种方法,并且要么得到语法错误,要么得到Null

我认为这个问题与数据库输入而不是输出有关。输入为“select”我只是使用常规查询输入ID,然而它填充了信息。当试图使用我的tban时({getter; Setter;}它只返回Null ...

我研究过的所有内容都引用了一个带有.Text的按钮,似乎没有任何东西可以帮助一个getter和setter。

        [HttpPost]
        [Route("A3Bans/searchBan")]
        public string oSearchBan(tBan ban)
        {
            {
                tBan bans = new tBan();
                string dbConnection = "datasource=127.0.0.1;port=3306;username=admin;password=00000";
                MySqlConnection conDataBase = new MySqlConnection(dbConnection);
                MySqlDataReader dbReader;
                MySqlCommand SelectCommand = new MySqlCommand("select * from a3bans.bans where GUID = '" + ban.GuidOrIP + "' ; ", conDataBase); // Returning a null value?!

                conDataBase.Open();
                dbReader = SelectCommand.ExecuteReader();
                while (dbReader.Read())
                {
                    tBan searchBan = new tBan();
                    searchBan.GuidOrIP = dbReader.GetString("GUID");
                    searchBan.BanType = dbReader.GetString("BanType");
                    searchBan.BanReason = dbReader.GetString("Reason");
                    searchBan.Proof = dbReader.GetString("Proof");
                    bans = searchBan;
                }
                dbReader.Close();
                return bans.Proof;
            }
        }

如果我用字符串selectQuery = "Select * FROM a3bans.bans WHERE BanID=" + int.Parse(schBanID.Text);替换查询字符串它将从客户端返回正确的值。如果我要将它翻译成API,它会返回正确的值。但它是一个int而不是一个字符串。

更新:我使用的只适用于int值而不是字符串。

        [HttpPost]
        [Route("A3Bans/searchBan")]
        public string oSearchBan(tBan ban)
        {
            {
                tBan bans = new tBan();
                string dbConnection = "datasource=127.0.0.1;port=3306;username=admin;password=0000";
                MySqlConnection conDataBase = new MySqlConnection(dbConnection);
                MySqlDataReader dbReader;
                conDataBase.Open();

                // Returns a null value from bans.Proof
                // If placing a /w real GUID without null fields, rather than "ban.GuidOrIP" , the return populates properly. 
                // and works with int
                // Either Null or Invalid Syntax...
                MySqlCommand selectCommand = new MySqlCommand("SELECT * FROM a3bans.bans WHERE (BanID) LIKE (@BanID)", conDataBase);
                selectCommand.Parameters.AddWithValue("@BanID", ban.BanID);
                selectCommand.ExecuteNonQuery()

这适用于int ...但仍然无法使用字符串...我该怎么做才能让它与文本/数字字符串一起使用?

c# mysql asp.net return parameter-passing
1个回答
1
投票
[HttpPost]
        [Route("A3Bans/searchBan")]
        public string oSearchBan(tBan ban)
        {
            {
                tBan bans = new tBan();
                string dbConnection = "datasource=127.0.0.1;port=3306;username=admin;password=00000";
                MySqlConnection conDataBase = new MySqlConnection(dbConnection);
                MySqlDataReader dbReader;

//<Changes>
                MySqlCommand SelectCommand = new MySqlCommand("select * from a3bans.bans where [GUID] =@prmGuid  ", conDataBase); // Returning a null value?!

                selectCommand.Parameters.AddWithValue("@prmGuid", new Guid(ban.GuidOrIP));

//</Changes end>
                conDataBase.Open();
                dbReader = SelectCommand.ExecuteReader();
                while (dbReader.Read())
                {
                    tBan searchBan = new tBan();
                    searchBan.GuidOrIP = dbReader.GetString("GUID");
                    searchBan.BanType = dbReader.GetString("BanType");
                    searchBan.BanReason = dbReader.GetString("Reason");
                    searchBan.Proof = dbReader.GetString("Proof");
                    bans = searchBan;
                }
                dbReader.Close();
                return bans.Proof;


            }
        }
© www.soinside.com 2019 - 2024. All rights reserved.