aspx中的代码:
<%@ Page Xlanguage="C#" AutoEventWireup="true" CodeFile="PersonalInformation .aspx.cs" Inherits="PersonalInformation_" Debug="true" %>
aspx.cs中的代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Text;
using System.Data.SqlClient;
public partial class PersonalInformation_ : System.Web.UI.Page
{
string sqlserverstr = ConfigurationManager.ConnectionStrings["sqlservercon"].ConnectionString;
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection(sqlserverstr);
conn.Open();
SqlCommand sqlcmd = new SqlCommand();
sqlcmd.Connection = conn;
sqlcmd.CommandText = " insert into Person(Nickname,Sex,Hobby,Email,Birthday,HeadPortrait,BriefIntroduction) values(@Nickname,@Sex,@Hobby,@Email,@Birthday,@HeadPortrait,@BriefIntroduction)";
sqlcmd.Parameters.AddWithValue("@Nickname", Nickname.Text);
if (Sex1.Checked)
{
sqlcmd.Parameters.AddWithValue("@Sex", Sex1.Text);
}
else
{
sqlcmd.Parameters.AddWithValue("@Sex", Sex2.Text);
}
string str = "";
foreach (ListItem li in CheckBoxList1.Items)
{
if (li.Selected)
{
str += li.Text+"、";
}
}
sqlcmd.Parameters.AddWithValue("@Hobby", str);
sqlcmd.Parameters.AddWithValue("@Email", Email.Text);
sqlcmd.Parameters.AddWithValue("@Birthday", Birthday.Text);
StringBuilder sb = new StringBuilder();
string filename = this.file1.PostedFile.FileName;
filename = DateTime.Now.Ticks.ToString() + filename.Substring(filename.LastIndexOf("."));
this.file1.PostedFile.SaveAs(Server.MapPath("upload") + "\\" + filename);
sb.Append("
");
sqlcmd.Parameters.AddWithValue("@HeadPortrait", sb.ToString());
sqlcmd.Parameters.AddWithValue("@BriefIntroduction", BriefIntroduction.Text);
sqlcmd.ExecuteNonQuery();
SqlConnection conn1 = new SqlConnection(sqlserverstr);
conn1.Open();
SqlCommand sqlcmd1 = new SqlCommand();
sqlcmd1.Connection = conn1;
sqlcmd1.CommandText = " insert into Department(department) values(@department)";
sqlcmd1.Parameters.AddWithValue("@department", department.Text);
sqlcmd1.ExecuteNonQuery();
conn.Close();
conn.Dispose();
}
}