开发者

How to upload image from C# application to SQL Server 2005 [duplicate]

开发者 https://www.devze.com 2023-04-01 06:07 出处:网络
This question already has answers here: Closed 11 years ago. Possible Duplicate: Up开发者_StackOverflow中文版load image to server using C#/.NET and storing filename in DB
This question already has answers here: Closed 11 years ago.

Possible Duplicate:

Up开发者_StackOverflow中文版load image to server using C#/.NET and storing filename in DB

How to upload image from a C# application to SQL Server 2005


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

0

精彩评论

暂无评论...
验证码 换一张
取 消