Web.config配置文件代码

1、Web.config配置文件代码:
<appSettings><addkey="DataSource"value="server=(local);uid=sa;pwd=;database=Login"></add></appSettings>
 
2、登陆页面(查看用户名是否存在)
          private void btnLogin_Click(object sender, System.EventArgs e)
         { //登陆时首先判断用户名和密码是否正确
              Session["Name"]=TextBox1.Text;
             string uName=this.TextBox1.Text.Trim();
              string uPwd=this.TextBox2.Text.Trim();
              string str=System.Configuration.ConfigurationSettings.AppSettings["DataSource"];
              SqlConnection con=new SqlConnection(str);
              con.Open();
              SqlCommand cmd=new SqlCommand("select count(*) from Login where Username='"+uName+"' and Password='"+uPwd+"'",con);
              int count=Convert.ToInt32(cmd.ExecuteScalar());
              if(count>0)
              {     //Response.Redirect("LoginOK.aspx?Username");
                   Response.Redirect("LoginOK.aspx");
              }
              else
              {     this.Label3.Text="您输入的用户名或密码错误!请重新登陆"; }
         }
 
3、登陆成功后页面(显示欢迎信息)
          private void Page_Load(object sender, System.EventArgs e)
         {
         this.Label1.Text="您好"+Session["Name"]+"欢迎您的到来";
          }
 
4、注册页面
          private void CustomValidator1_ServerValidate(object source, System.Web.UI.WebControls.ServerValidateEventArgs args)
         {    //用自定义控件CustomValidator查看用户名是否存在
              string userName=args.Value;
              string str=System.Configuration.ConfigurationSettings.AppSettings["DataSource"];
              SqlConnection con=new SqlConnection(str);
              con.Open();
              SqlCommand cmd=new SqlCommand("select count(*) from Login where Username='"+userName+"'",con);
              int count=Convert.ToInt32(cmd.ExecuteScalar());
              if(count>0)
              {     args.IsValid=false;
                   this.TextBox1.Text="";
              }
              else
              { args.IsValid=true;    }
              con.Close();
         }
 
          private void Button1_Click(object sender, System.EventArgs e) //注册按钮
         {
              SqlConnection con=new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["DataSource"].ToString());
              string insertcmd="insert into Login(Username,Password,Sex,Birthday,Skill,Show) values(@Username,@Password,@Sex,@Birthday,@Skill,@Show)";
              SqlCommand cmd=new SqlCommand(insertcmd,con);
              cmd.Parameters.Add(new SqlParameter("@Username",SqlDbType.NVarChar,16));
              cmd.Parameters["@Username"].Value=this.TextBox1.Text;
              略---
              cmd.Connection.Open();
              cmd.ExecuteNonQuery();
              cmd.Connection.Close();
              if(Page.IsValid==true)
              {
                   Response.Redirect("Login.aspx");
                   //Page.RegisterStartupScript("Message","<script language=javascript>alert('注册成功,请登陆');</script>");
              }
 
 
客户信息管理
1、共一个WEB页面(实现绑定数据库、添加用户、查询、删除):
绑定数据库(2个表):
          private void Page_Load(object sender, System.EventArgs e)
         {     if(!this.IsPostBack)
              {     this.FillDG(); }
         }
          private void FillDG()   //绑定DataGrid控件方法
         {     con=DB.createCon();
              ds=new DataSet();
              sda=new SqlDataAdapter("select CustomerID,CustomerName,Address,WebsiteName,Website,CustomerTypeName from CustomerList AS a JOIN DIM_CustomerType AS b ON a.CustomerTypeID=b.CustomerTypeID",con);
              sda.Fill(ds,"CustomerList");
              this.DataGrid1.DataKeyField="CustomerID"; //设置主键,删除和更新数据时用
              this.DataGrid1.DataSource=ds.Tables["CustomerList"];
              this.DataGrid1.DataBind();
         }
 
添加用户(多表):
          private void btnAdd_Click(object sender, System.EventArgs e)  
         {     string userName=this.txtName.Text;
              if(!DB.judge(userName)) //用户名如果存在,则不能添加
              {     con=DB.createCon();
                   int CustomerTypeID=0;
                   if(this.DropDownList1.SelectedValue.ToString()=="高级会员")   //方法1
                   {     CustomerTypeID=1;   }
                   if(this.DropDownList1.SelectedValue.ToString()=="普通用户")
                   {     CustomerTypeID=2;   }
                   string insertCmd="insert into CustomerList(CustomerName,Address,WebsiteName,Website,CustomerTypeID) VALUES (@CustomerName,@Address,@WebsiteName,@Website,@CustomerTypeID)";
                   cmd=new SqlCommand(insertCmd,con);
                   cmd.Parameters.Add(new SqlParameter("@CustomerName",SqlDbType.VarChar,50));
                   cmd.Parameters["@CustomerName"].Value=this.txtName.Text;
                   略--与上代码相同  
                   cmd.Parameters.Add(new SqlParameter("@CustomerTypeID",SqlDbType.Int,4));
                   cmd.Parameters["@CustomerTypeID"].Value=CustomerTypeID;
                   cmd.Connection.Open();
                   cmd.ExecuteNonQuery();
                   cmd.Connection.Close();
                   this.FillDG();
              }
              else     {Response.Write("用户名已存在");    }
         }
 
查询:
          private void btnQuery_Click(object sender, System.EventArgs e)  
         {    string c="";        //定义搜索字段
              if(this.txtName.Text=="")   //如果此文本框为空,则显示所有数据,可省略此段
              {     c="CustomerName like '%'";     }
              else
              {     c+="CustomerName like '%"+this.txtName.Text+"%'";   }
              if(this.txtAddress.Text!="")
              {     c+=" and Address like '%"+this.txtAddress.Text+"%'";     }
              if(this.DropDownList1.SelectedValue.ToString()!="") //DropDownList的查询字段
              {     int CustomerTypeID=0;
                   string CustomerTypeName=this.DropDownList1.SelectedValue.ToString();                   
                   if(CustomerTypeName=="高级会员")
                   {     CustomerTypeID=1;            
                       c+=" and CustomerTypeName='高级会员'";
                   }                     
                   略--
              }略--
             
              con=DB.createCon();
              sda=new SqlDataAdapter();
              sda.SelectCommand=new SqlCommand("select CustomerID,CustomerName,Address,WebsiteName,Website,CustomerTypeName from CustomerList AS a JOIN DIM_CustomerType AS b ON a.CustomerTypeID=b.CustomerTypeID",con);
              ds=new DataSet();
              sda.Fill(ds,"CustomerList");
              DataView dv=new DataView(ds.Tables["CustomerList"]);
              dv.RowFilter=c;
              this.DataGrid1.DataSource=dv;
              this.DataGrid1.DataBind();
         }
 
删除:
          private void DataGrid1_DeleteCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
         {     string CustomerID=this.DataGrid1.DataKeys[e.Item.ItemIndex].ToString();
              con=DB.createCon();
            cmd=new SqlCommand("delete from CustomerList where CustomerID='"+CustomerID+"'",con);
              con.Open();
              cmd.ExecuteNonQuery();
              this.FillDG();
         }
附:删除确认:(DataGrid1_ItemDataBound事件里写)
              if(e.Item.ItemType==ListItemType.Item||e.Item.ItemType==ListItemType.AlternatingItem)
              {((LinkButton)(e.Item.Cells[6].Controls[0])).Attributes.Add("onclick","return confirm('确认删除吗?')");}
 
2、DB.cs类文件:
         public static SqlConnection createCon()
         {     return new SqlConnection("server=.;uid=sa;pwd=;database=CRMSmart");}
         public static bool judge(string userName) //查看用户名是否存在的方法
         {     SqlConnection con=DB.createCon();
              con.Open();
              SqlCommand cmd=new SqlCommand("select count(*) from CustomerList where CustomerName='"+userName+"'",con);
              int count=Convert.ToInt32(cmd.ExecuteScalar());
              if(count>0)
              {    return true;     }
              else
              {     return false;   }
         }
 
 
通讯录管理 (用参数实现更新)
先在Page_Load里定义主键:this.DataGrid1.DataKeyField="ID";   //主键
更新:
    private void DataGrid1_UpdateCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
         {   //更新,用参数的方式做
      string pID=this.DataGrid1.DataKeys[e.Item.ItemIndex].ToString();
                   con=new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["strcon"].ToString());
                   //String updateCmd="update AddressTable set ID=@pID,Name=@pName,Address=@pAddress,Sex=@pSex,Age=@pAge where ID=@pID";
                   String updateCmd="update AddressTable set Name=@pName,Address=@pAddress,Sex=@pSex,Age=@pAge where ID='"+pID+"'";
                   cmd=new SqlCommand(updateCmd,con);
                   //cmd.Parameters.Add(new SqlParameter("@pID",SqlDbType.Int,4));
                   cmd.Parameters.Add(new SqlParameter("@pName",SqlDbType.VarChar,50));    
                   略--
                   //cmd.Parameters["@pID"].Value=this.DataGrid1.DataKeys[(int)e.Item.ItemIndex]; //取主键
                   cmd.Parameters["@pName"].Value=((TextBox)e.Item.Cells[3].Controls[0]).Text;
                   cmd.Parameters["@pAddress"].Value=((TextBox)e.Item.Cells[4].Controls[0]).Text;
                   略--
                   cmd.Connection.Open();
                   cmd.ExecuteNonQuery();
                   this.DataGrid1.EditItemIndex=-1;
                   cmd.Connection.Close();
                   this.BindToDataGrid();
                   lblMessage.Text="已更新记录";
         }
 
 
Web系统用户登录模块   (注销、未登陆用户不能浏览其他网页)
1、登陆页面:
首先导入命名空间using System.Web.Security;using System.Data.SqlClient;
          private void Button1_Click(object sender, System.EventArgs e)   //登陆按钮
         { //登陆时首先判断用户名和密码是否正确,略--如果存在,则—登陆后还返回到本页面          
              {    FormsAuthentication.RedirectFromLoginPage(uName,false);
                   Response.Redirect("loginOK.aspx");
              }
         }
 
2、注销登陆:
          private void Button1_Click(object sender, System.EventArgs e)   //注销登陆
          {    FormsAuthentication.SignOut();
              Context.User=null;  
              Response.Redirect("login.aspx",false);
     }
 
3、Web.config配置文件代码(未登陆用户只可以进入注册页面):
//在<configuration>下添加
<locationpath="AddUser.aspx">    <!-- path可以是文件夹,也可以是文件-->
<system.web>
<authorization>
<allowusers="*"></allow>    <!--使这个文件夹下的文件都可以浏览 -->
</authorization>
</system.web>
</location>
 
    <authenticationmode="Forms">
    <formsname="aaa"loginUrl="login.aspx"></forms>   <!-- 如果没有登陆,则自动转向登陆页面-->
    </authentication>
 
    <authorization>
    <denyusers="?"/>          <!-- *是指所有用户, ?是指未登录的用户-->
    </authorization>
 
 
鲜花管理系统 
1、选择鲜花页面:点击RadioButtonList,进入相应选项
          private void Button1_Click(object sender, System.EventArgs e)
          {        if(this.RadioButtonList1.SelectedIndex==0)
                   {     Response.Redirect("Item1.aspx");    }略--                  
         }
 
2、显示相应类别信息页面Item1.aspx:
          private void Page_Load(object sender, System.EventArgs e)
         {         SqlConnection con=new SqlConnection("server=.;uid=sa;pwd=;database=FlowerDB");
                   string sdaStr="select FlowerName,FlowerPrice,FloweType.FloweName from Flowers INNER JOIN FloweType ON Flowers.FloweTypeID=FloweType.FloweTypeID AND Flowers.FloweTypeID=1";
                   SqlDataAdapter sda=new SqlDataAdapter(sdaStr,con);
                   DataSet ds=new DataSet();
                   sda.Fill(ds,"Flowers");
                   this.DataGrid1.DataSource=ds.Tables["Flowers"];
                   this.DataGrid1.DataBind();
         }
 
 
CheckBoxList、DropDownList1控件绑定数据库
1、CheckBoxList控件绑定数据库(选择省,则显示相应的市)
          private void Page_Load(object sender, System.EventArgs e)
         {     if(!IsPostBack)
              {     SqlConnection con=DB.createConn();
                   con.Open();
                   SqlCommand cmd=new SqlCommand("select * from Login",con);
                   SqlDataReader dr=cmd.ExecuteReader();
                   this.CheckBoxList1.DataTextField="userPwd";   //显示文件字段名
                   this.CheckBoxList1.DataValueField="userName"; //主键名
                   this.CheckBoxList1.DataSource=dr;
                   this.CheckBoxList1.DataBind();
                   dr.Close();
                   con.Close();
              }
         }
 
    private void Button2_Click(object sender, System.EventArgs e)
          {     for(int i=0;i<this.CheckBoxList1.Items.Count;i++)
               {     if(this.CheckBoxList1.Items[i].Selected)
                    {
                        Response.Write(this.CheckBoxList1.Items[i].Value.ToString()+"-"+this.CheckBoxList1.Items[i].Text+"<br>");
                    }
               }
         }
 
 
2、DropDownList1控件绑定数据库:在控件里选择省,在另一个控件里显示相应的市名
     private void Page_Load(object sender, System.EventArgs e)
         {     if(!IsPostBack)
              {     SqlConnection con=DB.createConn();
                   con.Open();
                   SqlCommand cmd=new SqlCommand("select * from province",con); //取出所有的省
                   SqlDataReader dr=cmd.ExecuteReader();
                   this.DropDownList1.DataSource=dr;
                   this.DropDownList1.DataTextField="proName";
                   this.DropDownList1.DataValueField="proID";
                   this.DropDownList1.DataBind();
                   dr.Close();
                   // 绑定市
                   SqlCommand cmdCity=new SqlCommand("select * from city where proID="+this.DropDownList1.SelectedValue,con);
                   dr=cmdCity.ExecuteReader();
                   this.DropDownList2.DataSource=dr;
                   this.DropDownList2.DataTextField="cityName";
                   this.DropDownList2.DataValueField="cityID";
                   this.DropDownList2.DataBind();
                   dr.Close();
                   con.Close();
              }
         }
 
   // 设DropDownList1控件的AutoPostBack=true,然后在其SelectedIndexChanged事件中输入以下代码:
          private void DropDownList1_SelectedIndexChanged(object sender, System.EventArgs e)
          {    string proID=this.DropDownList1.SelectedValue; //直接获取主键
              SqlConnection con=DB.createConn();
              con.Open();
              SqlCommand cmd=new SqlCommand("select * from city where proID="+proID,con);
              SqlDataReader dr=cmd.ExecuteReader();
              this.DropDownList2.DataSource=dr;
              this.DropDownList2.DataTextField="cityName";
              this.DropDownList2.DataValueField="cityID";
              this.DropDownList2.DataBind();
              dr.Close();
              con.Close();
         }
 
 
file Field控件上传文件
1、 private void Button1_Click(object sender, System.EventArgs e)
         {
             string fullFileName=this.File1.PostedFile.FileName; //获取文件的全路径
              string fileName=fullFileName.Substring(fullFileName.LastIndexOf("\\")+1); //截取最后一个文件名
              string type=fullFileName.Substring(fullFileName.LastIndexOf(".")+1);
              if(type=="jpg"||type=="bmp"||type=="gif")
              {
                   this.File1.PostedFile.SaveAs(Server.MapPath("up")+"\\"+fileName);
                   this.Image1.ImageUrl="up/"+fileName;
              }
              else
              {
                   Response.Write("<script language='javascript'>alert('你选择的图片格式不正确!');</script>");
              }
 
         }
-------------
2、          this.File1.PostedFile.SaveAs(Server.MapPath("upFile")+"\\1.jpg");
              this.Image1.ImageUrl=Server.MapPath("upFile")+"\\1.jpg";
 
 
显示在线人数
Global.asax文件代码:
          protected void Application_Start(Object sender, EventArgs e)
         {     Application.Add("count",0);    //初始化count的值为零   }
          protected void Session_Start(Object sender, EventArgs e)
         {     Application.Lock();
              Application["count"]=(int)Application["count"]+1;
              Application.UnLock();
         }
在Page_Load 里调用:     Response.Write(Application["count"].ToString());
 
显示在线人数
Global.asax文件代码:
          protected void Application_Start(Object sender, EventArgs e)
         {     Application.Add("count",0);    //初始化count的值为零   }
          protected void Session_Start(Object sender, EventArgs e)
         {     Application.Lock();
              Application["count"]=(int)Application["count"]+1;
              Application.UnLock();
         }
在Page_Load 里调用:     Response.Write(Application["count"].ToString());


本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/daban_/archive/2007/05/29/1629549.aspx

转载于:https://www.cnblogs.com/gotianya/archive/2010/04/19/1715647.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值