導航:首頁 > 文件教程 > sql二進制文件

sql二進制文件

發布時間:2023-10-03 12:53:51

Ⅰ SQL資料庫 二進制圖片如何導出成文件

SQL資料庫 二進制圖片如何導出成文件
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控制項顯示從資料庫中讀出的二進制圖片

Ⅱ 如何從SQL資料庫中讀取二進制數據

讀出並生成圖片到物理位置
public void Read()
{
byte[] MyData = new byte[0];
using (SqlConnection conn = new SqlConnection(sqlconnstr))
{
conn.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandText = "select * from T_img";
SqlDataReader sdr = cmd.ExecuteReader();
sdr.Read();
MyData = (byte[])sdr["ImgFile"];//讀取第一個圖片的位流
int ArraySize= MyData.GetUpperBound(0);//獲得資料庫中存儲的位流數組的維度上限,用作讀取流的上限

FileStream fs = new FileStream(@"c:\00.jpg", FileMode.OpenOrCreate, FileAccess.Write);
fs.Write(MyData, 0, ArraySize);
fs.Close(); //-- 寫入到c:\00.jpg。
conn.Close();
Console.WriteLine("讀取成功");//查看硬碟上的文件
}
}

閱讀全文

與sql二進制文件相關的資料

熱點內容
access資料庫查詢生成表 瀏覽:661
nodejsforever配置 瀏覽:181
蘿卜網路什麼意思 瀏覽:925
steam地平線4存檔在哪個文件夾 瀏覽:870
JAVA下載壓縮文件包括文件夾 瀏覽:277
上古5法術代碼 瀏覽:235
將文件列印成pdf格式 瀏覽:540
ug導出cad為什麼沒有文件 瀏覽:469
怪物獵人3g弓升級 瀏覽:373
java隨機4位字元驗證碼 瀏覽:589
前端postjson數據 瀏覽:462
plc初次編程如何清理原有程序 瀏覽:204
蘋果6plus手機美版序列號 瀏覽:6
c語言怎麼做成app 瀏覽:501
u盤共享文件能中病毒嗎 瀏覽:941
騰訊版權視頻文件加密什麼意思 瀏覽:632
編程里f4是什麼意思 瀏覽:627
帝國時代2scx文件 瀏覽:658
什麼酷的app 瀏覽:59
自學手機編程哪裡好 瀏覽:637

友情鏈接