从ASP输入到MySQL.净NULL值
本文关键字:NULL MySQL ASP 输入 | 更新日期: 2023-09-27 18:15:58
所以我试图从asp.net的输入返回一个值。然而问题是,我一直收到null
值。但是如果我输入字符串,就好像它是通过查询插入的一样,它工作得很好。
为什么我得到一个空值?
This Works:
[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 `BanID`, `GUID`, `BanTime`, `Reason`, `BanType`, `Proof` FROM `a3bans`.`bans` WHERE `GUID`= 'e7af78997ef220a557c97a1a4c11e0c2'", conDataBase); //@prmGuid, conDataBase); // Returning a null value?!
// selectCommand.Parameters.AddWithValue("@prmGuid", new tID(ban.GuidOrIP));
conDataBase.Open();
dbReader = selectCommand.ExecuteReader();
try
{
while (dbReader.Read())
{
tBan searchBan = new tBan();
searchBan.BanID = dbReader.GetString("BanID");
searchBan.GuidOrIP = dbReader.GetString("GUID");
searchBan.BanTime = dbReader.GetString("BanTime");
searchBan.BanType = dbReader.GetString("BanType");
searchBan.BanReason = dbReader.GetString("Reason");
searchBan.BanType = dbReader.GetString("BanType");
searchBan.Proof = dbReader.GetString("Proof");
bans = searchBan;
}
}
catch (Exception ex)
{
}
finally
{
dbReader.Close();
conDataBase.Close();
}
return bans.Proof;
}
}
然而,一旦我试图从另一个来源输入它,它是空的。
所以我的问题在这里:
MySqlCommand selectCommand = new MySqlCommand("SELECT `BanID`, `GUID`, `BanTime`, `Reason`, `BanType`, `Proof` FROM `a3bans`.`bans` WHERE `GUID`= '@prmGuid'", conDataBase);
selectCommand.Parameters.AddWithValue("@prmGuid", new tID(ban.GuidOrIP));
带构造函数的类这可能是完全错误的。tID
namespace A3Bans.Schemas
{
public class tID
{
public tID(string guidOrIP)
{
GuidOrIP = guidOrIP;
}
public string GuidOrIP { get; set; }
public string BanID { get; set; }
}
}
如果我不得不猜它是我的构造函数…任何想法吗?
编辑让它部分工作,它只是返回错误的值。
MySqlConnection con = new MySqlConnection(dbConnection);
MySqlCommand cmd;
con.Open();
MySqlDataReader dbReader;
string cmdText = "SELECT `BanID`, `GUID`, `BanTime`, `Reason`, `BanType`, `Proof` FROM `a3bans`.`bans` WHERE `GUID` LIKE @pGUID";
cmd = new MySqlCommand(cmdText, con);
cmd.Parameters.AddWithValue("@pGUID", "%" + bans.GuidOrIP + "%");
不要将参数占位符放在单引号之间。这样,它就变成了一个字面值字符串,您的查询搜索列GUID的值,该值等于字面值字符串'@prmGuid',当然,在此条件下不会返回任何内容。
只需将sql命令文本更改为
..... WHERE `GUID`= @prmGuid", conDataBase);