gpt4 book ai didi

c# - MySQL ODBC 存储过程结果缺少列

转载 作者:可可西里 更新时间:2023-11-01 07:38:12 24 4
gpt4 key购买 nike

我有一组用于填充 ASP.Net CheckBoxList 的存储过程。从代码运行此过程时 { CALL ProcedureName(params); } 将类型设置为存储过程,我似乎只得到了部分结果(即实际结果中的许多列都丢失了。)

如果我从查询中复制 CommandText(使用断点来获取发送的确切文本)并直接在 Navicat(或任何其他 MySQL GUI)中运行它,我会得到所有预期的列。

这是不起作用的代码:

using (OdbcCommand command = OdbcConnection.CreateCommand())
{
command.CommandType = CommandType.StoredProcedure;
command.CommandText = "{ " + string.Format(StoredProcedureCall, foundationId, fpids, "", "", "NULL", "2001/01/02", "2001/01/01", "*") +
" }";
using (OdbcDataReader reader = command.ExecuteReader())
{

for (int i = 0; i < reader.FieldCount; i++)
{
columns.Add(reader.GetName(i));
}
}
}

如果我将代码更改为以下但它开始工作(仅添加其他使用):

using (OdbcConnection)
using (OdbcCommand command = OdbcConnection.CreateCommand())
{
command.CommandType = CommandType.StoredProcedure;
command.CommandText = "{ " + string.Format(StoredProcedureCall, foundationId, fpids, "", "", "NULL", "2001/01/02", "2001/01/01", "*") +
" }";
using (OdbcDataReader reader = command.ExecuteReader())
{

for (int i = 0; i < reader.FieldCount; i++)
{
columns.Add(reader.GetName(i));
}
}
}

这是怎么回事?

此处供引用的是 OdbcConnection 属性:

public static OdbcConnection OdbcConnection
{
get
{
// If we have no connection or our connection isn't open
if (null == odbcConnection || ConnectionState.Open != odbcConnection.State)
{
odbcConnection = new OdbcConnection(BaseAccess.DBConnectionString);
odbcConnection.Open();
}

return odbcConnection;
}
}

最佳答案

这可能是 Odbc MySql 驱动程序中的错误。尝试使用 ADO.NET driver .另外,我建议您不要在静态属性中手动处理连接。将此任务留给 ADO.NET,它将有效地处理连接池:

using (var conn = new MySqlConnection(DBConnectionString))
using (var cmd = conn.CreateCommand())
{
conn.Open();
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "sp_name";

cmd.Parameters.Add(new SqlParameter("@foundationId", foundationId));
cmd.Parameters.Add(new SqlParameter("@fpids", fpids));
...

using (var reader = cmd.ExecuteReader())
{
for (int i = 0; i < reader.FieldCount; i++)
{
columns.Add(reader.GetName(i));
}
}
}

关于c# - MySQL ODBC 存储过程结果缺少列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1574665/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com