简体   繁体   English

无法将类型字符串隐式转换为System.Collections.Generic.Dictionary <string,System.Collections.Generic.Dictionary><string,object> &gt;

[英]Cannot implicitly convert type string to System.Collections.Generic.Dictionary<string,System.Collections.Generic.Dictionary><string,object>>

I am getting an error near return errmsg(ex); 我在return errmsg(ex);附近遇到错误return errmsg(ex); :

cannot implicitly convert type string to system.collections.generic.dictionary> 无法将类型字符串隐式转换为system.collections.generic.dictionary>

Here is my code: 这是我的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Script.Serialization;
using System.Web.Script.Services;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.ComponentModel;


namespace Webservice
{
    /// <summary>
    /// Summary description for Service1
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class Service1 : System.Web.Services.WebService
    {
        public Service1()
        {
            //Uncomment the following line if using designed components 
            //InitializeComponent(); 
        }
        [WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        //public string GetEmployees(string SearchTerm) 
        private static Dictionary<string, Dictionary<string, object>> DatatableToDictionary(DataTable dt)
        {
            var cols = dt.Columns.Cast<DataColumn>();
            return dt.Rows.Cast<DataRow>()
                     .ToDictionary(r => dt.Rows.IndexOf(r).ToString(),
                                   r => cols.ToDictionary(c => c.ColumnName, c => r[c.ColumnName]));
        }
        public Dictionary<string, Dictionary<string, object>> GetEmployees()
        {
            try
            {
                SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["NSConstr"].ToString());
                con.Open();
                SqlCommand cmd = new SqlCommand();
                //cmd.CommandText = "SELECT *  FROM Contact e WHERE FirstName LIKE '%" + SearchTerm + "%'";
                cmd.CommandText = "SELECT *  FROM Contact e ";
                DataSet ds = new DataSet();
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                da.SelectCommand.Connection = con;
                da.Fill(ds);
                con.Close();

                return DatatableToDictionary(ds.Tables[0]);
            }
            catch (Exception ex)
            {
                ***return errmsg(ex);***

            }
        }

        public string errmsg(Exception ex)
        {
            return "[['ERROR','" + ex.Message + "']]";
        }
     }
 }

Thanks in advance. 提前致谢。

You cannot return a string from method with return type Dictionary<string, Dictionary<string, object>> 您不能从返回类型为Dictionary<string, Dictionary<string, object>>方法中返回Dictionary<string, Dictionary<string, object>>

public Dictionary<string, Dictionary<string, object>> GetEmployees()
{
    try
    {
        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["NSConstr"].ToString());
        con.Open();
        SqlCommand cmd = new SqlCommand();
        cmd.CommandText = "SELECT *  FROM Contact e ";
        DataSet ds = new DataSet();
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        da.SelectCommand.Connection = con;
        da.Fill(ds);
        con.Close();

        return DatatableToDictionary(ds.Tables[0]);
    }
    catch (Exception ex)
    {
        // Here you are returning a string, the method signature is Dictionary<string, Dictionary<string, object>>
        return errmsg(ex);
    }
}

Instead of catching the Exception and returning the exception message, let the calling code handle the exception further up the call stack. 与其捕获Exception并返回异常消息,不如让调用代码在调用堆栈中进一步处理异常。 So you'd have something like: 所以你会有类似的东西:

public Dictionary<string, Dictionary<string, object>> GetEmployees()
{                    
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["NSConstr"].ToString());
    con.Open();
    SqlCommand cmd = new SqlCommand();
    cmd.CommandText = "SELECT *  FROM Contact e ";
    DataSet ds = new DataSet();
    SqlDataAdapter da = new SqlDataAdapter(cmd);
    da.SelectCommand.Connection = con;
    da.Fill(ds);
    con.Close();
    return DatatableToDictionary(ds.Tables[0]);
}

The following code solve my issues: 以下代码解决了我的问题:

public string GetEmployees()
        {
            System.Web.Script.Serialization.JavaScriptSerializer serializer = new System.Web.Script.Serialization.JavaScriptSerializer();

            SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["NSConstr"].ToString());
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = "SELECT *  FROM Contact e ";
            DataSet ds = new DataSet();
            DataTable dt = new DataTable();
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            da.SelectCommand.Connection = con;
            da.Fill(dt);
            con.Close();

            List<Dictionary<string, object>> rows = new List<Dictionary<string, object>>();
            Dictionary<string, object> row = null;
            foreach (DataRow rs in dt.Rows)
            {
                row = new Dictionary<string, object>();
                foreach (DataColumn col in dt.Columns)
                {
                    row.Add(col.ColumnName, rs[col]);
                }
                rows.Add(row);
            }
            return serializer.Serialize(rows);
        }     

        public string errmsg(Exception ex)
        {
            return "[['ERROR','" + ex.Message + "']]";
        }

暂无
暂无

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

相关问题 fb 事件代码统一参数 2:无法从 'System.Collections.Generic.Dictionary 转换<string, object> ' 浮?'</string,> - fb event code unity Argument 2: cannot convert from 'System.Collections.Generic.Dictionary<string, object>' to 'float?' 无法从“ System.Threading.Tasks.Task”转换为“ System.Collections.Generic.Dictionary” <string,string> &#39; - Cannot convert from 'System.Threading.Tasks.Task' to 'System.Collections.Generic.Dictionary<string,string>' 枚举System.Collections.Generic.Dictionary中的键<string,string> - enumerate keys in in a System.Collections.Generic.Dictionary<string,string> 无法从 &#39;void&#39; 转换为 &#39;System.Collections.Generic.Dictionary<string, bool> &#39; - Cannot convert from 'void' to 'System.Collections.Generic.Dictionary<string, bool>' &#39;System.Collections.Generic.Dictionary的最佳重载方法匹配 <int,System.Collections.Generic.Dictionary<string,int> &gt; .Dictionary(int)&#39; - The best overloaded method match for 'System.Collections.Generic.Dictionary<int,System.Collections.Generic.Dictionary<string,int>>.Dictionary(int)' 无法将System.String强制转换为System.Collections.Generic.Dictionary类型 <string, string> 与会话变量 - Unable to cast System.String to type System.Collections.Generic.Dictionary<string, string> with Session variable Firebase 数据库错误:System.Collections.Generic.Dictionary`2[System.String,System.Object] - Firebase Database Error: System.Collections.Generic.Dictionary`2[System.String,System.Object] 无法将“System.Collections.Hashtable”转换为“System.Collections.Generic.Dictionary” - Cannot convert “System.Collections.Hashtable” to type "System.Collections.Generic.Dictionary System.Collections.Generic.Dictionary =终极表现? - System.Collections.Generic.Dictionary = Ultimate performance? System.Collections.Generic.Dictionary foreach顺序 - System.Collections.Generic.Dictionary foreach order
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM