展开全部
//把文件转成二进制流出入数据库
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();
}
展开全部
一楼开玩笑了!!
可以保存到数据库的。
首先,你的数据库里要有一个存放二进制数据的字段。
然后,用一个文件选择控件,让用户选择图片。
用FileStream.Read把图片文件按照二进制读取到byte[]中,
接下来,链接数据库,用sql语句,进行相应的插入操作,将数据库的二进制数据的字段赋值为byte[]就行了。
以下是保存和显示的代码:
private void SaveImage(string fileName)
{
// Read the file into a byte array
using (FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read))
{
byte[] imageData = new Byte[fs.Length];
fs.Read(imageData, 0, (int)fs.Length);
using (SqlConnection conn = new SqlConnection(connectionString))
{
string sql = "insert into image (imagefilename,blobdata) values (@filename,@blobdata)";
SqlCommand cmd = new SqlCommand(sql, conn);
cmd.Parameters.Add("@filename",SqlDbType.Text);
cmd.Parameters["@filename"].Direction = ParameterDirection.Input;
cmd.Parameters.Add("@blobdata", SqlDbType.Image);
cmd.Parameters["@blobdata"].Direction = ParameterDirection.Input;
// Store the byte array within the image field
cmd.Parameters["@filename"].Value = fileName;
cmd.Parameters["@blobdata"].Value = imageData;
conn.Open();
if (cmd.ExecuteNonQuery() == 1)
{
MessageBox.Show("Done");
}
}
}
}
private void LoadImage(string fileName)
{
using (SqlConnection conn = new SqlConnection(connectionString))
{
string sql = "select blobdata from Image where ImageFileName like @filename";
SqlCommand cmd = new SqlCommand(sql, conn);
cmd.Parameters.Add("@filename", SqlDbType.Text);
cmd.Parameters["@filename"].Value = fileName;
conn.Open();
object objImage = cmd.ExecuteScalar();
byte[] buffer = (byte[])objImage;
BinaryWriter bw = new BinaryWriter(new FileStream("C:\\abcd.png", FileMode.Create));
bw.Write(buffer);
bw.Close();
MemoryStream ms = new MemoryStream(buffer);
Image bgImage = Image.FromStream(ms);
ms.Close();
this.BackgroundImage = bgImage;
}
}
------------------------------------------------------------------
PS:用空情报我踩踩空间,谢谢。
http://hi.baidu.com/kxl361
这样可以么?
可以保存到数据库的。
首先,你的数据库里要有一个存放二进制数据的字段。
然后,用一个文件选择控件,让用户选择图片。
用FileStream.Read把图片文件按照二进制读取到byte[]中,
接下来,链接数据库,用sql语句,进行相应的插入操作,将数据库的二进制数据的字段赋值为byte[]就行了。
以下是保存和显示的代码:
private void SaveImage(string fileName)
{
// Read the file into a byte array
using (FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read))
{
byte[] imageData = new Byte[fs.Length];
fs.Read(imageData, 0, (int)fs.Length);
using (SqlConnection conn = new SqlConnection(connectionString))
{
string sql = "insert into image (imagefilename,blobdata) values (@filename,@blobdata)";
SqlCommand cmd = new SqlCommand(sql, conn);
cmd.Parameters.Add("@filename",SqlDbType.Text);
cmd.Parameters["@filename"].Direction = ParameterDirection.Input;
cmd.Parameters.Add("@blobdata", SqlDbType.Image);
cmd.Parameters["@blobdata"].Direction = ParameterDirection.Input;
// Store the byte array within the image field
cmd.Parameters["@filename"].Value = fileName;
cmd.Parameters["@blobdata"].Value = imageData;
conn.Open();
if (cmd.ExecuteNonQuery() == 1)
{
MessageBox.Show("Done");
}
}
}
}
private void LoadImage(string fileName)
{
using (SqlConnection conn = new SqlConnection(connectionString))
{
string sql = "select blobdata from Image where ImageFileName like @filename";
SqlCommand cmd = new SqlCommand(sql, conn);
cmd.Parameters.Add("@filename", SqlDbType.Text);
cmd.Parameters["@filename"].Value = fileName;
conn.Open();
object objImage = cmd.ExecuteScalar();
byte[] buffer = (byte[])objImage;
BinaryWriter bw = new BinaryWriter(new FileStream("C:\\abcd.png", FileMode.Create));
bw.Write(buffer);
bw.Close();
MemoryStream ms = new MemoryStream(buffer);
Image bgImage = Image.FromStream(ms);
ms.Close();
this.BackgroundImage = bgImage;
}
}
------------------------------------------------------------------
PS:用空情报我踩踩空间,谢谢。
http://hi.baidu.com/kxl361
这样可以么?
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询