通过datatable把文件导入到数据库表

来源:互联网 发布:php 单引号 引用变量 编辑:程序博客网 时间:2024/06/15 21:44

 private void ImportTemplate()
        {
            SaveFileDialog sfd = new SaveFileDialog();
            try
            {
            EMRTemplateClient etc = Factory.getEMRTemplate();
            Service.WEIZOSERVER.TEM_FILE_TEXT model = new TEM_FILE_TEXT();
            FolderBrowserDialog fbd = new FolderBrowserDialog();
            if (fbd.ShowDialog() == DialogResult.OK)
            {
                string filePath = fbd.SelectedPath;
                DirectoryInfo dic = new DirectoryInfo(filePath);
                 int items=0;//插入多少行
                foreach (FileInfo fi in dic.GetFiles())
                {
                    if (fi.Extension == ".emr")
                    {
                        string fileName = fi.Name;
                        int index = fileName.IndexOf('.');
                        if (index > 0)
                        {
                            FileStream fs = new FileStream(filePath + "\\" + fileName, FileMode.Open);
                            byte[] buffer = new byte[fs.Length];
                            fs.Read(buffer, 0, buffer.Length);
                            fs.Seek(0, SeekOrigin.Begin);
                            model.FILE_TEXT = buffer;
                            fs.Close();
                            fileName = fileName.Remove(index);
                            model.TEM_CODE = fileName;
                            items = etc.Add_TEM_FILE_TEXT(model);
                            items++;
                        }
                    }
                    if (fi.Extension=="")
                    {
                        TEM_FILE_INDEX index = new TEM_FILE_INDEX();
                        string fileName = fi.Name;
                        using (StreamReader sr = new StreamReader(filePath + "\\" + fileName,Encoding.Default ))
                        {
                            String line;
                            while ((line = sr.ReadLine()) != null)
                            {
                                string[] cell = line.Split('|');
                                index.TEM_CODE = cell[0];
                                index.DS_ID = cell[1];
                                index.TEM_CLASS = cell[2];
                                index.P_CODE = cell[3];
                                index.TEM_NAME = cell[4];
                                index.RANGE_ID = cell[5];
                                index.RANGE_NAME = cell[6];
                                if (cell[7] != "")
                                {
                                    index.RANGE_FALG = Convert.ToInt32(cell[7]);
                                }
                                index.AUDIT_USERID = cell[8];
                                index.AUDIT_USERNAME = cell[9];
                                if (cell[10] != "")
                                {
                                    index.AUDIT_TIME = Convert.ToDateTime(cell[10]);
                                }
                                index.CREAT_USERID = cell[11];
                                index.CREAT_USERNAME = cell[12];
                                if (cell[13] != "")
                                {
                                    index.CREAT_TIME = Convert.ToDateTime(cell[13]);
                                }
                                index.LAST_USERID = cell[14];
                                index.LAST_USERNAME = cell[15];
                                if (cell[16] != "")
                                {
                                    index.LAST_TIME = Convert.ToDateTime(cell[16]);
                                }
                                index.REMARK = cell[17];
                                if (cell[18] != "")
                                {
                                    index.DATA_MODEL_CLASS = Convert.ToInt32(cell[18]);
                                }
                                index.DATA_MODEL_CODE = cell[19];
                                if (cell[20] != "")
                                {
                                    index.FILE_FALG = Convert.ToInt32(cell[20]);
                                }
                                if (cell[21] != "")
                                {
                                    index.DEL_FLAG = Convert.ToInt32(cell[21]);
                                }
                                index.MONITOR_CODE = cell[22];
                                if (cell[23] != "")
                                {
                                    index.LEVEL_FLAG = Convert.ToInt32(cell[23]);
                                }
                                if (cell[24] != "")
                                {
                                    index.AUTO_PAGE = Convert.ToInt32(cell[24]);
                                }
                                index.TEM_ATTR = cell[25];
                                if (cell[26] != "")
                                {
                                    index.SYSTEM_FLAG = Convert.ToInt32(cell[26]);
                                }
                                etc.Add_TEM_FILE_INDEX(index);
                            }
                           
                        }
                       

                    }
                }
                MessageBox.Show(items +"行数据受影响");
            }
            }

原创粉丝点击