當前位置:首頁 » 編程語言 » sqlserver轉二進制
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

sqlserver轉二進制

發布時間: 2022-06-16 19:50:40

A. c#怎麼存取sqlserver資料庫binary類型,如下題

//把文件轉成二進制流出入資料庫
privatevoidbutton2_Click(objectsender,EventArgse)
{
FileStreamfs=newFileStream(textBox1.Text,FileMode.Open);
BinaryReaderbr=newBinaryReader(fs);
Byte[]byData=br.ReadBytes((int)fs.Length);
fs.Close();
stringconn="server=.;database=testDB;Uid=sa;Pwd=sa";
SqlConnectionmyconn=newSqlConnection(conn);
myconn.Open();
stringstr="insertintopro_table(pro_name,pro_file)values('測試文件',@file)";
SqlCommandmycomm=newSqlCommand(str,myconn);
mycomm.Parameters.Add("@file",SqlDbType.Binary,byData.Length);
mycomm.Parameters["@file"].Value=byData;
mycomm.ExecuteNonQuery();
myconn.Close();
}

//從資料庫中把二進制流讀出寫入還原成文件
privatevoidbutton4_Click(objectsender,EventArgse)
{
stringconn="server=.;database=testDB;Uid=sa;Pwd=sa";
stringstr="selectpro_filefrompro_tablewherepro_name='測試文件'";
SqlConnectionmyconn=newSqlConnection(conn);
SqlDataAdaptersda=newSqlDataAdapter(str,conn);
DataSetmyds=newDataSet();
myconn.Open();
sda.Fill(myds);
myconn.Close();
Byte[]Files=(Byte[])myds.Tables[0].Rows[0]["pro_file"];
BinaryWriterbw=newBinaryWriter(File.Open("D:\2.rdlc",FileMode.OpenOrCreate));
bw.Write(Files);
bw.Close();

}

B. 怎麼把Sqlserver資料庫,裡面的image格式的二進制 修改掉 更換圖片

二進制欄位只能存二進制的數據,如果要改變存儲形式則必須要改欄位的類型

C. 怎樣在sqlserver2008中用sql語句操作二進制數據

sqlserver之二進制和字元串sql語句
正常情況下我們對資料庫的操作就是如下的寫法來操作資料庫
SELECT TOP 10 ID AS 編號,BookName AS 書名 FROM dbo.books ORDER BY ID;

UPDATE dbo.books SET BookName='新的書名' WHERE ID=1233;

DELETE FROM dbo.books WHERE ID=122

但是在客戶正在使用的資料庫里,我們開發人員一般不能夠直接操作資料庫,但是會給我們做一個網頁以便方便我們核對數據,查找錯誤,但是這種情況下一般都會屏蔽一些關鍵詞,比如update delete,create,alter神馬的,一般請客下對客戶資料庫的操作都得嚴格按照公司流程來走,這種情況下效率一般都會很低,在這里還有一種情況可以直接讓我們對資料庫做更改,那就是首先將字元串以二進制的形式騙過後台程序,以便發送到資料庫中去執行,如下:
DECLARE @S NVARCHAR(4000)
SET @S=CAST( AS VARCHAR(max))
PRINT @S
EXEC(@S)

下面便是直接把sql語句轉換成二進制

DECLARE @str VARCHAR(MAX),@bary VARBINARY(MAX)
SET @str='SELECT TOP 10 ID AS 編號,BookName AS 書名 FROM dbo.books ORDER BY ID;'

--將字元串轉換成二進制對象
SET @bary= CAST(@str AS VARBINARY(MAX))
PRINT @bary

--將二進制對象轉換成字元串
SET @str=CAST(@bary AS VARCHAR(max))
--執行sql腳本
EXEC(@str)

D. sql Server如何編寫函數實現把十進制數轉換為二進制數求大神相助!!

創建函數

createFUNCTION[dbo].[Dec2Bin](@DecINT,@StrLenINT)
RETURNSVARCHAR(31)--INT型,4位元組,正數轉為二進制字元串最多31位
AS
BEGIN
DECLARE@BinStrASVARCHAR(31)--二進製表示的字元串
DECLARE@Div2ASINT--商
DECLARE@Mod2ASINT--模/余數
IF@Dec<0
RETURN'NULL'--不支持負數的轉換
SET@Div2=@Dec/2
SET@Mod2=@Dec%2
SET@BinStr=''
WHILE@Div2<>0
BEGIN
SET@BinStr=CAST(@Mod2ASCHAR(1))+@BinStr
SET@Dec=@Dec/2
SET@Div2=@Dec/2
SET@Mod2=@Dec%2

END;
SET@BinStr=CAST(@Mod2ASCHAR(1))+@BinStr--至此,已完成十進制到二進制的轉換
IF@StrLen>LEN(@BinStr)--如果用戶指定的長度大於實際長度,則需要左邊補0
BEGIN
IF@StrLen>31--返回的長度,最長為32
SET@StrLen=31

DECLARE@ZeroStrVARCHAR(31)--需要補充的"0000..."
DECLARE@OffsetLenTINYINT--需要補充幾個"0"
SET@ZeroStr=''
SET@OffsetLen=@StrLen-LEN(@BinStr)

WHILE@OffsetLen>0
BEGIN
SET@ZeroStr=@ZeroStr+'0'
SET@OffsetLen=@OffsetLen-1
END

SET@BinStr=@ZeroStr+@BinStr
END
RETURN@BinStr
END

調用函數

select[dbo].[Dec2Bin](3,8)

其中3是要轉換的數字,8是最後二進制的長度,不足位的前補0

E. sqlserver 資料庫保存二進制數據流出錯,大蝦幫忙看下哪裡有問題,沒有分了,就這點分散了

轉換C盤命令是:開始-運行-cmd-convert c:/fs:ntfs
非系統盤轉換比較容易,直接右鍵格式化,選中NFS格式即可,或者用一般的格式轉換軟體都可以,一般不會造成不好影響。但系統盤(一般是C盤)就難度大了,重裝系統是最好的辦法,但你也可以試試Paragon Hard Disk Manager 這個軟體,但強烈不建議轉換系統盤,弄不好會出現莫名其妙的問題。所有盤轉化前都最好要備份好你的重要資料!

F. sqlserver 二進制數據存儲問題

這種欄位我記得只要給個byte數組就可以了,不用轉移成binary

G. 我用來讀取sqlserver資料庫中二進制數據讀到文件中是這樣的

編碼格式問題吧
看你寫入資料庫用的什麼編碼格式了
如果ASCII:
string info = System.Text.Encoding.ASCII.GetString(data );
如果UTF8:
string info = System.Text.Encoding.UTF8.GetString(data );

然後再把info寫入文件中,就不用說了吧

H. sqlserver資料庫中img類型的二進制數據怎麼轉換成圖片,再怎麼把圖片轉換成這個類型的二進制數據

C# 保存
openFileDialog1.Filter = "*jpg|*.JPG|*.GIF|*.GIF|*.BMP|*.BMP";             if(openFileDialog1.ShowDialog()==DialogResult.OK)             { 
              string fullpath =openFileDialog1.FileName;//文件路徑               FileStream fs = new FileStream(fullpath, FileMode.Open);                 byte[] imagebytes =new byte[fs.Length];                 BinaryReader br = new BinaryReader(fs); 
                imagebytes = br.ReadBytes(Convert.ToInt32(fs.Length));                 //打開資料庫 
                SqlConnection con = new 
SqlConnection("server=(local);uid=sa;pwd=;database=test");                 con.Open(); 
                SqlCommand com = new SqlCommand("insert into tb_08 values(@ImageList)",con); 
                com.Parameters.Add("ImageList", SqlDbType.Image);                 com.Parameters["ImageList"].Value = imagebytes;                com.ExecuteNonQuery();                con.Close();

I. 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;
}
}

J. 用php將sqlserver中的二進制流欄位(類型為image)讀取出來顯示的是「」是什麼原因

檢查下 資料庫的編碼和顯示時候的編碼,一般出現亂碼都是編碼的問題,建議重新設置下編碼