我必须将HeidiSql
中存在的一个数据库连接到Visual Studio中的.Net项目。当我与数据库进行连接时调试项目时,我在字段Server Version
中获得了错误,描述为“ .server Version生成了类型System.InvalidOperationException
的异常”。你知道是什么原因吗?
我的连接字符串是:
<connectionStrings>
<add name="TicketDB"
connectionString="server=localhost;user id=root;password=Omega;database=crm6"
providerName="MySql.Data.MySqlClient" />
</connectionStrings>
代码是:
public void getNumberAllTicketOpen()
{
try
{
clDataBase ticketDB = new clDataBase();
string sQuery = "SELECT * from ticket";
DataTable ticket = ticketDB.ExecuteDataTable(sQuery, null, false);
Console.WriteLine("i am here!");
}
catch (Exception e)
{
Console.WriteLine("Errore : " + e.Message);
}
finally
{
}
}
public DataTable ExecuteDataTable(string CommandText, List<SqlParameter> Params = null, bool isStoredProcedure = true)
{
SqlConnection oCnn = null;
SqlCommand oCmd = null;
SqlDataAdapter oAdap = null;
try
{
**oCnn = new SqlConnection(this.connectionString);
oCnn.Open();** //this is the line that launch the error
oCmd = new SqlCommand(CommandText, oCnn);
if (isStoredProcedure)
oCmd.CommandType = System.Data.CommandType.StoredProcedure;
else
oCmd.CommandType = System.Data.CommandType.Text;
oCmd.CommandTimeout = this.commandTimeOut;
if (Params != null)
{
foreach (SqlParameter p in Params)
oCmd.Parameters.Add(p);
}
oAdap = new SqlDataAdapter(oCmd);
DataTable ResultTable = new DataTable();
oAdap.Fill(ResultTable);
return ResultTable;
}
catch (Exception ex)
{
logger.Error("Errore esecuzione " + CommandText, ex, MethodInfo.GetCurrentMethod().Name + " in " + MethodInfo.GetCurrentMethod().Module.Assembly.FullName);
throw;
}
}
oCnn = new SqlConnection(this.connectionString); oCnn.Open(); //this is the line with error 谢谢您的帮助!
答案 0 :(得分:0)
您显示的错误是Visual Studio调试器尝试查看SqlConnection
对象的属性时遇到的错误。
有问题的属性是ServerVersion
。我猜该连接仅在它实际设法连接到服务器时才知道服务器上正在运行哪个版本的MySQL,但是在此时,您已经停止了代码,但它尚未连接。当时返回服务器版本没有意义,因此抛出InvalidOperationException
是适当的响应。
我经常在VS调试器中看到这样的异常。他们没什么好担心的。并非每个对象上的每个属性始终都是有效的。
答案 1 :(得分:0)
我已经解决了我的问题。 我试图连接到mySQL数据库,但是我的代码中有错误。我已经解决了使用NuGet安装软件包MySql.Data的问题,并分别使用MysqlConnection,MySqlCommand和MySqlDataAdapter替换了SqlConnection,SqlCommand,SqlDataAdapter的代码。下面我写修改后的代码:
<script>
var datags = <?!= JSON.stringify(data) ?>;
console.log(datags);
</script>
<!DOCTYPE html>
<html>
<head>
<meta charset ="utf-8">
<meta name="viewport" content="width=device-width initial-scale=1, shrink-to-fit=no">
<base target="_top">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
<style>
td {
font-style: 10px;
}
</style>
</head>
<body>
<div class="container">
<div id="flashMessage"></div>
<div id="output"></div>
</div>
<script src = "https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script>
$(function () {
var row = '<table class="table">';
for(var i=0;i < (datags.length); i++){
if(i != 0 ){
row+='<tr>';
}
else {
row+='<thead class ="thead-dark"><tr>';
}
for(var j=0;j < datags[i].length; j++){
if(datags[i][j]){
if(i != 0 ){
row=row+'<td ><input data-row ="'+i+'" data-col="'+j+'" type="text" value="'+datags[i][j]+'" class="dtValue"></td>'
}
else {
row=row+'<th>'+datags[i][j]+'</th>';
}
}
}
if(i != 0 ){
row+='</tr>';
}
else {
row+='</thead></tr>';
}
}
$('#output').html(row);
$('#output').on('change','.dtValue',function(){
console.log(this.dataset.row, this.dataset.col, this.value);
google.script.run.withSuccessHandler(onSuccess)
.sheetUpdate(this.dataset.row, this.dataset.col, this.value);
});
})
function onSuccess(data) {
$('#flashMessage').html('<div class="alert alert-success" role="alert">'+data.info+'</div>');
}
</script>
</body>
</html>