我正在尝试从 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): Incorrect number of arguments for PROCEDURE stman.Users_SelectByEmailAndPassword; expected 2, got 0] MySql.Data.MySqlClient.MySqlStream.ReadPacket() +383 MySql.Data.MySqlClient.NativeDriver.GetResult(Int32& affectedRow, Int64& insertedId) +116 MySql.Data.MySqlClient.Driver.GetResult(Int32 statementId, Int32& affectedRows, Int64& insertedId) +54 MySql.Data.MySqlClient.Driver.NextResult(Int32 statementId, Boolean force) +145 MySql.Data.MySqlClient.MySqlDataReader.NextResult() +1258 MySql.Data.MySqlClient.MySqlCommand.ExecuteReader(CommandBehavior behavior) +2364 MySql.Data.MySqlClient.MySqlCommand.ExecuteDbDataReader(CommandBehavior behavior) +41 System.Data.Common.DbCommand.System.Data.IDbCommand.ExecuteReader(CommandBehavior behavior) +10 System.Data.Common.DbDataAdapter.FillInternal(DataSet dataset, DataTable[] datatables, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior) +140 System.Data.Common.DbDataAdapter.Fill(DataTable[] dataTables, Int32 startRecord, Int32 maxRecords, IDbCommand command, CommandBehavior behavior) +160 System.Data.Common.DbDataAdapter.Fill(DataTable dataTable) +108 stman.Database.GetDataTable() in d:\Development\stman\stman\App_Code\Database.cs:85 stman.LoginContext.DoLogin() in d:\Development\stman\stman\App_Code\LoginContext.cs:45 stman.Login.processLogin() in d:\Development\stman\stman\Login.aspx.cs:40 stman.Login.btnLogin_Click(Object sender, EventArgs e) in 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(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1724
我多年来一直像这样填充数据表,这是我第一次遇到这个错误。我不知道为什么会发生这种情况,也不知道如何解决。
谁能帮我弄清楚参数的去向吗?
编辑
回应雷南的评论:
参数名称相同。
查询文本(在本例中为存储过程的名称)被传递到 sql
实例化此类时的属性。
最后,我的 IDE 的配色方案就是 VS2012 深色主题
最佳答案
好吧,这不是我在代码中所做的事情的问题,这是我没有做的事情的问题。
虽然代码中没有真正的错误,但它没有指定 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/