/// <summary>
/// 保存文件到数据库
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button1_Click(object sender, EventArgs e)
{
string conn = @"Data Source=YANGYIBANG\SQLEXPRESS;Initial Catalog=joe;Integrated Security=True";
SqlConnection cn = new SqlConnection(conn);
Stream myStream = null;
OpenFileDialog openFileDialog1 = new OpenFileDialog();
//openFileDialog1.InitialDirectory = "c:\\";
//openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
//openFileDialog1.FilterIndex = 2;
openFileDialog1.RestoreDirectory = true;
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
try
{
FileInfo fi = new FileInfo(openFileDialog1.FileName);
FileStream fs = fi.OpenRead();
byte[] bytes = new byte[fs.Length];
fs.Read(bytes, 0, Convert.ToInt32(fs.Length));
SqlCommand cm = new SqlCommand();
cm.Connection = cn;
cm.CommandType = CommandType.Text;
if (cn.State == 0) cn.Open();
cm.CommandText = "insert into title (titileName, files ) values('aaa',@file)";
SqlParameter spFile = new SqlParameter("@file", SqlDbType.Image);
spFile.Value = bytes;
cm.Parameters.Add(spFile);
cm.ExecuteNonQuery();
}
catch (Exception ex)
{
MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);
}
}
}
/// <summary>
/// 从数据库就读取文件另存为
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button2_Click(object sender, EventArgs e)
{
try
{
string conn = @"Data Source=YANGYIBANG\SQLEXPRESS;Initial Catalog=joe;Integrated Security=True";
SqlConnection cn = new SqlConnection(conn);
SqlDataReader dr = null;
SqlCommand cm = new SqlCommand();
cm.Connection = cn;
cm.CommandType = CommandType.Text;
cm.CommandText = "select files from title where titileId=5";
cn.Open();
dr = cm.ExecuteReader();
byte[] File = null;
if (dr.Read())
{
File = (byte[])dr[0];
}
dr.Close();
cn.Close();
/*
* 直接另存为文件
*
*/
SaveFileDialog saveFileDialog1 = new SaveFileDialog();
Stream myStream;
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
if ((myStream = saveFileDialog1.OpenFile()) != null)
{
using (BinaryWriter bw = new BinaryWriter(myStream))
{
// Add some text to the file.
bw.Write(File);
}
}
}
}
catch (Exception ex)
{
MessageBox.Show("Error: Could not read file from db. Original error: " + ex.Message);
}
}
分享到:
相关推荐
C#保存文件或读取数据库文件 另存为.doc
C#基础操作之读取文件到数据库,值得下载和练习
C#保存文件到数据库或从数据库导出C#保存文件到数据库或从数据库导出
C#读取XML文件并写入数据库的三种方法,直接写入和先读取到内存再写入数据库
C# 文件保存到数据库中或者从数据库中读取文件
简单的例子,把图片保存到数据库中,附带有sql语句。 并且有读取方法。可以直接读取并查看数据库...当然,你也可以保存其他文件到数据库中。只要自己修改几个。 用的VS2010.sql语句自己随意咯~ 我的博客 1994blog.cn
C#读取Excel文件,显示到ListView上,保存Excel文件
c# 实现的。通过配置文件config 读取数据库连接字符串,用户名密码等。通过配置文件,有利于系统的升级。
在C#或asp.net中将文件以字节流的方式保存到数据库中
每天自动执行:采用Winform读取固定文件夹里面的HTML文件并保存到sql数据库
c#大文件读取和写入数据库(带进度条的源代码).pdf
用c#读取图片文件,并存入数据库,然后从数据库中把相应的二进制图片数据还原成指定目录下的图片文件。 注意:数据库名称和数据表需要修改成自己的数据表,否则出现数据库连接错误,
C# 文件写入数据库 image类型读写 文件text类型读写 base64转换 C# 文件写入数据库 image类型读写 文件text类型读写 base64转换
C#保存任何文件和从数据库导出文件到本地磁盘
c#读取二进制文件c#读取二进制文件c#读取二进制文件c#读取二进制文件c#读取二进制文件c#读取二进制文件
C#读取txt文件并导入数据库
用C#写的文件读取功能,根据文件已读取数据同步显示已读取文件进度条。
c#中把图片存入数据库并进行读取c#中把图片存入数据库并进行读取c#中把图片存入数据库并进行读取