首页新闻找找看学习计划

反序列化后导入SQLserver

0
悬赏园豆:15 [已解决问题] 解决于 2017-03-14 17:12

现在一个文件夹里有很多压缩文件,我需要先解压,然后读取解压后的txt文本内容,文本内容已经是序列化后的格式,现在需要反序列化并导入数据库,然后删除txt文本,下面是我目前的代码

private void button4_Click(object sender, EventArgs e)
{
Person p = new Person();
try
{
if (txtfiles2.Text.Trim() != "")
{
toolStripProgressBar1.Maximum = files2.Length;
for (int i = 0; i < files2.Length; i++)
{
toolStripProgressBar1.Value = i;
string path = files2[i].ToString();
string newpath = path.Remove(path.LastIndexOf("\\") + 1);
UnZip(path, newpath);

string str = null;
using (StreamReader sr = new StreamReader(@"E:\国省道采集数据\Command.txt", System.Text.Encoding.GetEncoding("utf-8")))
{
str = sr.ReadToEnd();
}
var xuliehua = str;
//StreamReader sr = new StreamReader(@"E:\国省道采集数据\Command.txt", Encoding.GetEncoding("gb2312"));
//MessageBox.Show(sr.ToString());


}

toolStripProgressBar1.Value = 0;
MessageBox.Show("解压缩成功!");
}
else
{
MessageBox.Show("警告:请选择要进行批量解压缩的文件!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
catch { }

}

c#
问题补充:

这是txt文本里的内容

{"UserID":"huangshan1@78","BatchID":"1489281487936","CityCode":"341000","CustomFields":[{"Code":"GLDWMC","Value":"徽州交通局"},{"Code":"QLMC","Value":"杨村2号桥"},{"Code":"QLBH","Value":"K3001"},{"Code":"JCSJ","Value":""},{"Code":"QLCD","Value":"134"},{"Code":"SJHZ","Value":"1"},{"Code":"AKJF","Value":"2"},{"Code":"AJCHNXF","Value":"1"},{"Code":"B_ISDANGER","Value":"2"},{"Code":"KJZC","Value":"125"},{"Code":"DKZDKJ","Value":"25"},{"Code":"QMQK","Value":"8.5"},{"Code":"QMJK","Value":"7.4"},{"Code":"LXBM","Value":"S103341000"},{"Code":"LDXLH","Value":"001"}],"Images":[{"Time":"Mar 12, 2017 12:00:00 AM","FileName":"/storage/emulated/0/easyroad/collectionpo/1489282287821.jpeg","Lon":118.1775853,"Lat":29.97436336,"Alt":0.0}],"Time":"2017-03-12","LineName":"S103341000","Note":"","PO_Code":"T_ql","Pile":"5.092","Version":2,"Lat":29.97436336,"Lon":118.1775853,"Alt":0.0,"Type":4}

姚刘乐的主页 姚刘乐 | 初学一级 | 园豆:70
提问于:2017-03-14 16:05
< >
分享
最佳答案
0

这段json数据全部存入到DB? 

个人建议直接反序列化成实体类,然后再存入DB. 如果是存入不同的表中,并且这些表有关连关系,建议写个存储过程,使用事务提交,try catch 捕捉下异常.

CustomFields这个是对应DB的一个字典表吗?

收获园豆:15
~扎克伯格 | 小虾三级 |园豆:1799 | 2017-03-14 16:22

对,是要反序列化成实体类,比如我现在有个实体类

[DataContract]
public class Person
{
[DataMember]
public string UserID { get; set; }
}

姚刘乐 | 园豆:70 (初学一级) | 2017-03-14 16:41

@姚刘乐: 你实体类定义好了吗?反序列化就一句话的事,这段json是复杂类型,主要要看你的实体类是怎么定义的了

~扎克伯格 | 园豆:1799 (小虾三级) | 2017-03-14 16:45

@~扎克伯格: 定义好了

姚刘乐 | 园豆:70 (初学一级) | 2017-03-14 16:46

@姚刘乐: 贴出来,编辑器右上角有插入代码工具,不要直接贴上来

~扎克伯格 | 园豆:1799 (小虾三级) | 2017-03-14 16:48

@姚刘乐: 

  public class JsonHelper
    {
        public static List<T> JsonToList<T>(string jsonStr)
        {
            JavaScriptSerializer js = new JavaScriptSerializer();
            List<T> t = (List<T>)js.Deserialize(jsonStr, typeof(List<T>));

            return t;
                                                                
        }

        public static T JsonToEntity<T>(string jsonStr)
        {
            JavaScriptSerializer js = new JavaScriptSerializer();
            T t = (T)js.Deserialize(jsonStr, typeof(T));

            return t;
        }
    }

这个json help类先拿去玩玩吧!有问题再说

~扎克伯格 | 园豆:1799 (小虾三级) | 2017-03-14 16:51
  1 using System;
  2 using System.Collections.Generic;
  3 using System.ComponentModel;
  4 using System.Data;
  5 using System.Drawing;
  6 using System.Linq;
  7 using System.Text;
  8 using System.Windows.Forms;
  9 using System.Collections;
 10 using ICSharpCode.SharpZipLib;
 11 using ICSharpCode.SharpZipLib.Zip;
 12 using ICSharpCode.SharpZipLib.Checksums;
 13 using System.IO;
 14 using System.Runtime.Serialization;
 15 using System.Text.RegularExpressions;
 16 
 17 namespace BatchDecompression
 18 {
 19     public partial class Form1 : Form
 20     {
 21         public Form1()
 22         {
 23             InitializeComponent();
 24         }
 25 
 26      
 27     
 28      
 29 
 30   
 31         /// <summary>
 32         /// 解压文件
 33         /// </summary>
 34         /// <param name="FileToUpZip">待解压的文件</param>
 35         /// <param name="ZipedFolder">指定解压目标目录</param>
 36         public void UnZip(string FileToUpZip, string ZipedFolder)
 37         {
 38             if (!File.Exists(FileToUpZip))
 39             {
 40                 return;
 41             }
 42             if (!Directory.Exists(ZipedFolder))
 43             {
 44                 Directory.CreateDirectory(ZipedFolder);
 45             }
 46             ZipInputStream ZIPStream = null;
 47             ZipEntry theEntry = null;
 48             string fileName;
 49             FileStream streamWriter = null;
 50             try
 51             {
 52                 //生成一个GZipInputStream流,用来打开压缩文件
 53                 ZIPStream = new ZipInputStream(File.OpenRead(FileToUpZip));
 54                 while ((theEntry = ZIPStream.GetNextEntry()) != null)
 55                 {
 56                     if (theEntry.Name != String.Empty)
 57                     {
 58                         fileName = Path.Combine(ZipedFolder, theEntry.Name);
 59                         //判断文件路径是否是文件夹
 60                         if (fileName.EndsWith("/") || fileName.EndsWith("\\"))
 61                         {
 62                             Directory.CreateDirectory(fileName);
 63                             continue;
 64                         }
 65                         //生成一个文件流,它用来生成解压文件
 66                         streamWriter = File.Create(fileName);
 67                         int size = 2048;//指定压缩块的大小,一般为2048的倍数
 68                         byte[] data = new byte[2048];//指定缓冲区的大小
 69                         while (true)
 70                         {
 71                             size = ZIPStream.Read(data, 0, data.Length);//读入一个压缩块
 72                             if (size > 0)
 73                             {
 74                                 streamWriter.Write(data, 0, size);//写入解压文件代表的文件流
 75                             }
 76                             else
 77                             {
 78                                 break;//若读到压缩文件尾,则结束 
 79                             }
 80                         }
 81                     }
 82                 }
 83             }
 84             finally
 85             {
 86                 if (streamWriter != null)
 87                 {
 88                     streamWriter.Close();
 89                     streamWriter = null;
 90                 }
 91                 if (theEntry != null)
 92                 {
 93                     theEntry = null;
 94                 }
 95                 if (ZIPStream != null)
 96                 {
 97                     ZIPStream.Close();
 98                     ZIPStream = null;
 99                 }
100                 GC.Collect();
101                 GC.Collect(1);
102             }
103         }
104      
105 
106     
107         string[] files2;//存储要进行解压缩的文件数组
108       
109 
110         private void button3_Click(object sender, EventArgs e)//选择批量解压缩的文件
111         {
112             if (openFileDialog2.ShowDialog() == DialogResult.OK)
113             {
114                 files2 = openFileDialog2.FileNames;
115                 string file = "";
116                 for (int i = 0; i < files2.Length; i++)
117                 {
118                     file += files2[i].ToString() + ",";
119                 }
120                 file = file.Remove(file.LastIndexOf(","));
121                 txtfiles2.Text = file;
122             }
123         }
124 
125  
126 
127         private void button4_Click(object sender, EventArgs e)
128         {
129             Person p = new Person();
130             try
131             {
132                 if (txtfiles2.Text.Trim() != "")
133                 {
134                     toolStripProgressBar1.Maximum = files2.Length;
135                     for (int i = 0; i < files2.Length; i++)
136                     {
137                         toolStripProgressBar1.Value = i;
138                         string path = files2[i].ToString();
139                         string newpath = path.Remove(path.LastIndexOf("\\") + 1);
140                         UnZip(path, newpath);
141 
142                         string str = null;
143                         using (StreamReader sr = new StreamReader(@"E:\国省道采集数据\Command.txt", System.Text.Encoding.GetEncoding("utf-8")))
144                         {
145                             str = sr.ReadToEnd();
146                             
147 
148                         }
149 
150                         var xuliehua = str;
151                  
152                         //StreamReader sr = new StreamReader(@"E:\国省道采集数据\Command.txt", Encoding.GetEncoding("gb2312"));
153                         //MessageBox.Show(sr.ToString());
154                         
155                    
156                     }
157 
158                     toolStripProgressBar1.Value = 0;
159                     MessageBox.Show("解压缩成功!");
160                 }
161                 else
162                 {
163                     MessageBox.Show("警告:请选择要进行批量解压缩的文件!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Error);
164                 }
165             }
166             catch { }
167                 
168         }
169 
170         private void txtfiles2_TextChanged(object sender, EventArgs e)
171         {
172 
173         }
174          [DataContract]
175     public class Person
176     {
177         [DataMember]
178         public string UserID { get; set; }
179     }
180      
181     }
182 
183 }
姚刘乐 | 园豆:70 (初学一级) | 2017-03-14 16:52

@~扎克伯格: 额好的,我试试哈,谢谢

姚刘乐 | 园豆:70 (初学一级) | 2017-03-14 16:52

@姚刘乐:

 [DataContract]
    public class Person
    {
        [DataMember]
        public string UserID { get; set; }
    }

这是实体类,其他的多余的.

 你觉得你的实体类定义的对吗?难道你只打算存UserUD?

~扎克伯格 | 园豆:1799 (小虾三级) | 2017-03-14 16:53

@~扎克伯格: 我想先存个试试能不能成功,成功了其他的就好弄了嘛

姚刘乐 | 园豆:70 (初学一级) | 2017-03-14 16:54

@姚刘乐: 那你试试呗!看会不会报错.

~扎克伯格 | 园豆:1799 (小虾三级) | 2017-03-14 16:55

@~扎克伯格: 额,我先试试这个helpr类怎么用,对反序列化基本不知道是啥....有啥例子供参考不~

姚刘乐 | 园豆:70 (初学一级) | 2017-03-14 16:57

@姚刘乐: 你加我qq:2375287790 吧!这里说不方便,每次把框口拉到底回答你

~扎克伯格 | 园豆:1799 (小虾三级) | 2017-03-14 17:02

@~扎克伯格: 加了

姚刘乐 | 园豆:70 (初学一级) | 2017-03-14 17:03
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册