ai didi

c# - 查询在 phpMyAdmin 中工作正常,但在应用程序中则不行(C#)

转载 作者:行者123 更新时间:2023-11-29 09:05:59 24 4
gpt4 key购买 nike

SO 和其他地方有一些类似的问题,但主要是关于 php 的,我不明白。我正在尝试恢复具有 62 个表的数据库,如下所示:

string query = @"SET SQL_MODE= 'NO_AUTO_VALUE_ON_ZERO'; CREATE DATABASE " + dbName + " DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci; USE " + dbName + ";" + Environment.NewLine;

using (StreamReader reader = File.OpenText("C:\b.sql"))
{
string line = reader.ReadToEnd();
query += line; //almost 1700 lines.
}
// upto this i get the query correctly which works fine in phpMyAdmin.

MySqlCommand c = new MySqlCommand(query, conn);
c.ExecuteReader();
//but when I execute, throws: "Fatal error encountered during command execution."

为什么会这样呢?如果它是查询长度的余弦,那么我如何从应用程序执行如此大的查询?

最佳答案

尝试检查错误:

List<string> query = new List<string>(){
"SET SQL_MODE= 'NO_AUTO_VALUE_ON_ZERO';",
string.Format("CREATE DATABASE `{0}` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;", dbName),
string.Format("USE `{0}`;", dbName)}; // error string.Format("USE `{0}`;{1}", dbName)
/*
using (StreamReader reader = File.OpenText("C:\b.sql"))
{
while (reader.Peek() >= 0)
query.Add(reader.ReadLine());
}
*/

using (StreamReader reader = File.OpenText("C:\b.sql"))
{
string lines = reader.ReadToEnd();
string[] alines = lines.Split(';');
foreach(string q in alines)
query.Add(q);
}

foreach (string command in query)
{
try
{
using (MySqlCommand c = new MySqlCommand(command, conn))
{
c.ExecuteReader();
Console.WriteLine(string.Format("OK Command: {0}", command));
}
}
catch (Exception ex)
{
Console.WriteLine(string.Format("Error: {0}. Command: {1}", ex.Message, command));
break;
}
}

编辑

为了获得更好的性能,您可以使用此类。我没有尝试过,希望效果很好:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MySql.Data.MySqlClient;
using System.IO;

namespace MySQLHelperTest
{
public class MySQLTestingQuery
{
public MySqlConnection MyConnection { get; set; }
public string FileSql { get; set; }
public List<string> PreviousQuerys { get; set; }
public List<string> CorrectQuerys { get; private set; }
public string ErrorQuery { get; private set; }

public MySQLTestingQuery()
{
this.CorrectQuerys = new List<string>();
this.ErrorQuery = string.Empty;
}

public void Start()
{
FileInfo file = new FileInfo(this.FileSql);
if (!file.Exists)
throw new ApplicationException(string.Format("nonexistent file: '{0}'", this.FileSql));

if (this.PreviousQuerys != null)
foreach (string command in this.PreviousQuerys)
this.RunMySQLCommand(command);

try
{
foreach (string command in this.ReadQuerys(this.FileSql))
Console.WriteLine(command);
}
catch (ApplicationException ex)
{
throw ex;
}
catch (Exception ex)
{
throw new ApplicationException(string.Format("an unexpected error happened: {0}. ", ex.Message));
}

}

private void RunMySQLCommand(string command)
{
try
{
using (MySqlCommand c = new MySqlCommand(command, this.MyConnection))
{
c.ExecuteReader();
this.CorrectQuerys.Add(command);
}
}
catch (Exception ex)
{
this.ErrorQuery = command;
throw new ApplicationException(string.Format("error: {0}. command: {1}", ex.Message, command));
}
}

private IEnumerable<string> ReadQuerys(string file)
{
using (StreamReader sr = new StreamReader(file))
{
string query = string.Empty;
while (sr.Peek() >= 0)
{
query += (char)sr.Read();
if (query.EndsWith(";"))
{
yield return query;
query = string.Empty;
}
}
}
}

}
}

关于c# - 查询在 phpMyAdmin 中工作正常,但在应用程序中则不行(C#),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7040252/

24 4 0
文章推荐: php - MySQL 后续数字
文章推荐: bash - 用于 bash 的 jdoc/pydoc/rdoc?
文章推荐: bash - 为半可移植 shell 脚本制作弹出菜单的最佳(?)方法?
文章推荐: java - 动态报告 : Showing error in compilation with NetBeans
行者123
个人简介

我是一名优秀的程序员,十分优秀!

滴滴打车优惠券免费领取
滴滴打车优惠券
全站热门文章
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com