怎样在sqlserver2008中用sql语句操作二进制数据
成都创新互联公司是专业的临汾网站建设公司,临汾接单;提供网站设计、成都网站建设,网页设计,网站设计,建网站,PHP网站建设等专业做网站服务;采用PHP框架,可快速的进行临汾网站开发网页制作和功能扩展;专业做搜索引擎喜爱的网站,专业的做网站团队,希望更多企业前来合作!
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
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;
}
}
目前SqlServer没有单表恢复,相对於oracle确实是一个不太方便的事情。
你可以将数据恢复到一个新数据库,然後将误删的数据插入到目前A表中,这样就不会影响其他数据了。
存储图片:以二进制的形式存储图片时,要把数据库中的字段设置为Image数据类型(SQL Server),存储的数据是Byte[]
先程序中把MP3文件读取为byte数组,然后就可以保存到数据库中二进制字段VARBINARY(max)中
求采纳
主要涉及 binary varbinary image 这三个字段类型吧
请仔细了解sqlserver 各种数据类型
sql二进制数据类型详解
如有疑问,及时沟通!