gpt4 book ai didi

C# - 无法将数据插入 MySQL 数据库

转载 作者:搜寻专家 更新时间:2023-10-30 19:55:16 26 4
gpt4 key购买 nike

我正在制作一个应用程序,用于抓取某个网站。该网站有很多玩家,我抓取了他们每个人的个人资料页面。他们的个人资料页面包含姓名、级别、世界和上次登录日期等信息。

所以我做了一个对象叫做Player。然后我将他们所有的数据添加到列表中。

public static List<Player> Players = new List<Player> { };

public class Player
{
public string Name { get; set; }
public string Sex { get; set; }
public string Vocation { get; set; }
public int Level { get; set; }
public int Achievements { get; set; }
public string World { get; set; }
public string LastLogin { get; set; }
public string AccountStatus { get; set; }
public int OnlineStatus { get; set; }
}

我这样添加数据:

new Player { Name = playerName, Sex = playerSex, Vocation = playerVocation, Level = playerLevel, Achievements = playerAchievements, World = playerWorld, LastLogin = playerLastLogin, AccountStatus = playerAccountStatus, OnlineStatus = playerOnlineStatus };

我现在想将所有玩家添加到我的 MySQL 数据库中,但我似乎无法理解如何插入数据。

我建立了一个连接并尝试选择数据并且它工作正常。所以我的连接还不错。但是插入好像不行。

这是我添加的用于将数据插入表中的代码:

string connString = "Server=localhost;Port=3306;Database=rookstat;Uid=XXXXXXX;password=XXXXXX;";
MySqlConnection conn = new MySqlConnection(connString);
MySqlCommand command = conn.CreateCommand();

foreach (Player rooker in Players)
{
command.CommandText = "Insert into rookstayers (id, name, sex, vocation, level, achievements, world, lastlogin, accountstatus, onlinestatus) values('', rooker.Name, rooker.Sex, rooker.Vocation, rooker.Level, rooker.Achievements, rooker.World, rooker.LastLogin, rooker.AccountStatus, rooker.OnlineStatus)";
conn.Open();
command.ExecuteNonQuery();
}

conn.Close();

我做错了什么?我不确定我插入的值。它应该是 rooker.Name 还是什么?

最佳答案

构建查询的方式会创建一个字符串,并将变量名称视为文字字符串。

相反,您需要这样的参数化查询

string connString = "Server=localhost;Port=3306;Database=rookstat;Uid=XXXXXXX;password=XXXXXX;";
using(MySqlConnection conn = new MySqlConnection(connString))
using(MySqlCommand command = conn.CreateCommand())
{
conn.Open();
command.CommandText = @"Insert into rookstayers
(id, name, sex, vocation, level, achievements,
world, lastlogin, accountstatus, onlinestatus)
values('', @name, @sex, @vocation, @level, @Achievements,
@World, @LastLogin, @AccountStatus, @OnlineStatus)";

command.Parameters.Add("@name", MySqlDbType.VarChar);
command.Parameters.Add("@sex", MySqlDbType.VarChar);
command.Parameters.Add("@vocation", MySqlDbType.VarChar);
command.Parameters.Add("@level", MySqlDbType.Int32);
.... and so on for all the parameters placeholders

command.Prepare();
foreach (Player rooker in Players)
{
command.Parameters["@name"].Value = rooker.Name;
command.Parameters["@sex"].Value = rooker.Sex;
command.Parameters["@vocation"].Value = rooker.Vocation;
command.Parameters["@level"].Value = rooker.Level;

... and so on for the other parameters defined
command.ExecuteNonQuery();
}
}

这样,在进入循环之前,您定义了 sql 命令并在 MySqlCommand Parameters 集合中创建参数而不设置值。并且,正如 Mr 在下面建议的那样,添加对 command.Prepare 的调用可以提高性能。

在循环内,对于定义的每个参数,您可以设置它的值并调用命令来执行。

我还想指出确保正确关闭和处理对象的 using 语句,以及使用为每个参数设置正确参数类型的 Add 而不是 AddWithValue。 AddWithValue 是一个有许多缺点的快捷方式,您可以在本文中阅读 Can we stop using AddWithValue already?

最后的建议,如果对象和数据之间的这种转换经常发生,那么您应该花一些时间来学习使用像 Dapper 这样的 ORM 工具。

编辑:查看您的整个代码,您似乎错过了抓取过程中的一个重要步骤。您需要将播放器添加到您的播放器列表中

private async void insertTimer_Tick(object sender, EventArgs e)
{
if (onlineList.Items.Count > 0)
{
for (int i=0; i<onlineList.Items.Count; i++)
{
CurrentPlayer = onlineList.Items[i].ToString();
var playerProfile = await GetData();
foreach (var row in playerProfile)
{
.... gets player data ....
}

// Creates a new Player and add to the insertion list
Players.Add(new Player
{
Name = playerName,
Sex = playerSex,
Vocation = playerVocation,
Level = playerLevel,
Achievements = playerAchievements,
World = playerWorld,
LastLogin = playerLastLogin,
AccountStatus = playerAccountStatus,
OnlineStatus = playerOnlineStatus
};
}

// And now the inserts goes here
.......

关于C# - 无法将数据插入 MySQL 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30174207/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com