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)读取出来显示的是“”是什么原因
检查下 数据库的编码和显示时候的编码,一般出现乱码都是编码的问题,建议重新设置下编码