【发布时间】:2011-10-29 18:40:35
【问题描述】:
我在使用 ExecuteNonQuery() 时遇到致命错误。
我是使用 sql 的新手,因此我不确定为什么这个 SET 命令会导致问题。
我使用的是 MySql 5.5.17,我在 VS2010 IDE 中使用的是 C# .Net Framework 3.5。
异常消息 = 命令执行期间遇到致命错误。 错误行 = SET @oldguid = 250006;
我使用的 sql 文件中的内容如下减去我删除的“cmets”:
DELETE FROM `disables` WHERE `sourceType`=0 AND `entry`=61904;
INSERT INTO `disables` (`sourceType`, `entry`, `flags`, `comment`) VALUES(0, 61904, 8, 'Magma Totem TEST - can crash client by spawning too many totems');
SET @oldguid = 250006;
SET @newguid = 202602;
UPDATE `creature` SET `guid`=@newguid WHERE `guid`=@oldguid;
UPDATE `creature_addon` SET `guid`=@newguid, `path_id`=@newguid*100 WHERE `guid`=@oldguid;
UPDATE `waypoint_data` SET `id`=@newguid*100 WHERE `id`=@oldguid*100;
UPDATE `areatrigger_teleport` SET `target_orientation`=2.255664 WHERE `id`=4386;
UPDATE `gameobject` SET `spawnMask`=3 WHERE `guid`=22674;
guid 列是无符号整数 (10)
我用来处理这个 .sql 文件的 C# 代码如下:
filename = lstBox_SqlFiles.SelectedItem.ToString();
mysql = new MySqlConnection(connection + Database);
string line;
OpenDatabase();
using (StreamReader reader = new StreamReader(filename))
{
StringBuilder parsedLine = new StringBuilder();
int count = 0;
while ((line = reader.ReadLine()) != null)
{
if (line.Length > 0 && !line.StartsWith("--"))
{
if (line.EndsWith(";"))
{
if (parsedLine.Length > 0)
line = parsedLine.ToString() + line;
try
{
count++;
lbl_LineQuery.Text = line;
lbl_QueryCount.Text = String.Format("Count: {0}", count);
MySqlCommand cmd = new MySqlCommand(line, mysql);
cmd.ExecuteNonQuery();
}
catch (MySqlException ex)
{
string msg = String.Format("Source FileName: SqlUpdater.cs\nSql FileName: {0}\nError Line: {1}\nException Message: {2}\n", filename, line, ex.Message);
MessageBox.Show("cmd.ExecuteNonQuery() Error!\n" + msg, "MySql Error", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
return;
}
sbClear(parsedLine);
}
else
{
parsedLine.Append(line);
continue;
}
}
else
continue;
}
}
谁能看到我的代码有问题?我需要对“SET @var”字符串进行一些特殊操作吗?
任何帮助表示赞赏 提前致谢
路人
- 编辑 * 作为旁注我应该指出,如果我使用像 SQLYog 这样的 sql 管理程序,它会毫无问题地处理相同的 sql 文件,所以我假设问题出在我的 C# 代码中的字符串操作中.
【问题讨论】:
-
奇怪。在 mysql 监视器中手动运行时,设置的行完美运行。也许 C# mysql 库有问题。如果是这样的话,也许另一个
select @oldguid := 250006;会绕过它。 -
等一下,C# 代码是单独运行每一行的吗?
-
您应该尝试逐步运行您的代码并检查每个变量的值以检测最终错误。另外,sbClear 函数有什么作用??
-
我发布了一个解决方案,它有一个错误,我编辑了答案,现在可以了
-
GianT971 sbClear 函数只是一个自定义函数,用于清除 StringBuilder 以供重用。感谢您的帮助...