C# 没有给出与所需的形式参数-.NET Error相对应的参数
我一直在重新分解我的一个旧MSSQL连接帮助程序库,但出现以下错误: 严重性代码说明项目文件行错误CS7036没有 与所需形式参数相对应的给定参数 ErrorEventArg.ErrorEventArg(字符串, 字符串)'MSSQLTest C:\Users\Administrator\Desktop\MSSQLTest\MSSQLTest\MSSQLConnection.cs 61 这是我目前的代码: MSSQLConnection.csC# 没有给出与所需的形式参数-.NET Error相对应的参数,c#,.net,.net-4.5,C#,.net,.net 4.5,我一直在重新分解我的一个旧MSSQL连接帮助程序库,但出现以下错误: 严重性代码说明项目文件行错误CS7036没有 与所需形式参数相对应的给定参数 ErrorEventArg.ErrorEventArg(字符串, 字符串)'MSSQLTest C:\Users\Administrator\Desktop\MSSQLTest\MSSQLTest\MSSQLConnection.cs 61 这是我目前的代码: MSSQLConnection.cs using System; using System
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Threading;
namespace MSSQLTest
{
public class ErrorEventArg : EventArgs
{
public string ErrorMsg { get; set; }
public string LastQuery { get; set; }
public ErrorEventArg(string errorMsg, string lastQuery)
{
ErrorMsg = errorMsg;
LastQuery = lastQuery;
}
}
public class MSSQLConnection
{
/// <summary>
/// Private class objects.
/// </summary>
private SqlConnection sqlConnection;
private int sqlCommandTimeout;
private string lastQuery = string.Empty;
/// <summary>
/// Public event related objects & handler.
/// </summary>
public event ErrorHandler OnError;
public delegate void ErrorHandler(MSSQLConnection sender, ErrorEventArg e);
/// <summary>
/// Class constructor.
/// </summary>
/// <param name="sqlConnection"></param>
/// <param name="sqlCommandTimeout"></param>
public MSSQLConnection(SqlConnection sqlConnection, Int32 sqlCommandTimeout = 120)
{
if (null == sqlConnection)
throw new Exception("Invalid MSSQL Database Conection Handle");
if (sqlConnection.State != System.Data.ConnectionState.Open)
throw new Exception("MSSQL Database Connection Is Not Open");
this.sqlConnection = sqlConnection;
this.sqlCommandTimeout = sqlCommandTimeout;
}
/// <summary>
/// Helper method to emit a database error to event subscribers.
/// </summary>
/// <param name="errorMsg"></param>
internal void EmitError(String errorMsg)
{
var errorDelegate = OnError;
if (errorDelegate != null)
{
errorDelegate(this, new ErrorEventArg() // Line #61
{
ErrorMsg = errorMsg,
LastQuery = lastQuery
});
}
}
/// rest of the code snipped
}
}
使用系统;
使用System.Collections.Generic;
使用System.Data.SqlClient;
使用系统线程;
名称空间MSSQL测试
{
公共类ErrorEventArg:EventArgs
{
公共字符串ErrorMsg{get;set;}
公共字符串LastQuery{get;set;}
public ErrorEventArg(字符串errorMsg,字符串lastQuery)
{
ErrorMsg=ErrorMsg;
LastQuery=LastQuery;
}
}
公共类MSSQL连接
{
///
///私有类对象。
///
专用SqlConnection-SqlConnection;
私有int-sqlCommandTimeout;
私有字符串lastQuery=string.Empty;
///
///公共事件相关对象和处理程序。
///
公共事件错误处理程序OnError;
公共委托无效错误处理程序(MSSQL连接发送方,ErrorEventArg e);
///
///类构造函数。
///
///
///
公共MSSQLConnection(SqlConnection SqlConnection,Int32 sqlCommandTimeout=120)
{
if(null==sqlConnection)
抛出新异常(“无效的MSSQL数据库连接句柄”);
if(sqlConnection.State!=System.Data.ConnectionState.Open)
抛出新异常(“MSSQL数据库连接未打开”);
this.sqlConnection=sqlConnection;
this.sqlCommandTimeout=sqlCommandTimeout;
}
///
///帮助器方法向事件订阅服务器发出数据库错误。
///
///
内部无效错误(字符串错误消息)
{
var errorDelegate=OnError;
if(errorDelegate!=null)
{
errorDelegate(这是新的ErrorEventArg()//行#61
{
ErrorMsg=ErrorMsg,
LastQuery=LastQuery
});
}
}
///其余的代码被剪掉了
}
}
这个错误意味着什么&如何修复它?我以前没有见过这个错误…您有一个构造函数,它接受两个参数。你应该这样写:
new ErrorEventArg(errorMsv, lastQuery)
它的代码更少,更容易阅读
编辑
或者,为了您的工作方式,您可以尝试为ErrorEventArg编写一个没有参数的默认构造函数,如下所示:
public ErrorEventArg() {}
当构造函数所需的一个属性不是公共属性时,我出现了这个错误。如果出现这种情况,请确保构造函数中的所有参数都指向公共属性: 使用语句 名称空间名称空间
public class ExampleClass {
//Properties - one is not visible to the class calling the constructor
public string Property1 { get; set; }
string Property2 { get; set; }
//Constructor
public ExampleClass(string property1, string property2)
{
this.Property1 = property1;
this.Property2 = property2; //this caused that error for me
}
}
在
public class ErrorEventArg : EventArgs
您必须添加“基本”,如下所示:
public ErrorEventArg(string errorMsg, string lastQuery) : base (string errorMsg, string lastQuery)
{
ErrorMsg = errorMsg;
LastQuery = lastQuery;
}
这为我解决了这个问题,我得到了同样的错误,但这是因为我没有创建默认构造函数。如果您还没有尝试过,请按如下方式创建默认构造函数:
public ErrorEventArg() {}
公共测试类()
{
}我在以下关于DailReport的Linq声明中收到了相同的错误。问题是DailyReport没有默认构造函数。显然,它在填充属性之前实例化了对象
var sums = reports
.GroupBy(r => r.CountryRegion)
.Select(cr => new DailyReport
{
CountryRegion = cr.Key,
ProvinceState = "All",
RecordDate = cr.First().RecordDate,
Confirmed = cr.Sum(c => c.Confirmed),
Recovered = cr.Sum(c => c.Recovered),
Deaths = cr.Sum(c => c.Deaths)
});
你有一个2参数的构造函数,为什么不使用它呢,也就是说,
newerroreventarg(errorMsg,lastQuery)
?@pm100 chill-man,我大约在三年半前问过这个问题,大约在我加入时问过这个问题。很明显,当时我没有11k rep,也没有像现在这样理解C#/通用编程。另外,我主要是一名PHP开发人员,我“涉猎”了C#,这不是我的专长。