简体   繁体   English

C#asp.net MySQL值返回为Null

[英]C# asp.net MySQL Values return as Null

Communicating with my asp.net keeps returning a null value... I'm actually not sure why. 与我的asp.net通信不断返回空值...我实际上不确定为什么。

I've used Post Man to push information back to it to see the result excluding the use of the WPF to ensure it's my API. 我已经使用Post Man将信息推回​​去查看结果,不包括使用WPF以确保它是我的API。 The only result I can return is the result I am searching for... IE BanID returns BanID All others return a null value. 我可以返回的唯一结果是我正在搜索的结果... IE BanID返回BanID所有其他返回null值。

I've Used this method for other stuff on the same API and it works fine... Somewhere I made a typo. 我已经将这种方法用于同一API上的其他东西,它工作得很好......某处我犯了一个错字。

This is for a search box on a C# asp.net communicating with a WPF to MySQL 这是针对与WPF通信的C#asp.net上的搜索框

    [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=123";
            MySqlConnection conDataBase = new MySqlConnection(dbConnection);
            MySqlCommand dbSearch;
            conDataBase.Open();
            MySqlDataReader dbReader;
            string selectQuery = "Select * FROM a3bans.bans WHERE BanID=" + int.Parse(ban.BanID);

            dbSearch = new MySqlCommand(selectQuery, conDataBase);
            dbReader = dbSearch.ExecuteReader();

            if (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.Equals(searchBan);
            }
            dbReader.Close();  
            return bans.GuidOrIP;  
        }

If BanID is PK then only one row will be returned 如果BanID是PK,则只返回一行

If there are multiple rows for one BanID, then you need to iterate dbReader
while(dbReader.Read())
{
}

Finally check your return statement. 最后检查你的退货声明。

Change 更改

bans.Equals(searchBan);

To

bans = searchBan;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM