转自:http://zhidao.baidu.com/link?url=1UKceaS9BQjWKDIq5ePLjRoIB1mw7wnFqwiCjat3T3JDr7xo_oskx9N28A48waO-_UtPJoi_jFGupS28B4ZuNv9ZPJAoLf6HHRgHP3LmAoC
请问各位 如何图片直接存sql 数据库 ,而不是存 路径 ?谢谢 C#
2014-08-14 14:58
提问者采纳
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
//把文件转成二进制流出入数据库
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();
}
|
-
提问者评价
-
谢谢!