C# .net 如何向SQL Server数据库中存储图片?
需要什么控件呢?我用VS2013FileUpLoad可不可以实现呢?最后能把代码贴上,跪求...
需要什么控件呢? 我用VS2013
FileUpLoad 可不可以实现呢?
最后能把代码贴上,跪求 展开
FileUpLoad 可不可以实现呢?
最后能把代码贴上,跪求 展开
展开全部
可以,你用上传插件把图片传到一个文件中,然后把图片连接保存在数据库不就行了。
Upload上传图片预览
前台:
<head runat="server">
<title></title>
<style type="text/css">
#newPreview
{
filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=scale)
}
</style>
<script language="javascript" type="text/javascript">
function show(url) {
var imgserver = document.getElementById("Image1");
imgserver.style.display = 'none';
var newPreview = document.getElementById("newPreview");
newPreview.filters.item("DXImageTransform.Microsoft.AlphaImageLoader").src = url;
newPreview.style.width = "80px";
newPreview.style.height = "60px";
}
</script>
</head>
<body>
<form id="form1" runat="server">
<asp:Button ID="Button1" runat="server" Text="上传" onclick="Button1_Click" />
<div id="newPreview">
<asp:Image ID="Image1" runat="server" Height="214px" Width="236px" />
</div>
<asp:FileUpload ID="FileUpload_diff" onchange="show(this.value)" runat="server" />
</form>
</body>
后台:
protected void Button1_Click(object sender, EventArgs e)
{
//获取要上传图片的全名
string file = FileUpload_diff.FileName;
if (file.Equals(""))
{
ScriptManager.RegisterStartupScript(this, this.GetType(), "", "alert('请先点击浏览选择照片');", true);
}
else
{
//截取图片后缀
string type = file.Substring(file.LastIndexOf(".") + 1);
//设置文件上传的文件夹路径
string path = Server.MapPath("~/test/upload");
path += "//" + file;
//将图片上传到服务器
FileUpload_diff.PostedFile.SaveAs(path);
Image1.ImageUrl = "upload/"+file;
}
}
展开全部
参考:
//这个方法是浏览文件对象
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();
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |