对象类型 c# 不存在映射

No mapping exists from object type c#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Services;
using System.Data;
using System.Data.Sql;
using System.Data.SqlClient;
using System.IO;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    [WebMethod]
    public static string SaveLayout(string layout, object state)
    {



        SqlConnection conn = new SqlConnection(@"Persist Security Info=False;User ID=admin;Password=admin123;Initial Catalog=Test_Layout;Data Source=Myserver");
        conn.Open();
        SqlCommand cmd = new SqlCommand("insertlayout", conn);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("@layout", layout);
        cmd.Parameters.AddWithValue("@states", state);

        cmd.ExecuteNonQuery();
        conn.Close();
        return "successfully state saved";
    }
}

我正在使用 web 方法(使用 Json,jquery) 在 sql 服务器 2008 中存储对象 但是我在 sql 服务器 2008 上收到以下错误我正在使用 varchar(max) 数据类型来存储 object(state variable)数据库 如何将状态变量(对象)转换为字符串 以存储在数据库中

问题是您必须传递字符串而不是字典。我可能会建议您序列化您的字典并将该值传递给 AddWithValue 函数。让 JSON 成为序列化的格式。我会推荐 Json.net as serialization library. It provides convenient way of dictionary serialization/deserialization

因此您的代码可能如下所示:

cmd.Parameters.AddWithValue("@states", JsonConvert.SerializeObject(state));

P.S。更好地对 SqlConnectionSqlCommand 对象使用 using 语句。

如果您想存储任何对象,请在 sqlserver 中使用 VARBINARY(MAX),例如

sqlCmd.Parameters.Add("@states", SqlDbType.VarBinary, Int32.MaxValue);

sqlCmd.Parameters["@states"].Value = state;

这里state将是你的对象