![]() |
|
using System.Data.SqlClient; using System.Data.SqlTypes;// 添加命名空间 public string name1 { get { return TextBox9.Text.Trim(); } } //定义TextBox9中的内容为公用变量,设置文本框的Visible属性为False private void Page_Load(object sender, System.EventArgs e) { Page.RegisterStartupScript("focus", "<script language= javascript> document.getElementById('TextBox1').focus();</script>");//插入java脚本(聚焦TextBox1) if(!IsPostBack)// 检查是否是第一次加载本页 { SqlConnection myConnection=new SqlConnection("server=JXSERVER;uid=sa;pwd=1818;database=jx"); myConnection.Open();//连接数据库 SqlDataAdapter myAdapter=new SqlDataAdapter("select * from 车型",myConnection);// 建立myAdapter对象 DataSet myDataSet=new DataSet(); //建立一个myDataSet对象 myAdapter.Fill(myDataSet,"车型");//把执行select语句得到的记录添加到myDataSet中 DropDownList1.DataSource=myDataSet.Tables["车型"].DefaultView; //指定DropDownList1数据源 DropDownList1.DataTextField="车型"; //指定DropDownList1的文本值为“车型”表中‘车型’字段 DropDownList1.DataValueField="车型ID"; //指定DropDownList1的Value值为“车型”表中‘车型ID’字段 DropDownList1.DataBind();//绑定数据 DropDownList1.SelectedValue="C1";//指定DropDownList1的默认Value值为C1 myConnection.Close();//关闭数据库连接 } }// 在此处放置用户代码以初始化页面 /*由于在实现删改功能的同时可以实现查询功能因此仅以删改功能为例*/ private void Button2_Click(object sender, System.EventArgs e) { TextBox9.Text = "where 姓名 like '" + TextBox1.Text.Trim() + "'"; //定义姓名模糊查询条件语句 Server.Transfer("xy_search_win.aspx"); //传送姓名查询条件语句到第二个页面 } private void Button4_Click(object sender, System.EventArgs e) { TextBox9.Text = "where 证件号码= '" + TextBox2.Text.Trim() + "'"; //证件号码= TextBox2.Text的值 Server.Transfer("xy_search_win.aspx"); } private void Button6_Click(object sender, System.EventArgs e) { extBox9.Text = "where 学号= '" + TextBox3.Text.Trim() + "'"; //学号= TextBox3.Text的值 Server.Transfer("xy_search_win.aspx"); } private void Button8_Click(object sender, System.EventArgs e) { TextBox9.Text = "where 学号 like '"+TextBox4.Text.Trim()+"%'"; //学号与TextBox4.Text的值模糊匹配 Server.Transfer("xy_search_win.aspx"); } private void Button10_Click(object sender, System.EventArgs e) { TextBox9.Text = "where 报名日期 between '"+ TextBox5.Text.Trim() + "' and '" + TextBox6.Text.Trim() + "'"; //TextBox5.Text>=报名日期<=TextBox6.Text Server.Transfer("xy_search_win.aspx"); } private void Button12_Click(object sender, System.EventArgs e) { TextBox9.Text = "where 报考车种 = '"+DropDownList1.SelectedValue.ToString().Trim()+"'"; //报考车种=DropDownList1选中项的value值 Server.Transfer("xy_search_win.aspx"); } private void Button14_Click(object sender, System.EventArgs e) { TextBox9.Text = "where 欠款金额 between "+Convert.ToDecimal(TextBox7.Text.Trim())+" and "+Convert.ToDecimal(TextBox8.Text.Trim())+" "; //注意要将字符串类型转换成货币类型decimal Server.Transfer("xy_search_win.aspx"); } |
| using System.Data.SqlClient;//添加命名空间 public class xy_search_win : System.Web.UI.Page { public xy_search sourcepage; //定义第一个页面为当前页面的源页面 public string name1 { get { return DataGrid1.Items[DataGrid1.SelectedIndex].Cells[2].Text.ToString(); } }//定义所选行的‘学号’为公共变量 private void search1()//定义一个查询子程序 { SqlConnection myConnection= new SqlConnection ("server=JXSERVER;uid=sa;pwd=1818;database=jx"); myConnection.Open();//连接数据库jx SqlDataAdapter adapter1= new SqlDataAdapter ("select 学号,姓名,报考车种,联系方式,证件号码,培训方式,欠款金额,备注 from V学员资料 "+ TextBox1.Text + " order by 学号", myConnection); //查询视图 DataSet myDataSet=new DataSet(); Adapter1.Fill(myDataSet,"result1"); DataGrid1.DataSource=myDataSet.Tables["result1"]; DataGrid1.DataKeyField="学号";//指定关键字段为学号 DataGrid1.DataBind();//绑定数据 myConnection.Close();
} private void Page_Load(object sender, System.EventArgs e) { if(!IsPostBack) { sourcepage=(xy_search)Context.Handler; // 使用Context.Handler属性来获得前一个页面实例对象的引用 TextBox1.Text=sourcepage.name1;//将第一个页面传过来的查询语句赋给TextBox1 search1();//调用查询子程序显示查询结果 } } private void DataGrid1_DeleteCommand(object source,System.Web.UI.WebControls.DataGridCommandEventArgs e)//”删除”按钮操作代码 { SqlConnection myConnection =new SqlConnection("server=JXSERVER;uid=sa;pwd=1818;database=jx"); myConnection.Open(); string mydelete="delete from 学员资料 where 学号='"+DataGrid1.DataKeys[(int)e.Item.ItemIndex]+"'";//删除“学员资料”表中‘学号’等于所选行对应的学号 SqlCommand myCommand=new SqlCommand(mydelete, myConnection);//创建myCommand对象 myCommand.ExecuteNonQuery();//执行sql语句 myConnection.Close(); search1();//重新调用子程序刷新页面; } private void DataGrid1_SelectedIndexChanged(object sender, System.EventArgs e) //”编辑”按钮命令 { Server.Transfer("xy_search_detail.aspx"); } //转到第三个页面进行详细编辑 } |
| using System.Data.SqlClient; public class xy_search_detail : System.Web.UI.Page { public xy_search_win sourcepage;//设置第二个页面为当前页面的源页面 private void myselect1()//定义查询子程序 { SqlConnection myConnection =new SqlConnection("server=JXSERVER;uid=sa;pwd=1818;database=jx"); myConnection.Open(); string myselect1="select 学号,姓名,性别,报考车种,证件号码,手机,小灵通,住宅电话,办公电话,备注 from 学员资料 where 学号='"+TextBox10.Text+"'";//查询视图,TextBox10.Text为第二个页面传递过来的学号 SqlCommand myCommand1=new SqlCommand(myselect1, myConnection); SqlDataReader myReader1 =myCommand1.ExecuteReader();//执行读操作 while(myReader1.Read())//该条记录不为空时执行{…}里的程序 { TextBox1.Text=myReader1.GetString(0);//学号赋值给TextBox1 TextBox2.Text=myReader1.GetString(1);//姓名 TextBox3.Text=myReader1.GetString(2);//性别 TextBox4.Text=myReader1.GetString(3);//报考车种 TextBox5.Text=myReader1.GetString(4);//证件号码 if(myReader1.IsDBNull(5)==false) { TextBox9.Text=myReader1.GetString(5); //手机 } //判断该记录的手机这一列数据是否为空 else {TextBox9.Text=""; } if(myReader1.IsDBNull(6)==false) {TextBox11.Text=myReader1.GetString(6);//小灵通 } else {TextBox11.Text="";} if(myReader1.IsDBNull(7)==false) {TextBox12.Text=myReader1.GetString(7);//住宅电话} else {TextBox12.Text="";} if(myReader1.IsDBNull(8)==false) {TextBox13.Text=myReader1.GetString(8);//办公电话 } else {TextBox13.Text="";} if(myReader1.IsDBNull(9)==false) {TextBox7.Text=myReader1.GetString(9);}//备注 else {TextBox7.Text=""; } } myReader1.Close(); myConnection.Close(); } private void Page_Load(object sender, System.EventArgs e) { if(!IsPostBack) { sourcepage=(xy_search_win)Context.Handler; TextBox10.Text=sourcepage.name1;//将第二个页面的‘学号’值传到第三个页面 Label3.Text="";//提示标签置空 myselect1();//调用查询资料子程序 } } private void Button1_Click(object sender, System.EventArgs e) //“修改“按钮命令 { SqlConnection myConnection=new SqlConnection("server=JXSERVER;uid=sa;pwd=1818;database=jx"); myConnection.Open(); string lxfs=TextBox9.Text.Trim()+"+"+TextBox11.Text.Trim()+"+"+_ TextBox12.Text.Trim()+"+"+TextBox13.Text.Trim()+"+"; //定义联系方式 string myupdate= "update 学员资料 set _ 手机='"+TextBox9.Text.Trim()+"',小灵通='"+TextBox11.Text.Trim()+"',_ 住宅电话='"+TextBox12.Text.Trim()+"',办公电话='"+TextBox13.Text.Trim()+"',_ 备注='"+TextBox7.Text+"',联系方式='"+lxfs+"' where 学号="+TextBox10.Text.Trim()+""; SqlCommand myCommand2 = new SqlCommand(myupdate,myConnection); MyCommand2.ExecuteNonQuery();// Label3.Text="修改成功,请继续!"; myConnection.Close(); } private void Button2_Click(object sender, System.EventArgs e)//“重置”按钮命令 { Label3.Text="";//提示标签置空 myselect1();//调用查询资料子程序 } } |