我正在尝试从DataAdapter填充数据表,但是数据库不断报告未为查询指定任何参数...

这是我的代码:

    #region "Properties"
    public string sql { get; protected set; }
    public CommandType cType { get; protected set; }
    public List<MySqlParameter> args { get; protected set; }
    public string rValue { get; protected set; }

    private MySqlConnection conn;

    private string server = "127.0.0.1";
    private string database = "stman";
    private string user = "root";
    private string password = "root";
    #endregion

    #region "Constructor Logic"
        public Database(string CommandText, CommandType CommandType, List<MySqlParameter> Parameters, string ReturnParameter = "")
        {
            buildConnection();

            sql = CommandText;
            cType = CommandType;
            args = Parameters;
            rValue = ReturnParameter;
        }

        public Database(string CommandText, CommandType CommandType)
        {
            buildConnection();

            sql = CommandText;
            cType = CommandType;
        }

        private void buildConnection()
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(String.Format("data source={0}; initial catalog={1}; user id={2}; password={3};", server, database, user, password));

            conn = new MySqlConnection(sb.ToString());
        }
    #endregion

    public DataTable GetDataTable()
    {
        DataTable dt = new DataTable();
        MySqlDataAdapter da = new MySqlDataAdapter();

        using (MySqlCommand cmd = new MySqlCommand(sql, conn))
        {
            if (args.Count > 0)
            {
                cmd.Parameters.AddRange(args.ToArray());
            }

            da.SelectCommand = cmd;
            da.Fill(dt);
        }

        if (dt.Rows.Count > 0)
        {
            return dt;
        }
        else
        {
            return null;
        }
    }


GetDataTable函数在List<MySqlParameter>中读取查询所需的参数-此列表在调用代码中定义。

在命中da.Fill(dt);时,MySqlCommand确实具有用于所查询查询的正确参数,并且通过堆栈跟踪判断(请参见下文),看来从mysql获取数据的尝试确实通过了。实际上是mysql发回此错误:



这是堆栈跟踪:


  [MySqlException(0x80004005):PROCEDURE stman.Users_SelectByEmailAndPassword的参数数目不正确;预期2,得到0]
     MySql.Data.MySqlClient.MySqlStream.ReadPacket()+383
     MySql.Data.MySqlClient.NativeDriver.GetResult(Int32&受影响的行,Int64&insertId)+116
     MySql.Data.MySqlClient.Driver.GetResult(Int32 statementId,Int32&受影响的行,Int64&insertId)+54
     MySql.Data.MySqlClient.Driver.NextResult(Int32 statementId,布尔强制)+145
     MySql.Data.MySqlClient.MySqlDataReader.NextResult()+1258
     MySql.Data.MySqlClient.MySqlCommand.ExecuteReader(CommandBehavior行为)+2364
     MySql.Data.MySqlClient.MySqlCommand.ExecuteDbDataReader(CommandBehavior行为)+41
     System.Data.Common.DbCommand.System.Data.IDbCommand.ExecuteReader(CommandBehavior行为)+10
     System.Data.Common.DbDataAdapter.FillInternal(DataSet数据集,DataTable []数据表,Int32 startRecord,Int32 maxRecords,字符串srcTable,IDbCommand命令,CommandBehavior行为)+140
     System.Data.Common.DbDataAdapter.Fill(DataTable [] dataTables,Int32 startRecord,Int32 maxRecords,IDbCommand命令,CommandBehavior行为)+160
     System.Data.Common.DbDataAdapter.Fill(数据表dataTable)+108
     d:\ Development \ stman \ stman \ App_Code \ Database.cs中的stman.Database.GetDataTable():85
     d:\ Development \ stman \ stman \ App_Code \ LoginContext.cs中的stman.LoginContext.DoLogin():45
     d:\ Development \ stman \ stman \ Login.aspx.cs中的stman.Login.processLogin():40
     stman.Login.btnLogin_Click(Object sender,EventArgs e)在d:\ Development \ stman \ stman \ Login.aspx.cs:32中
     System.Web.UI.WebControls.Button.OnClick(EventArgs e)+9553594
     System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument)+103
     System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)+10
     System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl,String eventArgument)+13
     System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)+35
     System.Web.UI.Page.ProcessRequestMain(布尔值includeStagesBeforeAsyncPoint,布尔值includeStagesAfterAsyncPoint)+1724


我多年来一直在填充这样的DataTables,这是我第一次遇到此错误。我不知道为什么会这样或如何解决它。

谁能帮我弄清楚参数的去向?

编辑
针对Renan的评论:

参数名称相同。
实例化此类时,查询文本(在这种情况下为存储过程的名称)将传递到sql属性中。
最后,我的IDE的配色方案只是VS2012 Dark主题

最佳答案

好的,这与我在代码中所做的事情无关,而与我未做的事情有关。

尽管代码中没有真正的错误,但是它没有为MySqlCommand指定CommandType。

我通过添加以下行来修复此问题(指定对象使用的CommandType):

cmd.CommandType = cType;


这是完整的代码:

#region "Properties"
public string sql { get; protected set; }
public CommandType cType { get; protected set; }
public List<MySqlParameter> args { get; protected set; }
public string rValue { get; protected set; }

private MySqlConnection conn;

private string server = "127.0.0.1";
private string database = "stman";
private string user = "root";
private string password = "root";
#endregion

#region "Constructor Logic"
    public Database(string CommandText, CommandType CommandType, List<MySqlParameter> Parameters, string ReturnParameter = "")
    {
        buildConnection();

        sql = CommandText;
        cType = CommandType;
        args = Parameters;
        rValue = ReturnParameter;
    }

    public Database(string CommandText, CommandType CommandType)
    {
        buildConnection();

        sql = CommandText;
        cType = CommandType;
    }

    private void buildConnection()
    {
        StringBuilder sb = new StringBuilder();
        sb.Append(String.Format("data source={0}; initial catalog={1}; user id={2}; password={3};", server, database, user, password));

        conn = new MySqlConnection(sb.ToString());
    }
#endregion

public DataTable GetDataTable()
{
    DataTable dt = new DataTable();
    MySqlDataAdapter da = new MySqlDataAdapter();

    using (MySqlCommand cmd = new MySqlCommand(sql, conn))
    {
        if (args.Count > 0)
        {
            cmd.Parameters.AddRange(args.ToArray());
        }

        cmd.CommandType = cType;

        da.SelectCommand = cmd;
        da.Fill(dt);
    }

    if (dt.Rows.Count > 0)
    {
        return dt;
    }
    else
    {
        return null;
    }
}

关于c# - DataAdapter Fill没有从Command对象获取任何参数,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/17089405/

10-12 14:47