ASP.Net学习之常用模块整理(1)

每做完一个项目,就可以有很多的经验跟技巧学到,感谢公司能给我这么一个机会做这么一个大项目,让我从一个从刚学会一点ASP.Net的菜鸟过渡到可以用c#正常手写相关常用模块的一个真正的程序员,一个人做项目可以学得很多很多的东西,当然其中碰到困难也只能靠自己一个人去解决.为了让大家更好的学习,我把常用的模块写成一个个方法,希望能对大家有用.每个人都有不同的算法跟代码习惯,而且个人水平有限,欢迎大家多多指正.

前提准备:
由于项目是采用SQL数据库,所以我们先在web.config中设置好数据库连接

<appSettings>
        <add key="Conn" value="Server=(local);Database=dezai;User ID=sa;"></add>    
  </appSettings> 

  之后在CS代码中要注意引用

  c#
  using System.Data.Sqlclient;
  using System.Data;
  using System.Configuration;

  vb.net

  Imports System.Data.Sqlclient
  Imports System.Data
  Imports System.Configuration


以下就是常用的模块


1.会员登陆模块


用户控件: 
TextBox:TxtUser 用户名  TxtPwd 密码
Label:LblError 错误提示

存储过程:user_login

<script type="text/javascript">window.attachEvent("onload",function (){AutoSizeDIV('CODE_7643')})</script>
程序代码:[ 复制代码到剪贴板 ]
CREATE procedure user_login
@user_name varchar(50),
@user_password varchar(50)
as
select * from userwhere [User_Name] = @User_Name and [User_Pwd] = @User_Password
if @@rowcount>0
begin

update  [users] set user_LoginTimes=user_LoginTimes+1 where [User_Name] = @User_Name and [User_Pwd] = @User_Password

end
GO



C#.Net

<script type="text/javascript">window.attachEvent("onload",function (){AutoSizeDIV('CODE_6266')})</script>
程序代码:[ 复制代码到剪贴板 ]
Private void memberlogin()    
{
SqlConnection conndb=new SqlConnection(ConfigurationSettings.AppSettings["Conn"]);
   conndb.Open();            
SqlCommand cmdlogin = new SqlCommand("User_login",conndb);
cmdlogin.CommandType = CommandType.StoredProcedure;
cmdlogin.Parameters.Add("@user_name",TxtUser.Text.Trim());
cmdlogin.Parameters.Add("@user_password",TxtPwd.Text.Trim());
SqlDataReader reader=cmdlogin.ExecuteReader();
if(reader.Read())
{
                Session["user"]=reader["user_id"].ToString();
                Session["com"]=reader["com_id"].ToString();
                
                string url;
                url="../user/index.aspx?userid="+ Session["userid"] +"&comid="+ Session["comid"] +"";
                Response.Redirect(url);
            }
            else
            {
                LblError.Text ="Invalid Username or password!Please try again!";

            }


}


VB.Net

<script type="text/javascript">window.attachEvent("onload",function (){AutoSizeDIV('CODE_6635')})</script>
程序代码:[ 复制代码到剪贴板 ]
Private  Sub memberlogin()
Dim conndb As SqlConnection = New SqlConnection(ConfigurationSettings.AppSettings("Conn")) 
   conndb.Open()            
Dim cmdlogin As SqlCommand =  New SqlCommand("User_login",conndb) 
cmdlogin.CommandType = CommandType.StoredProcedure
cmdlogin.Parameters.Add("@user_name",TxtUser.Text.Trim())
cmdlogin.Parameters.Add("@user_password",TxtPwd.Text.Trim())
Dim reader As SqlDataReader = cmdlogin.ExecuteReader() 
If reader.Read() Then
                Session("user")=reader("user_id").ToString()
                Session("com")=reader("com_id").ToString()
 
                Dim url As String
                url="../user/index.aspx?userid="+ Session("userid") +"&comid="+ Session("comid") +""
                Response.Redirect(url)
Else 
                LblError.Text ="Invalid Username or password!Please try again!"
 
End If
 
 
End Sub




2.验证注册用户是否存在

用户控件:
TextBox: TxtMemberID
Label: LblChk


c#代码:


<script type="text/javascript">window.attachEvent("onload",function (){AutoSizeDIV('CODE_4316')})</script>
程序代码:[ 复制代码到剪贴板 ]
private bool idcheck()
        {
            SqlConnection conndb= new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["conn"]);
            conndb.Open();
            string memberid=TxtMemberId.Text.Trim();
            string sql="select User from users where User_Name ='"+memberid+"'";
            SqlCommand strchk=new SqlCommand(sql,conndb);
            SqlDataReader reader=strchk.ExecuteReader();
            if(reader.Read())
            {

                LblChk.Text="Sorry! this memberid was registed,Please choose another!";

                

                Response.Write("<script>al&#101;rt(/"Invalid member id/");</script>");

                

                Response.End();

                

                return false;

            }
            else
            {
                return true;
            }



VB.Net 代码


<script type="text/javascript">window.attachEvent("onload",function (){AutoSizeDIV('CODE_4415')})</script>
程序代码:[ 复制代码到剪贴板 ]
private Boolean idcheck()
        {
            Dim conndb As SqlConnection =  New SqlConnection(System.Configuration.ConfigurationSettings.AppSettings("conn")) 
            conndb.Open()
            Dim memberid As String = TxtMemberId.Text.Trim() 
            Dim sql As String = "select User from users where User_Name ='"+memberid+"'" 
            Dim strchk As SqlCommand = New SqlCommand(sql,conndb) 
            Dim reader As SqlDataReader = strchk.ExecuteReader() 
            If reader.Read() Then
 
                LblChk.Text="Sorry! this memberid was registed,Please choose another!"
 
 
 
                Response.Write("<script>al&#101;rt(/"Invalid member id/");</script>")
 
 
 
                Response.End()
 
 
 
                Return False
 
            Else 
                Return True
            End If




3.新用户注册

用户控件: 
TextBox:TxtMemberId  TxtPwd  TxtEmail 
ListBox:LstIndustry


存储过程:Users_Insert


<script type="text/javascript">window.attachEvent("onload",function (){AutoSizeDIV('CODE_8197')})</script>
程序代码:[ 复制代码到剪贴板 ]
/*
作者:dezai
用途:新进会员的增加注册,同时注册与其相关的企业名录
日期:2006-3-1
*/


CREATE PROCEDURE Users_Insert
@User_Id int output,
@User_Type bit,
@User_Name char(100),
@User_Pwd  char(100),
@User_Email char(100)
AS
   begin tran

    INSERT INTO [Users]
    (
    [user_type],
    [user_name],
    [user_pwd],
    [user_Email]
)

values
(
@User_Type,
@User_Name,
@User_Pwd,
@User_Email
)

if @@error<>0 goto error
set @user_Id=@@identity

Commit tran
return
ERROR:
    set @User_Id = 0
    rollback tran
GO



c#代码:

<script type="text/javascript">window.attachEvent("onload",function (){AutoSizeDIV('CODE_2112')})</script>
程序代码:[ 复制代码到剪贴板 ]
private void reguser()
{
SqlConnection conndb=new SqlConnection(ConfigurationSettings.AppSettings["Conn"]);
                
                SqlCommand cmdinsert = new SqlCommand("Users_Insert",conndb);

                cmdinsert.CommandType=CommandType.StoredProcedure;

                int intAuthorCount;
                cmdinsert.Parameters.Add("@User_Name",TxtMemberId.Text.ToString());
                cmdinsert.Parameters.Add("@User_Pwd",TxtPwd.Text.ToString());
                cmdinsert.Parameters.Add("@User_Email",TxtEmail.Text.ToString());
                cmdinsert.Parameters.Add("@User_Industry",LstIndustry.SelectedValue);
            SqlParameter  parmReturnValue = new SqlParameter("@User_id", SqlDbType.Int);
            parmReturnValue.Direction = ParameterDirection.Output; 
            cmdinsert.Parameters.Add(parmReturnValue);
                conndb.Open();
                cmdinsert.ExecuteNonQuery();
                 intAuthorCount = (int)cmdinsert.Parameters[ "@user_id"].Value; 
                             conndb.Close();
               

}


VB.Net代码

<script type="text/javascript">window.attachEvent("onload",function (){AutoSizeDIV('CODE_8085')})</script>
程序代码:[ 复制代码到剪贴板 ]
Private  Sub reguser()
Dim conndb As SqlConnection = New SqlConnection(ConfigurationSettings.AppSettings("Conn")) 
 
                Dim cmdinsert As SqlCommand =  New SqlCommand("Users_Insert",conndb) 
 
                cmdinsert.CommandType=CommandType.StoredProcedure
 
                Dim intAuthorCount As Integer
                cmdinsert.Parameters.Add("@User_Name",TxtMemberId.Text.ToString())
                cmdinsert.Parameters.Add("@User_Pwd",TxtPwd.Text.ToString())
                cmdinsert.Parameters.Add("@User_Email",TxtEmail.Text.ToString())
                cmdinsert.Parameters.Add("@User_Industry",LstIndusTry.SelectedValue)
            Dim parmReturnValue As SqlParameter =  New SqlParameter("@User_id",SqlDbType.Int) 
            parmReturnValue.Direction = ParameterDirection.Output 
            cmdinsert.Parameters.Add(parmReturnValue)
                conndb.Open()
                cmdinsert.ExecuteNonQuery()
                 intAuthorCount = CType(cmdinsert.Parameters( "@user_id").Value, Integer) 
                             conndb.Close()
 
 
End Sub



4.图片上传

c#.Net


<script type="text/javascript">window.attachEvent("onload",function (){AutoSizeDIV('CODE_8515')})</script>
程序代码:[ 复制代码到剪贴板 ]
private void uppic()
        {

             string mPath;
   string imagePath;
   string imageType;
   string imageName;
    DateTime dtmDate;

    dtmDate = DateTime.Now;

            if(""!=this.fileup.PostedFile.FileName)
            {
                imagePath = this.fileup.PostedFile.FileName;

                imageType = imagePath.Substring(imagePath.LastIndexOf(".")+1);

                imageName=imagePath.Substring(imagePath.LastIndexOf("//")+1);

                if("jpg" != imageType && "gif" !=imageType && "png" !=imageType && "PNG" !=imageType && "GIF" !=imageType && "JPG" !=imageType)
                {
                    Response.Write("<script language='javascript'>al&#101;rt('sorry!Please choose *.jpg or *.gif or *.png');</script>");
 
                    return;
                }
                else

                {
                    try
                    {


                        mPath=Server.MapPath("upfile");

                        this.fileup.PostedFile.SaveAs(mPath+"//"+"dezaistudio"+dtmDate.ToString("yyyyMMddhhmmss")+imageName);

                        this.ImageSmall.ImageUrl  = "dezaistudio"+dtmDate.ToString("yyyyMMddhhmmss")+imageName;
                        

                        Response.Write("<script language='javascript'>al&#101;rt('upload succesful');</script>");

                        TxtPicPath.Text = this.ImageSmall.ImageUrl.ToString().Trim();

                        
                    }
                    catch
                    {
                        Response.Write("error");
                    }
                }
            }
                    
              


        }


VB.Net代码


<script type="text/javascript">window.attachEvent("onload",function (){AutoSizeDIV('CODE_7671')})</script>
程序代码:[ 复制代码到剪贴板 ]
Private  Sub uppic()
 
             Dim mPath As String
   Dim imagePath As String
   Dim imageType As String
   Dim imageName As String
    Dim dtmDate As DateTime
 
    dtmDate = DateTime.Now
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
ASP.NET开发典型模块大全(修订版)》以关键技术和热点技术为核心,通过27个典型模块和5章热点技术,全面地介绍了如何使用asp.net进行各领域的web项目开发。全书共3篇分为32章,第1篇关键模块篇,覆盖网站开发的关键领域,内容涉及论坛、博客、播客、网络硬盘、电子邮件、在线考试、网站备忘录、在线短消息、网站访问量统计与分析、系统后台管理权限分配等网站关键模块;第2篇常见模块篇,覆盖网站开发的各个领域,内容涉及网站会员注册及登录、会员密码找回、留言本、上传与下载、图片资源管理、搜索引擎、网上问卷调查、rss在线订阅、聊天室、购物车、在线银行支付、手机短消息管理、在线音乐、投票系统、万能打印、数据自动备份与恢复等常见模块;第3篇热点技术应用篇,解决网站开发在某个领域遇到的技术难题,内容涉及linq数据访问技术、安全技术、服务技术、ajax、高级应用技术等。 《ASP.NET开发典型模块大全(修订版)》附有配套光盘。光盘提供了书中所有案例的全部源代码,并经过精心调试,在windows xp和windows 2000下全部通过,保证能够正常运行。此外,光盘中还提供有编程词典试用版软件。 《ASP.NET开发典型模块大全(修订版)》案例涉及领域广泛,实用性非常强。学习本书读者可以了解各个领域的特点,能够针对某一行业进行软件开发,也可以通过光盘中提供的模块源代码进行二次开发,以减少开发系统所需要的时间。本书适合各级软件开发人员学习使用,也可供大、中专院校师生学习参考。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值