using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//CheckBox1.Checked = true;
using (SqlConnection con = new SqlConnection("server=.; Integrated Security=true; Initial Catalog=stu")) //
{
using (SqlCommand cmd = new SqlCommand("select * from h0 ", con))
{
//以下为dropdownlist1绑定部门
con.Open();
SqlDataReader myreader;
myreader = cmd.ExecuteReader();
DropDownList1.DataSource = myreader;
DropDownList1.DataTextField = "部门";
DropDownList1.DataValueField = "id";
DropDownList1.DataBind();
con.Close();
}
}
}
}
//用参数传递的方法进行数据库操作()插入
protected void Button1_Click(object sender, EventArgs e)
{
using (SqlConnection con = new SqlConnection("server=.; Integrated Security=true; Initial Catalog=stu"))
{
using (SqlCommand cmd = new SqlCommand("insert h1 values(@姓名,@性别,@所在部门,@爱好,@E,@生日,@个人头像,@个人简介)", con))
{
SqlParameter name = new SqlParameter("@姓名", TextBox1.Text);
cmd.Parameters.Add(name);
string sex1="";
if (RadioButton1.Checked)
{
sex1=RadioButton1.Text;
}
else
{
sex1=RadioButton2.Text;
}
SqlParameter sex = new SqlParameter("@性别",sex1);
cmd.Parameters.Add(sex);
SqlParameter department=new SqlParameter("@所在部门",DropDownList1.SelectedItem.Text);
cmd.Parameters.Add(department);
Response.Write(department.Value);
string aih="";
//将爱好在后中进行编码
//读取的时候用此代码翻译过来进行先中的每一项
//这样做的好处是用一个字段来来存储多个CheckedBox控件的状态
if(CheckBox1.Checked)
{
aih+="1,"; //第一个爱好
}
if(CheckBox2.Checked)
{
aih+="2,";
}
if(CheckBox3.Checked)
{
aih+="3,";
}
if(CheckBox4.Checked)
{
aih+="4";
}
SqlParameter hobby = new SqlParameter("@爱好", aih);
cmd.Parameters.Add(hobby);
SqlParameter email = new SqlParameter("@E",TextBox2.Text);
cmd.Parameters.Add(email);
SqlParameter brigthday = new SqlParameter("@生日",TextBox3.Text);
cmd.Parameters.Add(brigthday);
string filename = FileUpload1.PostedFile.FileName;
filename = System.DateTime.Now.Ticks.ToString() + filename.Substring(filename.LastIndexOf('.'));
FileUpload1.SaveAs(Server.MapPath("image")+"//" + filename);
SqlParameter pho = new SqlParameter("@个人头像", filename);
cmd.Parameters.Add(pho);
SqlParameter jie = new SqlParameter("@个人简介",TextBox4.Text);
cmd.Parameters.Add(jie);
// Response.Write(department.Value);
con.Open();
if (
cmd.ExecuteNonQuery() != 0) //执行操作判断是否操作成功
{
Response.Write("successful");
Image1.ImageUrl = @"image/" + filename; //操作成功的同时也进行显示图片
}
con.Close();
}
}
}
//单击这个按钮的同时也要先进丢扫尾操作,就是把前台一些需要进行初始化的控件进行初始化
protected void Button2_Click(object sender, EventArgs e)
{
CheckBox1.Checked = false;
CheckBox2.Checked = false;
CheckBox3.Checked = false;
CheckBox4.Checked = false;
using(SqlConnection con=new SqlConnection("server=.; Integrated Security=true; Initial Catalog=stu"))
{
using(SqlCommand cmd=new SqlCommand("select * from h1",con))
{
SqlDataReader myreader;
con.Open();
myreader = cmd.ExecuteReader();
//姓名的添加
while (myreader.Read())
{
if (myreader["姓名"].ToString().Trim() == TextBox1.Text.Trim())
{
TextBox1.Text = myreader["姓名"].ToString();
//性别的添加
string sex = myreader["性别"].ToString();
if (sex == "男")
RadioButton1.Checked = true;
else
RadioButton2.Checked = true;
//所有部门
DropDownList1.SelectedItem.Text = myreader["所在部门"].ToString();
//添加爱好
string hobby = myreader["爱好"].ToString();
string[] a = hobby.Split(new char[] { ',' });
//这就是用上面那个编码进行还原信息来进行每一个用来选不同的CheckeBox
foreach (var item in a)
{
if (item == "1")
CheckBox1.Checked = true;
if (item == "2")
CheckBox2.Checked = true;
if (item == "3")
CheckBox3.Checked = true;
if (item == "4")
CheckBox4.Checked = true;
}
//add e-mail
TextBox2.Text = myreader["E-mail"].ToString();
//add 生日
TextBox3.Text = myreader["生日"].ToString();
//add person memo
TextBox4.Text = myreader["个人简介"].ToString();
//disp image
string filename = myreader["个人头像"].ToString();
Image1.ImageUrl = @"image/" + filename;
}
}
}
}
}
}