コード例 #1
0
ファイル: Program.cs プロジェクト: viticm/pap2
        /// <summary>
        /// 导入商店物品信息
        /// </summary>
        public bool LoadShopItemFromPath()
        {
            bool loadSucess = true; // 是否导入成功
            rootFolder = GetLoadPath(); // 根目录

            if (rootFolder == null) // 用户未选择商店道具表所在的目录
            {
                MessageBox.Show("没有选择商店道具表所在的目录!");
                return false;
            }

            List<string> fileNameList = new List<string>();
            DirectoryInfo shopDirectoryInfo = new DirectoryInfo(path);
            
            foreach (DirectoryInfo di in shopDirectoryInfo.GetDirectories())
            {
                if (!di.Attributes.ToString().Contains("Hidden")) // 不显示隐藏文件夹
                {
                    string folderName = di.Name;

                    foreach (FileInfo fi in di.GetFiles())
                    {
                        if (!fi.Attributes.ToString().Contains("Hidden")) // 不显示隐藏文件
                        {
                            string fileName = fi.Name;

                            fileNameList.Add(string.Format("{0}\\{1}", folderName, fileName));
                        }                        
                    }
                }                
            }

            ChooseFileForm cForm = new ChooseFileForm("请选择要导入的商店文件", fileNameList);

            if (cForm.ShowDialog() == DialogResult.OK)
            {
                SqlTransaction transaction = null;
                ProgressForm pForm = new ProgressForm(0, fileNameList.Count);
                pForm.Show();

                try
                {
                    if (conn.State == ConnectionState.Closed)
                    {
                        conn.Open();
                    }

                    transaction = conn.BeginTransaction();
                    SqlCommand cmd = conn.CreateCommand();
                    cmd.Transaction = transaction;

                    int index = 1;

                    foreach (string s in fileNameList)
                    {
                        string[] data = s.Split(new char[] { '\\' }, StringSplitOptions.RemoveEmptyEntries);
                        string folderName = data[0];
                        string fileName = data[1];

                        pForm.ShowProgress(index, string.Format("更新文件{0}的数据...", s));
                        UpdateShopItem(cmd, fileName, folderName);
                        index++;
                    }

                    transaction.Commit();
                }
                catch (SqlException ex)
                {
                	MessageBox.Show("在更新商店道具信息时产生sql异常:" + ex.Message, "更新商店道具信息", MessageBoxButtons.OK,
                                    MessageBoxIcon.Information);

                    if (transaction != null)
                    {
                        transaction.Rollback();
                    }

                    loadSucess = false;
                }
                finally
                {
                    if (conn.State == ConnectionState.Open)
                    {
                        conn.Close();
                    }
                }
            }
            else
            {
                loadSucess = false;
            }           

            if (loadSucess)
            {
                MessageBox.Show("商店道具信息更新成功!\r\n\r\n当前导表操作已经完成,继续的话会有bug哦~", "更新商店道具信息",
                                MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                MessageBox.Show("商店道具信息更新失败!\r\n\r\n当前导表操作已经完成,继续的话会有bug哦~", "更新商店道具信息",
                                MessageBoxButtons.OK, MessageBoxIcon.Information);
            }

            return loadSucess;
        }