Saving image file using Fileupoad Control and sqld

2019-07-18 13:28发布

I want to add a image file to my database using sqldatasource .

steps that i have taken

1.Coverting image to Byte[] array.

Stream fs = FileUpload1.PostedFile.InputStream; 
BinaryReader br = new BinaryReader(fs); 
Byte[] bytes = br.ReadBytes((Int32)fs.Length); 

2.Setting the update parameter type as Object

<UpdateParameters>       
<asp:Parameter Name="File" Type="Object" /> 
</UpdateParameters> 

3.Adding byte array as update parameter in SqlDataSource control.

sqlDataSource.UpdateParameters["File"].DefaultValue = bytes;

but when i compile this code it is giving me a error "is giving me a error

"Cannot Implicitly convert byte[] to string" at third step

2条回答
狗以群分
2楼-- · 2019-07-18 14:13

Try to set parameter in Updating event

private void sqlDataSource_Updating(object sender, SqlDataSourceCommandEventArgs e)
{
    e.Command.Parameters["File"].Value = bytes;
}
查看更多
成全新的幸福
3楼-- · 2019-07-18 14:13

you should make data type varbinary

and made datatype varbinary(max) in your database

查看更多
登录 后发表回答