C#保存文件或读取数据库文件 另存为
2009-06-29 23:23
477 查看
/// <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);
}
}
/// 保存文件到数据库
/// </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#】---C#实现上传word以流形式保存到数据库和读取数据库中的word文件。
- C# 文件保存到数据库中或者从数据库中读取文件
- C#将文件保存到数据库中或者从数据库中读取文件
- 用C#编程从数据库中读取图片数据导进Excel文件的方法(如何从数据库中读取保存的文件,直接打开,中间不保存到本地)
- 点滴积累【C#】---C#实现上传word将路径保存到数据库,文件保存到服务器。并且按照名称读取服务器的word
- C#将文件保存到数据库中或者从数据库中读取文件
- 用C#编程从数据库中读取图片数据导进Excel文件的方法(如何从数据库中读取保存的文件,直接打开,中间不保存到本地)
- C#将文件保存到数据库中或者从数据库中读取文件
- C#将文件保存到数据库中或者从数据库中读取文件(转)
- C# 保存byte[]到数据库和读取,把byte[]写进文件文件中
- [转]C#将文件保存到数据库中或者从数据库中读取文件
- C# 文件保存到数据库中或者从数据库中读取文件
- C#将文件保存到数据库中或者从数据库中读取文件
- C#读取CSV文件并保存进数据库
- [转]C#将文件保存到数据库中或者从数据库中读取文件
- c#将文件保存到数据库中或者从数据库中读取文件
- C#将文件保存到数据库中或者从数据库中读取文件
- C#将文件保存到数据库中或者从数据库中读取文件
- C#将文件保存到数据库中或者从数据库中读取文件