Пример #1
0
        private void button1_Click(object sender, EventArgs e)
        {
            T_UpdateTableAdapter adapter = new T_UpdateTableAdapter();
            OpenFileDialog       mDlg    = new OpenFileDialog();

            mDlg.InitialDirectory = AppDomain.CurrentDomain.BaseDirectory;
            if (mDlg.ShowDialog() == DialogResult.OK)
            {
                FileStream fs    = new FileStream(mDlg.FileName, FileMode.Open);
                byte[]     bFile = new byte[fs.Length];
                fs.Read(bFile, 0, (int)fs.Length);
                adapter.Insert("销售管理", "1.1", "", DateTime.Now, bFile, "");
            }
        }
Пример #2
0
        private void btnUpload_Click(object sender, EventArgs e)
        {
            try
            {
                int count = 0;
                //把表格中的文件上传
                T_UpdateTableAdapter adapter = new T_UpdateTableAdapter();
                if (adapter.Connection.State == ConnectionState.Closed)
                {
                    adapter.Connection.Open();
                }
                //循环
                foreach (DataRow mRow in mTable.Rows)
                {
                    string fileName, fileUpPath, fileFullPath;
                    fileName     = mRow["文件名"].ToString();
                    fileUpPath   = mRow["相对路径"].ToString();
                    fileFullPath = mRow["文件目录"].ToString();


                    //
                    if (File.Exists(fileFullPath) == true) //检查文件是够存在
                    {
                        string filePath = "";

                        //如果上传文件是本身,复制文件并上传
                        filePath = fileFullPath.Insert(fileFullPath.LastIndexOf('\\'), "\\upload");
                        if (Directory.Exists(filePath.Substring(0, filePath.LastIndexOf('\\'))) == false)
                        {
                            Directory.CreateDirectory(filePath.Substring(0, filePath.LastIndexOf('\\')));
                        }
                        File.Copy(fileFullPath, filePath, true);


                        using (FileStream fs = new FileStream(filePath, FileMode.Open)) //打开文件,将文件写入字节数组
                        {
                            byte[] bFile = new byte[fs.Length];
                            fs.Read(bFile, 0, (int)fs.Length);

                            adapter.Insert(fileName, txtVer.Text, fileUpPath, DateTime.Now, bFile, "");
                            fs.Close();
                            fs.Dispose();
                            count++;
                        }
                    }
                    else
                    {
                        MessageBox.Show("文件不存在");
                    }
                }

                if (adapter.Connection.State == ConnectionState.Open)
                {
                    adapter.Connection.Close();
                }

                if (count > 0)
                {
                    MessageBox.Show("文件上传成功");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }