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列。
