[英]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.