Asp. NET adds a picture to a SQL Server database

Source: Internet
Author: User
Tags file size
asp.net|server| Data | database <%@ Page language= "C #" codebehind= "Register.aspx.cs" autoeventwireup= "false" inherits= "shop . Register "%>
<! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 transitional//en" >
<HTML>
<HEAD>
<title> Registration </title>
</HEAD>
<body style= "font-size:12px" >
<form id= "Form1" method= "POST" runat= "Server" >
<font face= "Song Body" >
<table id= "Table1" cellspacing= "0" cellpadding= "0" width= "50%" align= "center" bgcolor= "Beige"
border= "0" >
<TR>
&LT;TD bgcolor= "#cccc66" colspan= "2" height= "rowspan=" "> Add new User </TD>
</TR>
<TR>
<TD> name </TD>
<TD>
<asp:textbox id= "Txtpersonname" runat= "Server" ></asp:TextBox></TD>
</TR>
<TR>
<TD> Email </TD>
<TD>
<asp:textbox id= "Txtpersonemail" runat= "Server" ></asp:TextBox></TD>
</TR>
<TR>
<TD> Sex </TD>
<TD>
<asp:radiobutton groupname= "Sex" text= "man" id= "Sexmale" runat= "Server"/>
<asp:radiobutton groupname= "Sex" text= "female" id= "Sexfemale" runat= "Server"/>
</TD>
</TR>
<TR>
<TD> Birth date </TD>
<TD>
<asp:textbox id= "Txtpersondob" runat= "Server" ></asp:TextBox></TD>
</TR>
<TR>
<TD> Photo </TD>
<td><input type= "File" id= "Personimage" name= "Personimage" runat= "Server" ></TD>
</TR>
<TR>
&LT;TD align= "center" colspan= "2" >
<asp:button id= "Button1" runat= "Server" text= "add" ></asp:Button>
</TD>
</TR>
</TABLE>
</FONT>
</form>
</body>
</HTML>
Aspx.cs:
Using System;
Using System.IO;
Using System.Collections;
Using System.ComponentModel;
Using System.Data;
Using System.Data.SqlClient;
Using System.Drawing;
Using System.Web;
Using System.Web.SessionState;
Using System.Web.UI;
Using System.Web.UI.WebControls;
Using System.Web.UI.HtmlControls;
Using System.Configuration;
Namespace Shop
{
<summary>
A summary description of the Register.
</summary>
public class Register:System.Web.UI.Page
{
protected System.Web.UI.WebControls.TextBox txtpersonname;
protected System.Web.UI.WebControls.TextBox Txtpersonemail;
protected System.Web.UI.WebControls.RadioButton Sexmale;
protected System.Web.UI.WebControls.RadioButton Sexfemale;
protected System.Web.UI.WebControls.TextBox Txtpersondob;
protected System.Web.UI.HtmlControls.HtmlInputFile personimage;
protected System.Web.UI.WebControls.Button Button1;

private void Page_Load (object sender, System.EventArgs e)
{
Place user code here to initialize page
}
public void Addperson (object sender, System.EventArgs e)
{
int intimagesize;
String Strimagetype;
Stream ImageStream;
Intimagesize = PersonImage.PostedFile.ContentLength; File size
Strimagetype = PersonImage.PostedFile.ContentType; File type
ImageStream = PersonImage.PostedFile.InputStream;
byte[] imagecontent = new Byte[intimagesize];
int intstatus = Imagestream.read (imagecontent, 0, intimagesize);

Writing to the database
String strconn = configurationsettings.appsettings["ConnectionString"];
SqlConnection myconnection = new SqlConnection (strconn);
SqlCommand mycommand = new SqlCommand ("Sp_person_isp", MyConnection);
myCommand.CommandType = CommandType.StoredProcedure;

MYCOMMAND.PARAMETERS.ADD ("@PersonEmail", SqlDbType.VarChar, 255). Value = Txtpersonemail.text;
MYCOMMAND.PARAMETERS.ADD ("@PersonName", SqlDbType.VarChar, 255). Value = Txtpersonname.text;
MYCOMMAND.PARAMETERS.ADD ("@PersonSex", SqlDbType.Char, 1);
if (sexmale.checked)
mycommand.parameters["@PersonSex"]. Value = "M";
Else
mycommand.parameters["@PersonSex"]. Value = "F";
MYCOMMAND.PARAMETERS.ADD ("@PersonDOB", Sqldbtype.datetime). Value = Txtpersondob.text;
MYCOMMAND.PARAMETERS.ADD ("@PersonImage", Sqldbtype.image). Value = imagecontent;
MYCOMMAND.PARAMETERS.ADD ("@PersonImageType", SqlDbType.VarChar, 255). Value = Strimagetype;

Try
{
Myconnection.open ();
Mycommand.executenonquery ();
Myconnection.close ();
Response.Write ("Add success!") ");
}
catch (System.Exception sqlexe)
{
Response.Write ("Add failed!") Cause: "+sqlexe.tostring ());
}
}

Code generated #region the Web forms Designer
Override protected void OnInit (EventArgs e)
{
//
CodeGen: This call is required for the ASP.net Web forms Designer.
//
InitializeComponent ();
Base. OnInit (e);
}

<summary>
Designer supports required methods-do not use the Code editor to modify
The contents of this method.
</summary>
private void InitializeComponent ()
{
This. Load + = new System.EventHandler (this. Page_Load);
}
#endregion
}
}




Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.