你的分享就是我们的动力 ---﹥

C#MySQL数据库对Pro/e,cad文件的储存与读取

C#MySQL数据库对Pro/e,cad文件的存储与读取

C#MySQL数据库对Pro/e,cad文件的存储与读取


其中MySQL数据库的设计如下图所示:

C#MySQL数据库对Pro/e,cad文件的储存与读取


类实现如下:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data;
using System.Collections;
using System.Drawing;

//对流进行操作要引用
using System.IO;
//连接数据库操作要引用
using MySql.Data.MySqlClient;

namespace DBjiaju_Dev
{
    class FileMySQL
    {
        /// <存储文件至MySQL数据库>
        /// 存储图片至MySQL数据库
        /// </summary>
        /// <param name="strImagePath"></param>
        /// <returns></returns>
        public bool MapSearchWrite(String strImagePath, String name)
        {
            //将文件转换成缓冲流
            FileStream fs = new FileStream(strImagePath, FileMode.Open, FileAccess.Read);
            //获取文件的字节数组
            byte[] byImage = new byte[fs.Length];
            fs.Read(byImage, 0, byImage.Length);
            fs.Close();
            //数据库连接
            MySqlConnection conn = new MySqlConnection();
            conn.ConnectionString = "Server=localhost;Uid=root;Password=123456;Database=firefighting;charset=gb2312";
            try
            {
                conn.Open();
            }
            catch
            {
                conn.Close();
                conn.Dispose();
                throw new ArgumentException("数据库连接失败");
            }
            //ignore 的作用:当前字段存在则覆盖,不存在则插入
            String insertStr = "insert ignore into table_zzc(pro,name) values(?pro,?name)";
            MySqlCommand cmd = new MySqlCommand();
            cmd.Connection = conn;
            cmd.CommandText = insertStr;
            cmd.CommandType = CommandType.Text;
            //设置数据库字段类型MediumBlob的值为文件字节
            cmd.Parameters.Add(new MySqlParameter("?pro", MySqlDbType.MediumBlob)).Value = byImage;
            cmd.Parameters.Add(new MySqlParameter("?name", MySqlDbType.Text)).Value = name;
            int flag = 1;
            //执行命令
            if (flag.Equals(1))
            {
                try
                {
                    cmd.ExecuteNonQuery();
                }
                catch (Exception ex)
                {
                    flag = 0;
                    return false;
                    throw ex;
                }
                cmd.Dispose();
                conn.Close();
                conn.Dispose();
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <读取MySQL数据库中的文件>
        /// 读取MySQL数据库中的文件
        /// </summary>
        /// <param name="imageByteResulet"></param>
        public String MapSearchQuery(byte[] imageByteResulet, string name)
        {
            imageByteResulet = null;
            MySqlConnection conn = new MySqlConnection();
            conn.ConnectionString = "Server=localhost;Uid=root;Password=123456;Database=firefighting;charset=gb2312";
            try
            {
                conn.Open();
            }
            catch
            {
                conn.Close();
                conn.Dispose();
                throw new ArgumentException("数据库连接失败");
            }
            String strQueryCmd = "select cad from table_zzc where name='" + name + "'";
            MySqlCommand cmd = new MySqlCommand(strQueryCmd, conn);
            MySqlDataReader dataReader = null;
            try
            {
                dataReader = cmd.ExecuteReader();
            }
            catch
            {
                dataReader.Dispose();
                cmd.Dispose();
                conn.Close();
                conn.Dispose();
                throw new ArgumentException("数据库查询失败");
            }

            if (dataReader.Read())
            {
                imageByteResulet = new byte[dataReader.GetBytes(0, 0, null, 0, int.MaxValue)];
                dataReader.GetBytes(0, 0, imageByteResulet, 0, imageByteResulet.Length);
                //将文件字节数据加载入到缓冲流
                MemoryStream stream = new MemoryStream(imageByteResulet);
                byte[] bytes = new byte[stream.Length];
                stream.Read(bytes, 0, bytes.Length);
                //设置当前流的位置为流的开始
                stream.Seek(0, SeekOrigin.Begin);
                //把byte[]写入文件
                String fileName;
                fileName = @"C:\Users\Administrator\Desktop\DBjiaju\DBjiaju_Dev\DBjiaju_Dev\MyFile\" + name + ".dwg";
                BinaryWriter bw = new BinaryWriter(File.Open(fileName, FileMode.Create));
                bw.Write(bytes);
                bw.Close();
                return fileName;
            }
            dataReader.Dispose();
            cmd.Dispose();
            conn.Close();
            conn.Dispose();
            return null;
        }
    }
}