用c#将照片插入sqlserver [英] inserting photo into sqlserver with c#
本文介绍了用c#将照片插入sqlserver的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用C#代码在sql server 2005中插入照片?
解决方案
并且...到目前为止你尝试了什么?
网上有很多关于如何将图像插入数据库的文章。主题/工具:
1.使用ADO.NET
2.数据库表设计存储图像
3.查询
试试看,具体一点。
以下是询问者的预期:
1. 首先尝试你想做什么!你可能会发现它并不那么难。
2.制定你所做的看似问题/不工作的事情。
< blockquote>你好!
试试这个:
1)你需要了解一个关于你的文件的一切尝试上传
使用此(BellUpload是一个asp:FileUpload控件名称):
FileInfo fi = new FileInfo(BellUpload.FileName);
2)你需要一个内存缓冲区:
字节 [] buffer = new 字节 [BellUpload.PostedFile.ContentLength];
3)你需要将文件读入内存缓冲区
file.InputStream.Read(buffer, 0 ,BellUpload.PostedFile.ContentLength);
4)你需要在mssql中创建一个表
5)我正在使用存储加载文件的过程:
ALTER PROCEDURE [dbo]。[UploadFile]
@ WPS nvarchar ( 50 ),
@ FileName nvarchar ( 255 ),
@ FileType nvarchar ( 50 ),
@ FileContent varbinary (MAX),
@ FileSize bigint
AS
BEGIN
insert < span class =code-keyword> into Files([WPS],[FileName],[FileType],[FileContent])
值( @ WPS , @ FileName , @ FileType , @ FileContent )
END
6)最后你需要通过SQL类上传它
使用(SqlConnection _con = new SqlConnection( 数据源= .....))
使用(SqlCommand _cmd = < span class =code-keyword> new SqlCommand( UploadFile,_ zh) )
{
_cmd.CommandType = CommandType.StoredProcedure;
_cmd.Parameters.AddWithValue( @ WPS,WPSfield.Text.Trim() );
_cmd.Parameters.AddWithValue( @ FileName,fi.Name);
_cmd.Parameters.AddWithValue( @ FileType,file.ContentType);
_cmd.Parameters.AddWithValue( @ FileContent,buffer);
_cmd.Parameters.AddWithValue( @ FileSize,BellUpload.PostedFile.ContentLength);
_con.Open();
_cmd.ExecuteNonQuery();
_con.Close();
}
希望有所帮助。
I am trying to insert photo in sql server 2005 with C# code?
解决方案
And... what have you tried so far?
There are number of articles on web on how to insert an image into database. Topics/Tools:
1. Use ADO.NET
2. DB table design to have image stored
3. Query
Try out, be specific.
Here is what is expected of enquirers:
1. TRY first what you want to do! You may find that it''s not that hard.
2. Formulate what was done by you that looks like an issue/not working.
Hello!
Try this:
1) You need to know everything about a file you a trying to upload
Use this(BellUpload is a asp:FileUpload control name):
FileInfo fi = new FileInfo(BellUpload.FileName);
2) You need a memory buffer:
Byte[] buffer = new Byte[BellUpload.PostedFile.ContentLength];
3) You need to read the file in to the memory buffer
file.InputStream.Read(buffer, 0, BellUpload.PostedFile.ContentLength);
4) You need to create a table in mssql
5) I''m using stored procedure to load a file:
ALTER PROCEDURE [dbo].[UploadFile] @WPS nvarchar(50), @FileName nvarchar(255), @FileType nvarchar(50), @FileContent varbinary(MAX), @FileSize bigint AS BEGIN insert into Files ([WPS],[FileName], [FileType], [FileContent]) values (@WPS, @FileName, @FileType, @FileContent) END
6)Finally you need to upload it by SQL classes
using (SqlConnection _con = new SqlConnection("Data Source=.....")) using (SqlCommand _cmd = new SqlCommand("UploadFile", _con)) { _cmd.CommandType = CommandType.StoredProcedure; _cmd.Parameters.AddWithValue("@WPS", WPSfield.Text.Trim()); _cmd.Parameters.AddWithValue("@FileName", fi.Name); _cmd.Parameters.AddWithValue("@FileType", file.ContentType); _cmd.Parameters.AddWithValue("@FileContent", buffer); _cmd.Parameters.AddWithValue("@FileSize", BellUpload.PostedFile.ContentLength); _con.Open(); _cmd.ExecuteNonQuery(); _con.Close(); }
Hope it helps.
这篇关于用c#将照片插入sqlserver的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文