C# 文件与二进制互转数据库写入读出
2014-11-20 10:02
483 查看
//这个方法是浏览文件对象
private void button1_Click(object sender, EventArgs e)
{
//用户打开文件浏览
using (OpenFileDialog dialog = new OpenFileDialog())
{
//只能单选一个文件
dialog.Multiselect = false;
//选择一个文件
if (dialog.ShowDialog() == DialogResult.OK)
{
try
{
//把选择的文件路径给txtPath
this.textBox1.Text = dialog.FileName;
}
catch (Exception ex)
{
//抛出异常
throw (ex);
}
}
}
}
//关闭
private void button3_Click(object sender, EventArgs e)
{
this.Close();
}
//把文件转成二进制流出入数据库
private void button2_Click(object sender, EventArgs e)
{
FileStream fs = new FileStream(textBox1.Text, FileMode.Open);
BinaryReader br = new BinaryReader(fs);
Byte[] byData = br.ReadBytes((int)fs.Length);
fs.Close();
string conn = "server=.;database=testDB;Uid=sa;Pwd=sa ";
SqlConnection myconn = new SqlConnection(conn);
myconn.Open();
string str = "insert into pro_table (pro_name,pro_file) values('测试文件',@file)";
SqlCommand mycomm = new SqlCommand(str, myconn);
mycomm.Parameters.Add("@file", SqlDbType.Binary, byData.Length);
mycomm.Parameters["@file"].Value = byData;
mycomm.ExecuteNonQuery();
myconn.Close();
}
//从数据库中把二进制流读出写入还原成文件
private void button4_Click(object sender, EventArgs e)
{
string conn = "server=.;database=testDB;Uid=sa;Pwd=sa ";
string str = "select pro_file from pro_table where pro_name='测试文件' ";
SqlConnection myconn = new SqlConnection(conn);
SqlDataAdapter sda = new SqlDataAdapter(str, conn);
DataSet myds = new DataSet();
myconn.Open();
sda.Fill(myds);
myconn.Close();
Byte[] Files = (Byte[])myds.Tables[0].Rows[0]["pro_file"];
BinaryWriter bw = new BinaryWriter(File.Open("D:\\2.rdlc",FileMode.OpenOrCreate));
bw.Write(Files);
bw.Close();
}
//这个方法是浏览文件对象
private void button1_Click(object sender, EventArgs e)
{
//用户打开文件浏览
using (OpenFileDialog dialog = new OpenFileDialog())
{
//只能单选一个文件
dialog.Multiselect = false;
//选择一个文件
if (dialog.ShowDialog() == DialogResult.OK)
{
try
{
//把选择的文件路径给txtPath
this.textBox1.Text = dialog.FileName;
}
catch (Exception ex)
{
//抛出异常
throw (ex);
}
}
}
}
//关闭
private void button3_Click(object sender, EventArgs e)
{
this.Close();
}
//把文件转成二进制流出入数据库
private void button2_Click(object sender, EventArgs e)
{
FileStream fs = new FileStream(textBox1.Text, FileMode.Open);
BinaryReader br = new BinaryReader(fs);
Byte[] byData = br.ReadBytes((int)fs.Length);
fs.Close();
string conn = "server=.;database=testDB;Uid=sa;Pwd=sa ";
SqlConnection myconn = new SqlConnection(conn);
myconn.Open();
string str = "insert into pro_table (pro_name,pro_file) values('测试文件',@file)";
SqlCommand mycomm = new SqlCommand(str, myconn);
mycomm.Parameters.Add("@file", SqlDbType.Binary, byData.Length);
mycomm.Parameters["@file"].Value = byData;
mycomm.ExecuteNonQuery();
myconn.Close();
}
//从数据库中把二进制流读出写入还原成文件
private void button4_Click(object sender, EventArgs e)
{
string conn = "server=.;database=testDB;Uid=sa;Pwd=sa ";
string str = "select pro_file from pro_table where pro_name='测试文件' ";
SqlConnection myconn = new SqlConnection(conn);
SqlDataAdapter sda = new SqlDataAdapter(str, conn);
DataSet myds = new DataSet();
myconn.Open();
sda.Fill(myds);
myconn.Close();
Byte[] Files = (Byte[])myds.Tables[0].Rows[0]["pro_file"];
BinaryWriter bw = new BinaryWriter(File.Open("D:\\2.rdlc",FileMode.OpenOrCreate));
bw.Write(Files);
bw.Close();
}
相关文章推荐
- C# 单文件与二进制互转数据库写入读出(上传文件二进制方式到DB中,下载DB中的二进制文件到本地)
- C# 文件与二进制互转数据库写入读出
- C# 多文件与二进制互转数据库写入读出(上传文件二进制方式到DB中,下载DB中的二进制文件到本地)
- C# 文件与二进制互转数据库写入读出
- C# 文件与二进制互转数据库写入读出
- C# 文件与二进制互转数据库写入读出
- C# 文件与二进制互转数据库写入读出--还包含选择文件~
- 转:C# 文件与二进制互转数据库写入读出
- C# 将long类型写入二进制文件用bw.Write(num);将其读出用long num= br.ReadInt64();
- 将数据库二进制文件读出并保存到磁盘上
- c#将大文件读取或写入到数据库(带进度条的源码)
- C#读取二进制文件入数据库
- C# 读取TXT文件并写入数据库
- C#读取Excel写入数据库/将 Excel 文件转成 DataTable
- QX项目实战-3.读取数据、数据写入数据库、读出数据生成新文件
- C# 读取数据库word 和写入数据库word文件
- C#大文件读取和写入数据库
- 如何使用 ASP.NET 和 Visual C# .NET 向浏览器写入二进制文件
- mysql文件写入和读出数据库
- C#向数据库写二进制以及读出