错误:不允许使用默认参数说明符

本文关键字:默认 参数 说明符 不允许 错误 | 更新日期: 2023-09-27 18:09:35

在第一行第二行字符串SqlOutputParameterName = "MSG"—near =我得到这个错误。我使用的是3.5框架。如何解决这个

 public static DataTable JobsGetAll()
 {
      //SqlParameter[] CmdParamColl = new SqlParameter[1];
      string StoredProcedure = "GetJobsAll";
      //DataConnectSql.DataConnectParametersAdd(ref CmdParamColl, "@WebName", SqlDbType.VarChar, ParameterDirection.Input, ApplicationName, 0, 500);
      return DataConnectSql.DataConnectResultDataTable(CommandType.StoredProcedure, StoredProcedure);
 } 
 public static DataTable DataConnectResultDataTable(CommandType CmdType, string CmdText)
 {
    SqlCommand CmdExec = new SqlCommand();
    DataTable DtResult = new DataTable();
    try
    {
        CmdExec.Connection = DataConnectionOpen();
        CmdExec.CommandType = CmdType;
        CmdExec.CommandText = CmdText;
        SqlDataAdapter DapResult = new SqlDataAdapter(CmdExec);
        DapResult.Fill(DtResult);
    }
    catch (Exception)
    {
        DtResult = null;
        //throw;
    }
    finally { CmdExec.Connection.Close(); }
    return DtResult;
}

错误:不允许使用默认参数说明符

您在执行查询之前没有指定输出参数。
在您的代码中添加以下行,在try块之前:

CmdExec.Parameters.Add("@" + SqlOutputParameterName, SqlDbType.VarChar).Direction = ParameterDirection.Output;
在上下文:

public static string DataConnectExecuteNonQuery(CommandType CmdType, string CmdText, string SqlOutputParameterName = "Msg")
{
    SqlCommand CmdExec = new SqlCommand();
    CmdExec.Connection = DataConnectionOpen();
    CmdExec.CommandType = CmdType;
    CmdExec.CommandText = CmdText;
    CmdExec.Parameters.Add("@" + SqlOutputParameterName, SqlDbType.VarChar).Direction = ParameterDirection.Output; //** HERE **//
    string msg;
    try
    {
         CmdExec.ExecuteNonQuery();
         msg = CmdExec.Parameters["@" + SqlOutputParameterName].Value.ToString();
    }
}