1将以二进制存入数据库
//保存到数据库
protected void Button1_Click(object sender, EventArgs e)
{
//路径
string strPath = "~/photo/03JPG";
string strPhotoPath = ServerMapPath(strPath);
//读取
FileStream fs = new SystemIOFileStream(strPhotoPath, FileModeOpen, FileAccessRead);
BinaryReader br = new BinaryReader(fs);
byte[] photo = brReadBytes((int)fsLength);
brClose();
fsClose();
//存入
SqlConnection myConn = new SqlConnection("Data Source=127001;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);
myCommParametersAdd("@photoBinary", SqlDbTypeBinary,photoLength);
myCommParameters["@photoBinary"]Value = photo;
myConnOpen();
myCommExecuteNonQuery();
myConnClose();
}
2读取二进制在页面显示
//读取
SqlConnection myConn = new SqlConnection("Data Source=127001;Initial Catalog=TestDB;User ID=sa;Password=sa");
string strComm = " SELECT personPhoto FROM personPhoto WHERE personName='wangwu' ";
SqlCommand myComm = new SqlCommand(strComm, myConn);
myConnOpen();
SqlDataReader dr = myCommExecuteReader();
while (drRead())
{
byte[] photo = (byte[])dr["personPhoto"];
thisResponseBinaryWrite(photo);
}
drClose();
myConnClose();
或
SqlConnection myConn = new SqlConnection("Data Source=127001;Initial Catalog=TestDB;User ID=sa;Password=sa");
SqlDataAdapter myda = new SqlDataAdapter(" SELECT personPhoto FROM personPhoto WHERE personName='11' ", myConn);
DataSet myds = new DataSet();
myConnOpen();
mydaFill(myds);
myConnClose();
byte[] photo = (byte[])mydsTables[0]Rows[0]["personPhoto"];
thisResponseBinaryWrite(photo);
3设置Image控件显示从数据库中读出的二进制
---------------------------------------------
SqlConnection myConn = new SqlConnection("Data Source=19216801;Initial Catalog=TestDB;User ID=sa;Password=sa");
SqlDataAdapter myda = new SqlDataAdapter(" SELECT personPhoto FROM personPhoto WHERE personName='11' ", myConn);
DataSet myds = new DataSet();
myConnOpen();
mydaFill(myds);
myConnClose();
byte[] photo = (byte[])mydsTables[0]Rows[0]["personPhoto"];
//路径
string strPath = "~/photo/wangwuJPG";
string strPhotoPath = ServerMapPath(strPath);
//保存文件
BinaryWriter bw = new BinaryWriter(FileOpen(strPhotoPath,FileModeOpenOrCreate));
bwWrite(photo);
bwClose();
3显示
thisImage1ImageUrl = strPath;
4GridView中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>
5GridView显示读出的二进制
//样板列
<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 (eRowRowIndex < 0)
return;
// SystemComponentModelContainer
string strPersonName = (string)DataBinderEval(eRowDataItem, "personName");
Image tmp_Image = (Image)eRowCells[2]FindControl("Image1");
if (!SystemConvertIsDBNull(DataBinderEval(eRowDataItem, "personPhoto")))
{
//
byte[] photo = (byte[])DataBinderEval(eRowDataItem, "personPhoto");
//路径
string strPath = "~/photo/" + strPersonNameTrim() + "JPG";
string strPhotoPath = ServerMapPath(strPath);
//保存文件
BinaryWriter bw = new BinaryWriter(FileOpen(strPhotoPath, FileModeOpenOrCreate));
bwWrite(photo);
bwClose();
//显示
tmp_ImageImageUrl = strPath;
}
}
dim
mst
as
stream
dim
rs
as
adodbrecordset
----------------------------------------
set
mst
=
new
adodbstream
msttype
=
adtypebinary
'二进制类型
mstopen
mstloadfromfile
mystr
'加载,mystr为路径
----------------------------------------
rsfields("images")=
mstread
'保存到数据库
这是保存的主要代码
private int WriteToDB(string strName, string strType, ref byte[] Buffer) { int nFileID = 0; // Create connection OleDbConnection dbConn = new OleDbConnection(GetConnectionString()); // Create Adapter OleDbDataAdapter dbAdapt = new OleDbDataAdapter("SELECT FROM tblFile", dbConn); // We need this to get an ID back from the database dbAdaptMissingSchemaAction = MissingSchemaActionAddWithKey; // Create and initialize CommandBuilder OleDbCommandBuilder dbCB = new OleDbCommandBuilder(dbAdapt); // Open Connection dbConnOpen(); // New DataSet DataSet dbSet = new DataSet(); // Populate DataSet with data dbAdaptFill(dbSet, "tblFile"); // Get reference to our table DataTable dbTable = dbSetTables["tblFile"]; // Create new row DataRow dbRow = dbTableNewRow(); // Store data in the row dbRow["FileName"] = strName; dbRow["FileSize"] = BufferLength; dbRow["ContentType"] = strType; dbRow["FileData"] = Buffer; // Add row back to table dbTableRowsAdd(dbRow); // Update data source dbAdaptUpdate(dbSet, "tblFile"); // Get newFileID if( !dbRowIsNull("FileID") ) nFileID = (int)dbRow["FileID"]; // Close connection dbConnClose(); // Return FileID return nFileID; } 写入库。 private void ShowTheFile(int FileID) { // Define SQL select statement string SQL = "SELECT FileSize, FileData, ContentType FROM tblFile WHERE FileID = " + FileIDToString(); // Create Connection object OleDbConnection dbConn = new OleDbConnection(GetConnectionString()); // Create Command Object OleDbCommand dbComm = new OleDbCommand(SQL, dbConn); // Open Connection dbConnOpen(); // Execute command and receive DataReader OleDbDataRea
先说保存,将你的二进制文件先加工成你要存的内容,其实也就是写二进制文件的 *** 作了,比如说你在第几个字节起,开始插入乱码,加入你的自定义内容,这些内容固定就行了,有几个字节要记清楚,相当于是把你的二进制文件分为两部分,在中间加入你要加的内容,
再说读取
既然是以二进制流存到表中的,读出来的时候,就for binary输出到二进制文件,输出时使用二进制方式,输出文件分成三段,第一段就是开始到你插入内容的那个字节结尾,第二段就是你加的内容的字节数,第三段是你插入内容的最后一个字节末尾开始到文件结尾,然后将你添加的字节也就是第二段去掉,再把一三两段合成一个文件
以上就是关于如何在mssql中二进制储存图片全部的内容,包括:如何在mssql中二进制储存图片、如何把一个二进制数据存到数据库、如何将txt,doc等文件以二进制形式保存在数据库中等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)