大佬教程收集整理的这篇文章主要介绍了在sqlserver中操作image、varbinary,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
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 style="width: 488px">
<tr>
<td>
上传图片(选择你要上传的图片)</</td>
<td>
<asp:FileUpload ID="UP_FILE" runat="server" /></td>
<td>
</td>
</tr>
<tr>
<td style="height: 40px">
文件说明(添加上传图片说明,如:作者、出处)</td>
<td style="height: 40px">
<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所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。