如何通过SQL函数将7个字节的十六进制数据转换为十进制,找了一个函数转换四个字节的可以,7个就不行了
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何通过SQL函数将7个字节的十六进制数据转换为十进制,找了一个函数转换四个字节的可以,7个就不行了相关的知识,希望对你有一定的参考价值。
错误提示:发生类型 int 的算术溢出错误,值 = 4294967296.000000。
求救,急~谢谢啊~
--建立数据库
create database test
--使用该数据库
use test
--建立存放图片的表
create table piclist(
id int Identity primary key,
pic Image not null
)
2、制作上传图片的模块,代码如下:
前台html代码:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="UpPhoto.aspx.cs" Inherits="Test_UpPhoto" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
<html xmlns="" >
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<input id="UpPhoto" name="UpPhoto" runat="server" type="file" />
<asp:Button id="btnAdd" runat="server" Text="上传" OnClick="btnAdd_Click"></asp:Button>
</div>
</form>
</body>
</html>
后台代码:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Data.SqlClient;
public partial class Test_UpPhoto : System.Web.UI.Page
protected void Page_Load(object sender, EventArgs e)
protected void btnAdd_Click(object sender, EventArgs e)
//获得图象并把图象转换为byte[]
HttpPostedFile upPhoto = UpPhoto.PostedFile;
int upPhotoLength = upPhoto.ContentLength;
byte[] PhotoArray = new Byte[upPhotoLength];
Stream PhotoStream = upPhoto.InputStream;
PhotoStream.Read(PhotoArray, 0, upPhotoLength);
//连接数据库
string ConStr = "server=(local);user id=sa;pwd=sa;database=test";
SqlConnection conn = new SqlConnection(ConStr);
string strSql = "Insert into piclist(pic) values(@pic)";
SqlCommand cmd = new SqlCommand(strSql, conn);
cmd.Parameters.Add("@pic", SqlDbType.Image);
cmd.Parameters["@pic"].Value = PhotoArray;
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
Response.Write("图片上传成功");
3、制作显示图片的模块(单独显示图片,即没用到datalist):
后台代码:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.IO;
public partial class Test_ShowPhoto : System.Web.UI.Page
protected void Page_Load(object sender, EventArgs e)
if(!Page.IsPostBack)
//连接数据库
string ConnStr = "server=(local);user id=sa;pwd=sa;database=test";
string strSql = "select * from piclist";
SqlConnection conn = new SqlConnection(ConnStr);
conn.Open();
SqlCommand cmd=new SqlCommand(strSql,conn);
SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
Response.ContentType = "application/octet-stream";
Response.BinaryWrite((Byte[])reader["pic"]);
Response.Write("successful");
reader.Close();
conn.Close();
Response.End();
补充步骤3,用datalist显示图片方法:
建立两个asp.net 页面,名称为piclist.aspx和StreamImg.aspx。
piclist.aspx前台代码为:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="piclist.aspx.cs" Inherits="Test_Test" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
<html xmlns="" >
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DataList ID="dlContent" runat="server" Width="554px">
<ItemTemplate>
<table cellpadding="0" cellspacing="0">
<tr>
<td style="width: 554px; text-align: left; background-image: url(Image/标头.jpg); height: 26px;">
<img id='img1' src='StreamImg.aspx?id= <%# DataBinder.Eval(Container.DataItem,"id") %>'>
</a>
</a>
</td>
</tr>
</table>
</ItemTemplate>
</asp:DataList>
</div>
</form>
</body>
</html>
piclist.aspx后台代码为:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.IO;
public partial class Test_Test : System.Web.UI.Page
protected void Page_Load(object sender, EventArgs e)
if (!Page.IsPostBack)
//连接数据库
string ConnStr = "server=(local);user id=sa;pwd=sa;database=test";
SqlConnection sqlcon = new SqlConnection(ConnStr);
sqlcon.Open();
string sqlstr = "select id from piclist";
SqlDataAdapter MyAdapter = new SqlDataAdapter(sqlstr, sqlcon);
DataSet ds = new DataSet();
MyAdapter.Fill(ds, "tb_pic");
this.dlContent.DataSource = ds;
this.dlContent.DataBind();
sqlcon.Close();
StreamImg.aspx无前台代码,后台代码为:
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.IO;
public partial class StreamImg : System.Web.UI.Page
protected void Page_Load(object sender, EventArgs e)
//string type = Request.QueryString["pt"];
int id = Convert.ToInt32(Request.QueryString["id"]);
ShowPic(id);
private void ShowPic(int id)
//连接数据库
string ConnStr = "server=(local);user id=sa;pwd=sa;database=test";
string strSql = "select * from piclist where id='"+ id +"'";
SqlConnection conn = new SqlConnection(ConnStr);
conn.Open();
SqlCommand cmd = new SqlCommand(strSql, conn);
SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
Response.ContentType = "application/octet-stream";
Response.BinaryWrite((Byte[])reader["pic"]);
Response.Write("successful");
reader.Close();
conn.Close();
Response.End();
参考技术A 这个值转换为16进制是"100000000",9个字节.你可以自己写一个函数.以下可供参考:
create function InttoHex(@numbigint,@hexLengthint)
returns varchar(100)
as
begin
declare@revarchar(100)
declare@bnbigint,@tempLenint
set@bn=@num
set@re=''
----------------------------------------
while (@num>0)
begin
set@re=substring('0123456789ABCDEF',@num%16+1,1)+@re
set@num=@num/16
end
----------------------------------------
set@tempLen=@hexLength-len(@re)
while(@tempLen> 0)
begin
set@re='0'+@re
set@tempLen=@tempLen- 1
end
return(@re)
end
GO
create function HextoInt (@hexSvarchar(16))
returns bigint
AS
begin
declare@iint,@resultbigint,@lenint
declare@powerbigint
set@power= 16
select@i= 0,@result= 0,@hexS=RTRIM(LTRIM(UPPER(@hexS)))
set@len=len(@hexS)
if (@len= 16)
begin
if (ascii(substring(@hexS, 1, 1))> 55)
begin
-- RaisError('超出数据运算范围', 1, 16)
return@result
end
end
-------------------------------------------------------
while (@i<@len)
begin
if ((substring(@hexS,@len-@i, 1)notbetween'0'and'9')
AND
(substring(@hexS,@len-@i, 1)notbetween'A'and'F'))
begin
set@result= 0
break;
end
----------------------------------------
set@result=@result+ (charindex(substring(@hexS,@len-@i, 1),'0123456789ABCDEF')- 1)*cast(power(@power,@i)asbigint)
set@i=@i+ 1
end
----------------------------------------------
return@result
end
GO
将金额转换为十万和千万
【中文标题】将金额转换为十万和千万【英文标题】:Converting amount to lakh and crore 【发布时间】:2017-04-20 23:43:44 【问题描述】:如何在 django 中将金额转换为 lakh and crore。例如,如果金额是 100 000,我想显示 10 万。是否可以通过使用自定义模板标签来做到这一点?
我是一个初学者,所以这是我为完成这项工作而编写的代码。但是如何在模板标签中使用呢?
if properties.expected_price >= 100000:
expected_price_in = expected_price/100000
elif properties.expected_price >= 1000000:
expected_price_in = expected_price/1000000
else:
expected_price_in = expected_price
【问题讨论】:
这个网址可能会有帮助:***.com/questions/4831306/… 小记:你的代码永远不会进入elif
我在那个链接中找不到任何有用的东西:(
我认为,在这种情况下,你需要去学习 Django 教程。
【参考方案1】:
你可以找到关于 Django 自定义模板过滤器here 和here 的很好的解释。
我只是给你一个简短的描述。
首先,您需要在 django 应用程序中创建文件夹模板标签并添加 __init__.py
文件。
在新文件夹中创建一些 .py 文件,例如 custom_filters.py
,内容如下:
from django import template
register = template.Library()
@register.filter
def num_format(value):
if value >= 1000000:
return value/1000000
elif value >= 100000:
return value/100000
else:
return value
现在你可以像这样在模板中使用它:
% load custom_filters %
your_number|num_format
但我也建议你看看humanize utils。说不定能解决你的问题。
只需添加到设置 INSTALLED_APPS 'django.contrib.humanize' 并在模板中尝试:
% load humanize %
your_number|intword
【讨论】:
以上是关于如何通过SQL函数将7个字节的十六进制数据转换为十进制,找了一个函数转换四个字节的可以,7个就不行了的主要内容,如果未能解决你的问题,请参考以下文章
在 C# 中将十六进制值从 SQL Server 转换为日期时间