繁体   English   中英

从KeyValuePair获取值

[英]Get a value from KeyValuePair

通过jQuery调用此方法,并填充一个下拉框。 在我需要向下拉文本中添加一些其他信息之前,它一直有效。 我正在尝试将ID传递到第二个存储的proc中,并将该值放在KeyValuePair中的第一个值旁边。 我不确定如何获取该ID。 有任何想法吗?

   [WebMethod]
//public static Dictionary<string, string> LoadRestByCityState(string city, string state)
public static List<KeyValuePair<string, string>> LoadRestByCityState(string city, string state)
{
    DataSet ds = new DataSet();
    Database db = DatabaseFactory.CreateDatabase(ConfigManager.AppSettings["ConnectionString.Data"]);
    DbCommand dbCommand = db.GetStoredProcCommand("sel_RestByCityState_p");
    db.AddInParameter(dbCommand, "@pCity", DbType.String, city);
    db.AddInParameter(dbCommand, "@pState", DbType.String, state);
    ds = db.ExecuteDataSet(dbCommand);

    KeyValuePair<string, string> parks = new KeyValuePair<string, string>();

    DataSet dset = new DataSet();

    var list = new List<KeyValuePair<string, string>>();

    list.Add(new KeyValuePair<string, string>("Select a restaurant", "0"));
    foreach (DataRow row in ds.Tables[0].Rows)
    {
        DbCommand comm = db.GetStoredProcCommand("dbo.sel_RestDetailsByID_p");
        db.AddInParameter(comm, "@pRestId", DbType.Int32, //keyvalueid );
        db.AddInParameter(comm, "@pAttributeCode", DbType.String, "Detail");
        dset = db.ExecuteDataSet(comm);

        string attr = ds.Tables[0].Rows[0]["AttributeValue"].ToString();

        list.Add(new KeyValuePair<string, string>(row[0].ToString() + " - " + attr, row[1].ToString()));

    }

    return list;

}

目前还不清楚您的问题是什么,但我认为这条线

string attr = ds.Tables[0].Rows[0]["AttributeValue"].ToString();

应该读

string attr = dset.Tables[0].Rows[0]["AttributeValue"].ToString();

您正在查看外部数据集,而不是从存储过程返回的数据集。

有什么帮助吗?

另外,调用存储过程时尚未创建KVP,因此只需执行此操作

db.AddInParameter(comm, "@pRestId", DbType.Int32, row[0]); // or row[1], it isn't clear to me which you want to use.

尝试这样可能会对您有所帮助。 您正在通过Web方法返回List时,Web服务会转换为数组。 因此最好返回Array,以便可以使用ToArray()轻松地对其进行序列化; Web服务和清单

  1. 使用字典,它将与您的KeyValuePair一起使用

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM