【问题标题】:Insert dynamic JSON data into Database将动态 JSON 数据插入数据库
【发布时间】:2019-10-22 10:12:03
【问题描述】:

我想将 JSON 数据插入到 sql server 表中,我通过 URL 检索我的 json 数据,并且我已经通过明显的错误 {"Must declare the scalar variable \"@Name\"."} 在按钮单击事件上编写了我的插入逻辑,因为我没有提供任何用于插入数据的参数,因为我可以不传递那些硬编码的参数

private string HttpContent(string url)
        {
            WebRequest objRequest = System.Net.HttpWebRequest.Create(url);
            StreamReader sr = new StreamReader(objRequest.GetResponse().GetResponseStream());
            string result = sr.ReadToEnd();
            sr.Close();
            return result;
        }
        protected void btnsave_Click(object sender, EventArgs e)
        {
            var conString = ConfigurationManager.ConnectionStrings["SQLDBConnection"];
            string strConnString = conString.ConnectionString;
            SqlConnection conn = new SqlConnection(strConnString);
            SqlCommand com;
            string data = HttpContent(txturl.Text);
                JavaScriptSerializer jsSerializer = new JavaScriptSerializer();
                var table = jsSerializer.Deserialize<dynamic>(data);
                conn.Open();
                com = new SqlCommand("INSERT INTO tbl_insert_json (Name, Mobile, Email) VALUES(@Name, @Mobile, @Email)", conn);
                com.CommandType = CommandType.Text;
                int refId = com.ExecuteNonQuery();
                conn.Close();
                if (refId > 0)
                {
                    Response.Write("{\"response\":{\"status\":\"success\",\"msg\":\"Details Saved Successfully..\"}}");
                }
                else
                {
                    Response.Write("{\"response\":{\"status\":\"fail\",\"msg\":\"oops!! something went wrong\"}}");
                }
            }

现在我的问题是我应该改变什么或者如何将我的数据插入到 sql server 表中

我使用每个循环来映射每一行然后插入但对我不起作用请分享将数据插入表的任何想法或方法?我在这部分遇到问题com = new SqlCommand("INSERT INTO tbl_insert_json (Name, Mobile, Email) VALUES(@Name, @Mobile, @Email)", conn); 如何将这些参数插入到表中

【问题讨论】:

  • 在共享代码中,您没有替换参数“@Name”、“@Mobile”、“@Email”
  • @AhmedMsaouri 是的,我知道,正如我提到的,我没有通过添加名称移动和电子邮件的值,因为我没有直接在我的页面上插入这些值,我想插入数据库是否可以读取我的 json 数据并插入数据库??

标签: c# asp.net json serialization


【解决方案1】:

必须在SqlCommand对象的实例行之后添加参数以将参数替换为json值,例如:

    com = new SqlCommand("INSERT INTO tbl_insert_json (Name, Mobile, Email) VALUES(@Name, @Mobile, @Email)", conn);
    com.Parameters.Add("@Name", SqlDbType.NChar);
    com.Parameters["@Name"].Value = "NameDataJson";

    com.Parameters.Add("@Mobile", SqlDbType.NChar);
    com.Parameters["@Mobile"].Value = "MobileDataJson";

    com.Parameters.Add("@Email", SqlDbType.NChar);
    com.Parameters["@Email"].Value = "EmailDataJson";

【讨论】:

  • sqlDbType 不包含字符串的定义
  • 必须使用 System.Data 导入库;或使用 System.Data.SqlDbType.String
  • 必须导入库 System.Data.SqlClient.dll 和 System.Data.dll
  • System.Data.dll 已安装,但 System.Data.SqlClient.dll 不存在,甚至我在程序集中搜索但力求找到
  • 但是如果你使用 SqlConnection 你必须导入 System.Data.SqlClient !!!!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-03-25
  • 1970-01-01
  • 1970-01-01
  • 2019-04-01
  • 2019-10-25
  • 1970-01-01
  • 2019-04-14
相关资源
最近更新 更多