Insert images on a database
<%@ Page Language="C#" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<%@ Import Namespace="System.Data" %>
<HTML>
<HEAD>
<script language="vb" runat="server">
private void Button1_Click(object sender, System.EventArgs e)
{
int intImageSize;
int strImageType;
Stream ImageStream;
intImageSize = myFile.PostedFile.ContentLength;
strImageType = myFile.PostedFile.ContentType;
ImageStream = myFile.PostedFile.InputStream;
Byte ImageContent = (Byte)intImageSize;
int intStatus;
intStatus = ImageStream.Read(ImageContent, 0, intImageSize);
SqlConnection myConnection = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"]);
SqlCommand myCommand = new SqlCommand("sp_insertimage", myConnection);
myCommand.CommandType = CommandType.StoredProcedure;
SqlParameter prmmyFile = new SqlParameter("@myFile", SqlDbType.Image);
prmmyFile.Value = ImageContent;
myCommand.Parameters.Add(prmmyFile);
SqlParameter prmmyFileType = new SqlParameter("@myFileType", SqlDbType.VarChar, 255);
prmmyFileType.Value = strImageType;
myCommand.Parameters.Add(prmmyFileType);
try
{
myConnection.Open();
myCommand.ExecuteNonQuery();
myConnection.Close();
Response.Write("Salvata!");
}
catch (SqlException SQLexc)
{
Response.Write("Errore: " + SQLexc.ToString());
}
}
</script>
</HEAD>
<body>
<P><INPUT type="file" id="myFile" name="myFile" runat="server"></P>
<P>
<asp:Button id="Button1" runat="server" Text="Salva" OnClick="Button1_Click"></asp:Button></P>
</body>
</HTML>
Download Code...
Print Page