sqlserver存储二进制数据
1. sqlserver数据库存储的图片格式(二进制数据)怎么显示到页面
把图片的二进制数据先读取出来,另存为图片格式的文件,然后再用页面脚本显示该图片文件。
2. sqlserver 二进制数据存储问题
这种字段我记得只要给个byte数组就可以了,不用转移成binary
3. 在SQL Server中,共使用了3种数据类型来存储二进制数据,分别是______、______、
主要涉及 binary varbinary image 这三个字段类型吧
请仔细了解sqlserver 各种数据类型
sql二进制数据类型详解
如有疑问,及时沟通!
4. Sqlserver数据库存储的图片格式(二进制数据)怎么显示到页面
1.将图片以二进制存入数据库
//保存图片到数据库
protected void Button1_Click(object sender, EventArgs e)
{
//图片路径
string strPath = "~/photo/03.JPG";
string strPhotoPath = Server.MapPath(strPath);
//读取图片
FileStream fs = new System.IO.FileStream(strPhotoPath, FileMode.Open, FileAccess.Read);
BinaryReader br = new BinaryReader(fs);
byte[] photo = br.ReadBytes((int)fs.Length);
br.Close();
fs.Close();
//存入
SqlConnection myConn = new SqlConnection("Data Source=127.0.0.1;Initial Catalog=TestDB;User ID=sa;Password=sa");
string strComm = " INSERT INTO personPhoto(personName, personPhotoPath, personPhoto) ";
strComm += " VALUES('wangwu', '" + strPath + "', @photoBinary )";
SqlCommand myComm = new SqlCommand(strComm, myConn);
myComm.Parameters.Add("@photoBinary", SqlDbType.Binary,photo.Length);
myComm.Parameters["@photoBinary"].Value = photo;
myConn.Open();
myComm.ExecuteNonQuery();
myConn.Close();
}
2.读取二进制图片在页面显示
//读取图片
SqlConnection myConn = new SqlConnection("Data Source=127.0.0.1;Initial Catalog=TestDB;User ID=sa;Password=sa");
string strComm = " SELECT personPhoto FROM personPhoto WHERE personName='wangwu' ";
SqlCommand myComm = new SqlCommand(strComm, myConn);
myConn.Open();
SqlDataReader dr = myComm.ExecuteReader();
while (dr.Read())
{
byte[] photo = (byte[])dr["personPhoto"];
this.Response.BinaryWrite(photo);
}
dr.Close();
myConn.Close();
或
SqlConnection myConn = new SqlConnection("Data Source=127.0.0.1;Initial Catalog=TestDB;User ID=sa;Password=sa");
SqlDataAdapter myda = new SqlDataAdapter(" SELECT personPhoto FROM personPhoto WHERE personName='11' ", myConn);
DataSet myds = new DataSet();
myConn.Open();
myda.Fill(myds);
myConn.Close();
byte[] photo = (byte[])myds.Tables[0].Rows[0]["personPhoto"];
this.Response.BinaryWrite(photo);
3.设置Image控件显示从数据库中读出的二进制图片
---------------------------------------------
SqlConnection myConn = new SqlConnection("Data Source=192.168.0.1;Initial Catalog=TestDB;User ID=sa;Password=sa");
SqlDataAdapter myda = new SqlDataAdapter(" SELECT personPhoto FROM personPhoto WHERE personName='11' ", myConn);
DataSet myds = new DataSet();
myConn.Open();
myda.Fill(myds);
myConn.Close();
byte[] photo = (byte[])myds.Tables[0].Rows[0]["personPhoto"];
//图片路径
string strPath = "~/photo/wangwu.JPG";
string strPhotoPath = Server.MapPath(strPath);
//保存图片文件
BinaryWriter bw = new BinaryWriter(File.Open(strPhotoPath,FileMode.OpenOrCreate));
bw.Write(photo);
bw.Close();
3.显示图片
this.Image1.ImageUrl = strPath;
4.GridView中ImageField以URL方式显示图片
--------------------------
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:BoundField DataField="personName" HeaderText="姓名" />
<asp:ImageField DataImageUrlField="personPhotoPath"
HeaderText="图片">
</asp:ImageField>
</Columns>
</asp:GridView>
5.GridView显示读出的二进制图片
//样板列
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" OnRowDataBound="GridView1_RowDataBound">
<Columns>
<asp:BoundField DataField="personName" HeaderText="姓名" />
<asp:ImageField DataImageUrlField="personPhotoPath"
HeaderText="图片">
</asp:ImageField>
<asp:TemplateField HeaderText="图片">
<ItemTemplate>
<asp:Image ID="Image1" runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowIndex < 0)
return;
// System.ComponentModel.Container
string strPersonName = (string)DataBinder.Eval(e.Row.DataItem, "personName");
Image tmp_Image = (Image)e.Row.Cells[2].FindControl("Image1");
if (!System.Convert.IsDBNull(DataBinder.Eval(e.Row.DataItem, "personPhoto")))
{
//
byte[] photo = (byte[])DataBinder.Eval(e.Row.DataItem, "personPhoto");
//图片路径
string strPath = "~/photo/" + strPersonName.Trim() + ".JPG";
string strPhotoPath = Server.MapPath(strPath);
//保存图片文件
BinaryWriter bw = new BinaryWriter(File.Open(strPhotoPath, FileMode.OpenOrCreate));
bw.Write(photo);
bw.Close();
//显示图片
tmp_Image.ImageUrl = strPath;
}
}
5. SQLServer存储二进制图片用什么类型
存储图片:以二进制的形式存储图片时,要把数据库中的字段设置为Image数据类型(SQL Server),存储的数据是Byte[]
6. sqlserver 数据库保存二进制数据流出错,大虾帮忙看下哪里有问题,没有分了,就这点分散了
转换C盘命令是:开始-运行-cmd-convert c:/fs:ntfs
非系统盘转换比较容易,直接右键格式化,选中NFS格式即可,或者用一般的格式转换软件都可以,一般不会造成不好影响。但系统盘(一般是C盘)就难度大了,重装系统是最好的办法,但你也可以试试Paragon Hard Disk Manager 这个软件,但强烈不建议转换系统盘,弄不好会出现莫名其妙的问题。所有盘转化前都最好要备份好你的重要资料!
7. 将截断字符串或二进制数据
报这个错误 是因为你插入的字符串长度超过了你定义的字符串长度\x0d\x0a就是你那个nchar 你上面定义了3个这样的类型 \x0d\x0a要一个个的排除了 看你的表files 对应的字段哪个字段比你传入的字符串长度短就是了\x0d\x0a还有就是变量的赋值也不能超过变量定义的长度 \x0d\x0a\x0d\x0a总结一下,有两原因:\x0d\x0a一、变量的赋值不能超过变量定义的长度\x0d\x0a二、表files 对应的字段的长度要和变量传入的字符串长度相同
8. 二、数据库与数据表--2、SQLServer的数据类型
(1)文本类型: 字符数据包含任意字母、符号或数字字符的组合
char: 固定长度的非Unicode字符数据,最大长度为8000个字符
varchar: 可变长度的非Unicode数据,最大长度为8000个字符
text: 存储长文本信息,最大长度为2^31-1(2147483647)个字符
nchar: 固定长度的Unicode数据,最大长度为4000个字符
narchar: 可变长度的Unicode数据,最大长度为4000个字符
ntext: 存储长文本信息,最大长度为2^30-1(1073741823)个字符
(Unicode是国际组织制定的可以容纳世界上所有文字和符号的字符编码方案,编码数据长度是非编码数据的两倍)
(2)整数类型
bigint: 占用8个字节,可表示范围-2^63~2^63-1之间的整数
int: 占用4个字节
smallint: 占用2个字节
tinint: 占用1个字节,可表示0~255之间的整数
(3)精确数字类型
decimal: -10^38~10^38-1之间固定精度和小数位的数字
numeric(常用): 功能等同于decimal
写法:
decimal(整数,小数)和numeric(整数,小数)——若不指定位数,默认18位整数,0位小数:
(4)近似数字(浮点)类型(很少用,可以用numeric替代)
float[(n)]: 表示范围-1.79E+308~1.79E+308
n 表示精度,在1-53之间取值,当n在1~24之间时,精度为7位有效数字,占用4个字节;当n在25~53之间时,精度为15位有效数字,占8个字节。
real: 表示范围-3.40E+38~3.40E+38占用4个字节,相当于float(24)
(5)日期类型
datetime: 允许范围1753-1-1至9999-1-1
smalldatetime(常用): 允许范围1900-1-1至2079-6-6
时间精度不同: datetime精确到3%秒;smalldatetime精确到1分钟
格式说明:
分隔符数字方式: 2013-08-20 或 08/20/2013
纯数字方式: 08202013
英文数字类型: Aug 20,2013
注意问题: 日期在使用的时候需要使用单引号' ' 括起来
(6)货币类型
money: 货币数值介于-2^63与2^63-1之间,精确到货币单位的千分之一
smallmoney: 货币数据介于-214748.3648与214748.3648之间,精确到货币单位的千分之十
(7)位类型
bit: 表示“是/否”类型的数据(1/0,true/false)
(8)二进制类型
binary: 固定长度的二进制数据,最大8000个字节
vbinary: 可变长度的二进制数据,最大8000个字节
image: 可变长度的二进制数据,最大长度2^31个字节—— 应用场合: 可存储图片
9. SqlServer转mysql 字段注释sql脚本
SELECT 'alter table ' + LOWER(d.name) + ' modify column `' + a.name + '` ' + b.name
+ '(' + RTRIM(CONVERT(char, COLUMNPROPERTY(a.id,a.name,'PRECISION')) ) + ') comment ''' + RTRIM(REPLACE(CONVERT(char, g.value), '''', '')) + ''';'
FROM syscolumns a
left join systypes b on a.xtype=b.xusertype
inner join sysobjects d on a.id=d.id and d.xtype='U' and d.name<>'dtproperties'
left join sys.extended_properties g on a.id=g.major_id AND a.colid=g.minor_id
where b.name is not null
AND g.VALUE is not null
order by a.id,a.colorder
问题: 1、SqlServer 的image类型需要转为mysql的Blob类型
BLOB类型,用于存储二进制数据。
MySQL中,BLOB是个类型系列,包括:TinyBlob、Blob、MediumBlob、LongBlob,
最大存储分别为:
TinyBlob 最大 255
Blob 最大 65K
MediumBlob 最大 16M
LongBlob 最大 4G;
mmsql的image可以存储图片;mysql的blob可以存储图片、文档等,功能更加强大。
2、datetime(23) -> datetime(0)
10. 将截断字符串或二进制数据
Bit数据类型在SQLServer数据库中以存储1、0进行存储。
如果它是连通的,那么它连续同构于R或C(关于通常绝对值的拓扑);如果它是完全不连通的,那么它就连续同构于p进数域Qp的一个有限扩域,或者某个有限域K上的形式幂级数域K((x))的有限扩域。
1.若使用SQL语句在查询分析器中进行修改,需要传入1、0。在SQL语句中也只能用0或者1,比如"wheresex=1"不能用"wheresex=TURE"。
2.若使用企业管理器直接在表中进行修改,需要使用True、False。使用SqlDataReader读出的Bit类型数据返回值为bool类型,若将返回结果关联到GridView中,那么此列下呈现的是复选框,复选框的Checked属性绑定Bit类型字段的返回值。

(10)sqlserver存储二进制数据扩展阅读:
har与varchar的比较:
1、数据存储开销:每个varchar列需要额外的两个字节,用于反映存储的数据的长度。每个可为NULL的char列,需要一些字节(空位图)来反应数据的为空性。无论实际数据的长度是多少,char按照定义的长度分配存储空间。
2、NULL值:char列的NULL值占用存储空间,varcahr列的NULL值不占用存储空间。插入同样数量的NULL值,varchar列的插入效率明显高出char列。
