
httpPostedfile Upfile = UP_file.Postedfile; //httpPostedfile对象,用于读取图象文件属性
int fileLength = Upfile.ContentLength; //记录文件长度
try
{
if (fileLength == 0)
{ //文件长度为零时
txtMessage.Text = "<b>请你选择你要上传的文件</b>";
}
else
{
Byte[] fileByteArray = new Byte[fileLength]; //图象文件临时储存Byte数组
Stream StreamObject = Upfile.inputStream; //建立数据流对像
//读取图象文件数据,fileByteArray为数据储存体,0为数据指针位置、fileLnegth为数据长度
StreamObject.Read(fileByteArray,fileLength);
//建立sql Server链接
sqlConnection Con = new sqlConnection("Data Source=YFXIANGGX;Initial Catalog=Jsmstc_teach;User ID=sa;Pwd=1;");
//type 上传文件类型,length上传文件长度,info image,info1 NVarChar,info2 VarChar,info3 VarBinary
String sqlCmd = "INSERT INTO BigFIEld (type,length,info,info1,info2,info3) VALUES (@type,@length,@info,@info1,@info2,@info3)";
sqlCommand Cmdobj = new sqlCommand(sqlCmd,Con);
Cmdobj.Parameters.Add("@type",sqlDbType.Char,50).Value = Upfile.ContentType;
Cmdobj.Parameters.Add("@length",sqlDbType.Int).Value = Upfile.ContentLength;
Cmdobj.Parameters.Add("@info",sqlDbType.Image).Value = fileByteArray;
Cmdobj.Parameters.Add("@info1",sqlDbType.NVarChar).Value = fileByteArray.ToString();
Cmdobj.Parameters.Add("@info2",sqlDbType.VarChar).Value = fileByteArray.ToString();
Cmdobj.Parameters.Add("@info3",sqlDbType.VarBinary,fileLength).Value = fileByteArray;
Con.open();
Cmdobj.ExecuteNonquery();
Con.Close();
txtMessage.Text = "<p><b>OK!你已经成功上传你的图片</b>";//提示上传成功
}
}
catch (Exception ex)
{
txtMessage.Text = ex.Message.ToString();
}
页面代码:
<%@ Page Language="C#" autoEventWireup="true" Codefile="saveall.aspx.cs" inherits="saveall" %>
<!DOCTYPE HTML PUBliC "-//W3C//DTD xhtml 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<HTML xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<Title>无标题页</Title>
</head>
<body>
<h1>测试数据库对于大字段()的存取</h1>
<form ID="form1" runat="server">
<div>
<table >
<tr>
<td>
@R_502_6735@(选择你要上传的图片)</</td>
<td>
<asp:fileUpload ID="UP_file" runat="server" /></td>
<td>
</td>
</tr>
<tr>
<td >
文件说明(添加@R_502_6735@说明,如:作者、出处)</td>
<td >
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td>
<asp:button ID="button1" runat="server" OnClick="button1_Click" Text="uploadImage" /></td>
<td>
<asp:Label ID="txtMessage" runat="server" Text="Label"></asp:Label></td>
</tr>
</table>
</div>
</form>
</body>
</HTML>
从数据库中读出时遇到了问题,首先是如何将二进制的数据读出后显示到image上,其次是对于保存成varchar,nvarchar字段的值无法取出来了.都没有解决.读出的数据只能通过response输出到当前页面,无法直接在image控件上输出.保存为image,varbinary字段的值取出来的代码如下:
string imgID = "1"; //imgID为图片ID
//建立数据库链接
sqlConnection Con = new sqlConnection("Data Source=YFXIANGGX;Initial Catalog=Jsmstc_teach;User ID=sa;Pwd=1;");
String sqlCmd = "SELECT * FROM BigFIEld WHERE ID = @ID";
sqlCommand Cmdobj = new sqlCommand(sqlCmd,Con);
Cmdobj.Parameters.Add("@ID",sqlDbType.Int).Value = imgID;
Con.open();
sqlDataReader sqlReader = Cmdobj.ExecuteReader();
sqlReader.Read();
//Response.ContentType = (string)sqlReader["ImageContentType"];//设定输出文件类型
Response.ContentType = sqlReader["type"].ToString();//设定输出文件类型
//输出图象文件二进制数制
Response.OutputStream.Write((byte[])sqlReader["info"],(int)sqlReader["length"]);//或者是sqlReader["info3"],
Response.End();
Con.Close();
总结以上是内存溢出为你收集整理的在sqlserver中 *** 作image、varbinary全部内容,希望文章能够帮你解决在sqlserver中 *** 作image、varbinary所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)