您的位置:首页 > 数据库 > MySQL

从MySQL中态读取、添加CAD文件

2016-07-15 11:40 519 查看
          首先在 VS环境中新建工程项目,然后选择windows窗体应用程序,右击项目名称,并单击“添加引用”,点击对话框中的“浏览”、“预览”,选择添加“MySql.Data.dll”,这是c#连接mysql数据库的动态库,里面封装了很多常用的操作数据库的方法。在窗体的代码中加入“using
MySql.Data.MySqlClient;”和“usingSystem.Data;”(这个是自动生成的),这就是代码中的实际引用mysql.data.dll中的内容,有了这个c#就能很方便地操作sql数据库。

       使用navicat第三方软件在数据库test中新建表user,然后新建字段cad(mediumblob)(上传时间)、name(varchar(64)),navicat软件是mysql的图形化界面工具,直观地通过界面来操作。

//对流进行操作要引用  

using System.IO;  

//连接数据库操作要引用  

using MySql.Data.MySqlClient; 

 
     下面在windows窗体的button控件单击事件下添加代码,添加CAD文件到数据库内。

 

//strImagePath是CAD文件
//将文件转换成缓冲流
FileStream fs = new FileStream(strImagePath, FileMode.Open, FileAccess.Read);//FileMode.Open(打开现有的文件,流指向文件的开头);FileAccess.Read(打开文件,用于只读)
//获取文件字节数组
byte[] byImage = new byte[fs.Length];
fs.Read(byImage, 0, byImage.Length-0);// byImage此方法返回时包含在指定的字节数组;第二个参数,byImage 中的从零开始的字节偏移量,从此处开始将字节读入到数组中;第三个参数,最多读入的字节数
fs.Close();
//数据库连接
MySqlConnection conn = new MySqlConnection();
//服务器名称;用户名;密码;数据库名称;字符编码/字符集,比如gb2312是简体中文,只包含简体中文,不包括繁体字;utf-8是一种任何语言都可以使用的编码形式等等。一般就用gb2312就行
conn.ConnectionString = "Server =localhost;Uid = root;Password =1234;Database =test;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 new exception("失败",ex);
}
cmd.Dispose();
conn.Close();
conn.Dispose();
return true;
}
else
{
return false;
}


从数据库中读取CAD文件



MySqlConnection conn = new MySqlConnection();
conn.ConnectionString = "Server =localhost;Uid = root;Password =1234;Database =test;charset = gb2312";
//服务器名称;用户名;密码;数据库名称;字符编码/字符集,比如gb2312是简体中文,只包含简体中文,不包括繁体字;utf-8是一种任何语言都可以使用的编码形式等等。一般就用gb2312就行
try//异常处理,判断数据库是否已经安全连接
{
conn.Open();
}
catch
{
conn.Close();
conn.Dispose();
throw new ArgumentException("数据库连接失败");
}
String strQueryCmd = "select cad from user where name='" + 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())//判断是否从数据库中已经读取数据,如果读取到文件,则将其输出
{
byte[] imageByteResulet = new byte[100];
imageByteResulet = new byte[dataReader.GetBytes(0, 0, null, 0, int.MaxValue)];//dataReader.GetBytes()读取二进制内容</span>
dataReader.GetBytes(0, 0, imageByteResulet, 0, imageByteResulet.Length);//第一个参数,从0开始的序列号;第二个参数,字段中的索引,从其开始读取操作;第三个参数,要将字节流读入的缓冲区;第四个参数,缓冲区中开始读入操作的索引;第五个参数,要复制到缓冲区的最大长度
//将文件字节数据加载入到缓冲流
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是新建的空文件夹
fileName = Directory.GetCurrentDirectory() + "\\MyFile\\" + name + ".prt.1";//Directory.GetCurrentDirectory()是指该软件打包之后的exe的安装位置;或者在该软件没有打包时,指该项目bin\debug目录。在取到的目录下新建MyFile文件夹,用于存放临时cad文件,cad文件是从数据库中取出的二进制文件转换而来的
BinaryWriter bw = new BinaryWriter(File.Open(fileName, FileMode.Create));
bw.Write(bytes);
bw.Close();
return fileName;
}
dataReader.Dispose();
cmd.Dispose();
conn.Close();
conn.Dispose();
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  c# MySQL