CREATE TABLE tablename(
[ID] [int] IDENTITY(1,1) NOT NULL,
[FileName] [varchar](50) NOT NULL,
[AttatchFile] [varchar](max) NOT NULL,
CONSTRAINT pk_tablename PRIMARY KEY CLUSTERED
(
[ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
2-Aspx Page
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default3.aspx.cs" Inherits="Default3" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
<div>
File:
<asp:FileUpload ID="FileUpload1" runat="server" /><br />
<asp:Button ID="SaveBtn" runat="server" Height="40px" OnClick="Button1_Click" Text="Save"
Width="90px" /></div>
</form>
</body>
</html>
3-server side
protected void Button1_Click(object sender , EventArgs e)
{
SqlConnection con=new SqlConnection("Data Source=YourServer-pc;Initial Catalog=YourDataBase;Integrated Security=True");
SqlCommand cmd=new SqlCommand("insert into attatchments(filename,attatchfile)values(@filename,@attatchfile)" , con);
string fileName=FileUpload1.PostedFile.FileName.ToString();
FileInfo info=new FileInfo(FileUpload1.PostedFile.FileName.ToString());
string file;
StreamReader br=new StreamReader(FileUpload1.PostedFile.InputStream);
file=br.ReadToEnd();
cmd.Parameters.AddWithValue("@filename" , fileName);
cmd.Parameters.AddWithValue("@attatchfile" , file);
con.Open();
if (FileUpload1.HasFile)
{
cmd.ExecuteNonQuery();
}
cmd.Dispose();
con.Close();
}
Himanshu Manjarawala
Sr. Software Engineer@AutomationAnywhere
http://fieredotnet.wordpress.com/
Sanoop, if this helps please login to Mark As Answer. | Alert Moderator