2011-09-02 1 views
1

가능한 중복을 이미지를 업로드하는 방법 : SQL 서버로 C# 응용 프로그램에서 이미지를 업로드하는 방법
Upload image to server using C#/.NET and storing filename in DBSQL Server에 C# 응용 프로그램에서 2005

2005

+1

[C#/.NET을 사용하여 서버에 이미지를 업로드하고 DB에 파일 이름을 저장하는 중] 중복 (http://stackoverflow.com/questions/6054359/upload-image-to-server-using-c-net-and-storing- filename-in-db) 또는 http://stackoverflow.com/questions/698912/asp-net-store-image-in-sql-and-retrieve-for-aspimage 또는 http://stackoverflow.com/questions/5681124/problems-adding-an-image-to-sql-database-asp-net –

+1

죄송합니다 -하지만이 질문은 이미 **이 사이트에서 시간 **의 gazillions을 물어 왔습니다! 제발 ** 검색 ** ** 전에 SQL Server에서 이미지에 대한 또 다른 질문을 게시 ...... –

답변

1
using (SqlConnection Conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString)) 
{ 
try 
{ 
const string SQL = "INSERT INTO [BinaryTable] ([FileName], [DateTimeUploaded], [MIME], [BinaryData]) VALUES (@FileName, @DateTimeUploaded, @MIME, @BinaryData)"; 
SqlCommand cmd = new SqlCommand(SQL, Conn); 
cmd.Parameters.AddWithValue("@FileName", FileName.Text.Trim()); 
cmd.Parameters.AddWithValue("@MIME", FileToUpload.PostedFile.ContentType); 

byte[] imageBytes = new byte[FileToUpload.PostedFile.InputStream.Length + 1]; 
FileToUpload.PostedFile.InputStream.Read(imageBytes, 0, imageBytes.Length); 
cmd.Parameters.AddWithValue("@BinaryData", imageBytes); 
cmd.Parameters.AddWithValue("@DateTimeUploaded", DateTime.Now); 

Conn.Open(); 
cmd.ExecuteNonQuery(); 
lit_Status.Text = "<br />File successfully uploaded - thank you.<br />"; 
Conn.Close(); 
} 
catch 
{ 
Conn.Close(); 
} 
} 

http://www.dotnettutorials.com/tutorials/database/upload-files-sql-database-cs.aspx