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

sql轉換成二進制

發布時間: 2022-04-21 04:03:23

sql語句執行過程中會解析成二進制位被計算機識別嗎在哪一個階段被解析的

會解析成二進制的,和電腦交互的時候被解析的。

⑵ SQL server 2008十進制轉二進制

第三行是給變數賦值,單引號就代表賦值為空呀~
倒數第三行和上邊的代碼都是演算法呀~如果沒看出這里應該是除2取余轉的二進制~

⑶ 如何把用SQL語句int型整數轉換成二進制數

您好:

提供一個函數,請查收

CreateFUNCTIONDecToBin(@DecInt)
RETURNSVARCHAR(16)
AS
begin
declare@BinStrasvarchar(20)
declare@Mod2asint
declare@Div2asint

set@Div2=@Dec/2
set@Mod2=@Dec%2
set@BinStr=''

while@Div2<>0
begin
if@Mod2=0
set@BinStr=@BinStr+'0'
else
set@BinStr=@BinStr+'1'

set@Dec=@Dec/2
set@Div2=@Dec/2
set@Mod2=@Dec%2
end

set@BinStr=@BinStr+'1'

returnREVERSE(@BinStr+right('0000000000000000',16-len(@BinStr)))
end

⑷ sql server 2008 如何使圖片變為二進制

Sql不會使圖片變成二進制,你要在寫程序的時候把圖片轉換成二進制來進行存儲

⑸ 求大神指導,如何在sql語句中進行二進制運算

二進制?可是我怎麼在你的查詢結果中看到4,4是怎麼來的?

另外根據你提供的內容,我也有點疑惑。你說的「例如10,包含星期一星期二」,那麼假設每星期三是船期,那麼這個二進制怎麼表示?
個人覺得二進制的存儲辦法是一個7位數的二進制串,有船期顯示1,無船期顯示0.如果是周一到周三那麼就是0000111(也可能調過來),如果是周一和周三,那麼就可能是0000101。可是感覺資料庫不是這么存的,反正我沒看懂。

⑹ 怎樣在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)

⑺ 用T-SQL語句編寫一個程序,它的題目是將十進制轉換成二進制

alter proc prc_num @num bigint
as
declare @numstr varchar(50)
set @numstr=
while (@num<>0)
begin
set @numstr=@numstr+convert(char(1),@num%2)
set @num=@num/2
end
select reverse(@numstr)

測試:exec prc_num 8

⑻ sql中varbinary 是什麼數據類型

varbinary 類型和char與varchar類型是相似的,只是他們存儲的是二進制數據,也就是說他們是包含位元組流而不是字元流,他們有二進制字元的集合和順序,他們的對比,排序是基於位元組的數值進行的

binary與varbinary的最大長度和char與varchar是一樣的,只不過他們是定義位元組長度,而char和varchar對應的是字元長度。Varbinary是一個可以改變長度的二進制數據。

Varbinary[(n)] 是 n 位變長度的二進制數據。其中,n 的取值范圍是從 1 到 8000。其存儲窨的大小是 n + 4個位元組,不是n 個位元組。

varbinary在插入不會去填補0x00位元組,查詢的時候也不會丟棄任何位元組,在比較的時候,所有的位元組都是有效的,並且0x00<space (space對應的是0x20)。

(8)sql轉換成二進制擴展閱讀:

varbinary( n | max):可變長度,n 的取值范圍為 1 至 8,000,max 是指最大存儲空間是 2^31-1 個位元組,即最大4GB;

在將數據轉換為二進制數據時,SQL Server會對生成的二進制數據進行填充或截斷,詳細的規則是:

1、填充(或擴展)的二進制數據是16進制的0x00的整數倍,這就是說,填充的位元組中每一個bit都是0;

2、將字元類型轉換成二進制數據時,在數據的右側填充或截斷數據,填充數值是0x00;

3、將其他類型數據轉換成二進制數據時,在數據的左側填充或截斷數據,填充數值是0x00;在截斷數據時,保留低位的數據,將高位數值截斷。

⑼ SQL資料庫二進制數如何轉換

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();

⑽ 怎麼把圖片文件在SQL server資料庫中保存為二進制記錄(註:不是用圖片路徑),最好有源代碼。

首先在SQL Server中建立一個圖片存儲的數庫表,ImageData Column為圖象二進制數據儲存欄位,ImageContentType Column為圖象文件類型記錄欄位,
ImageDescription Column為儲蓄圖象文件說明欄位,ImageSize Column為儲存圖象文件長度欄位,結構如下:
CREATE TABLE [dbo].[ImageStore] (
[ImageID] [int] IDENTITY (1, 1) NOT NULL ,
[ImageData] [image] NULL ,
[ImageContentType] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
[ImageDescription] [varchar] (200) COLLATE Chinese_PRC_CI_AS NULL ,
[ImageSize] [int] NULL
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY] 向資料庫中存入圖片:using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Data.SqlClient;namespace UpLoadFile
{
/// <summary>
/// Summary description for UpLoadImage.
/// </summary>
public class UpLoadImage : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Button btnUpload;
protected System.Web.UI.WebControls.Label txtMessage;
protected System.Web.UI.WebControls.TextBox txtDescription;
protected System.Web.UI.HtmlControls.HtmlTable Table1;
protected System.Web.UI.HtmlControls.HtmlInputFile UP_FILE;//HtmlControl、WebControls控制項對象
protected Int32 FileLength = 0;

private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
if(!Page.IsPostBack)
{
}
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.btnUpload.Click += new System.EventHandler(this.btnUpload_Click);
this.Load += new System.EventHandler(this.Page_Load); }
#endregion private void btnUpload_Click(object sender, System.EventArgs e)
{
HttpPostedFile UpFile = this.UP_FILE.PostedFile; //HttpPostedFile對象,用於讀取圖象文件屬性
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,0,FileLength);
//建立SQL Server鏈接
SqlConnection Con = new SqlConnection("uid=sa;pwd= ;initial catalog=EE;data source=127.0.0.1;Connect Timeout=90");
String SqlCmd = "INSERT INTO ImageStore (ImageData, ImageContentType, ImageDescription, ImageSize) VALUES (@Image, @ContentType, @ImageDescription, @ImageSize)";
SqlCommand CmdObj = new SqlCommand(SqlCmd, Con);
CmdObj.Parameters.Add("@Image",SqlDbType.Binary, FileLength).Value = FileByteArray;
CmdObj.Parameters.Add("@ContentType", SqlDbType.VarChar,50).Value = UpFile.ContentType; //記錄文件類型
//把其它單表數據記錄上傳
CmdObj.Parameters.Add("@ImageDescription", SqlDbType.VarChar,200).Value = txtDescription.Text;
//記錄文件長度,讀取時使用
CmdObj.Parameters.Add("@ImageSize", SqlDbType.BigInt,8).Value = UpFile.ContentLength;
Con.Open();
CmdObj.ExecuteNonQuery();
Con.Close();
txtMessage.Text = "<p><b>OK!你已經成功上傳你的圖片</b>";//提示上傳成功
}
}
catch (Exception ex)
{
txtMessage.Text = ex.Message.ToString();
}

} }
}
將資料庫中的圖片數據讀出來顯示:using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Data.SqlClient;namespace UpLoadFile
{
/// <summary>
/// Summary description for ReadImage.
/// </summary>
public class ReadImage : System.Web.UI.Page
{
private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
if(!Page.IsPostBack)
{
string id = Request.QueryString["ImgID"]; //得到圖片的ID if (id != ""&& id != null && id != string.Empty)
{
ShowImage( id);
}
}
} #region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load); }
#endregion public void ShowImage(string id)
{
int ImgID = Convert.ToInt32(id); //ImgID為圖片ID
//建立資料庫鏈接
SqlConnection Con = new SqlConnection("uid=sa;pwd= ;initial catalog=EE;data source=127.0.0.1;Connect Timeout=90");
String SqlCmd = "SELECT * FROM ImageStore WHERE ImageID = @ImageID";
SqlCommand CmdObj = new SqlCommand(SqlCmd, Con);
CmdObj.Parameters.Add("@ImageID", SqlDbType.Int).Value = ImgID;
try
{
Con.Open();
SqlDataReader SqlReader = CmdObj.ExecuteReader();
SqlReader.Read();
Response.ContentType = (string)SqlReader["ImageContentType"];//設定輸出文件類型
//輸出圖象文件二進制數制
Response.OutputStream.Write((byte[])SqlReader["ImageData"], 0, (int)SqlReader["ImageSize"]);
Response.End();
Con.Close();
}
catch
{
Response.Write("<script>alert('該圖片不存在');</script>");
return;
} }
}
}