加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > MsSql教程 > 正文

在sqlserver中操作image、varbinary

发布时间:2020-12-12 14:27:20 所属栏目:MsSql教程 来源:网络整理
导读:今天想把图片存到数据库中的image,varchar,nvarchar,varbinary字段, 终于成功了.保存到数据库的代码如下: ? HttpPostedFile UpFile = UP_FILE.PostedFile;? //HttpPostedFile对象,用于读取图象文件属性 ??????? int FileLength = UpFile.ContentLength;???
今天想把图片存到数据库中的image,varchar,nvarchar,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>
??????????????????? 上传图片(选择你要上传的图片)&lt;/</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();

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读