对JSON文件的修改 [英] Modifications to JSON file

查看:333
本文介绍了对JSON文件的修改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有json文件Users.json,我希望从文件中读取json对象并逐个存储到数据库中。

如何删除存储在数据库中的json对象。当对象没有保存在数据库中时,它不会从文件中删除。

i不知道删除json对象。





I have json file Users.json and I want read json object from file and store in to database one by one.
how to delete json object when it stored in database. when object not save in database then it not delete from file.
i have no idea about delete json object.


using (FileStream fs = new FileStream("user.json", FileMode.Open, FileAccess.Read))
                    using (StreamReader sr = new StreamReader(fs))
                    using (JsonTextReader reader = new JsonTextReader(sr))
                    {
                        while (reader.Read())
                        {
                            if (reader.TokenType == JsonToken.StartObject)
                            {
                                // Load each object from the stream and do something with it
                                JObject obj = JObject.Load(reader);
                                SqlCommand cmd = null;
		   con.Open();
                                string cb = "insert into user(userid,name) VALUES ('" + obj["userid"] + "','" + obj["name"] + "')";
                                cmd = new SqlCommand(cb);
                                cmd.Connection = con;
                                cmd.ExecuteNonQuery();
		   con.Close();
                            }
                        }
                    }

推荐答案

由于您使用SQL Server作为数据库,我会建议您阅读使用数据库和文件系统实现交易 [< a href =http://www.andrewthompson.co/2011/03/achieving-transactions-with-database.htmltarget =_ blanktitle =New Window> ^ ]。 br />


根据您想要去的兔子洞的距离,您还可以查看实现自己的资源管理器 [ ^ ]。
Since you are using SQL Server as database, I would suggest you read this article on Achieving Transactions with a Database and File System[^] for ideas.

Depending how far down the rabbit hole you want to go, you could also check out this article on Implement Your Own Resource Manager[^].


这篇关于对JSON文件的修改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆