ASP.Net学习之常用模块整理(1)
作者:admin 日期:2006-05-17
每做完一个项目,就可以有很多的经验跟技巧学到,感谢公司能给我这么一个机会做这么一个大项目,让我从一个从刚学会一点ASP.Net的菜鸟过渡到可以用c#正常手写相关常用模块的一个真正的程序员,一个人做项目可以学得很多很多的东西,当然其中碰到困难也只能靠自己一个人去解决.为了让大家更好的学习,我把常用的模块写成一个个方法,希望能对大家有用.每个人都有不同的算法跟代码习惯,而且个人水平有限,欢迎大家多多指正.
前提准备:
由于项目是采用SQL数据库,所以我们先在web.config中设置好数据库连接
之后在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
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
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
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#代码:
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("");
Response.End();
return false;
}
else
{
return true;
}
VB.Net 代码
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("")
Response.End()
Return False
Else
Return True
End If
3.新用户注册
用户控件:
TextBox:TxtMemberId TxtPwd TxtEmail
ListBox:LstIndustry
存储过程:Users_Insert
/*
作者: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#代码:
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代码
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
VB.Net代码
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
If ""<>Me.fileup.PostedFile.FileName Then
imagePath = Me.fileup.PostedFile.FileName
imageType = imagePath.Substring(imagePath.LastIndexOf(".")+1)
imageName=imagePath.Substring(imagePath.LastIndexOf("\\")+1)
If "jpg" <> imageType And "gif" <>imageType And "png" <>imageType And "PNG" <>imageType And "GIF" <>imageType And "JPG" <>imageType Then
Response.Write("")
Return
Else
Try
mPath=Server.MapPath("upfile")
Me.fileup.PostedFile.SaveAs(mPath+"\\"+"dezaistudio"+dtmDate.ToString("yyyyMMddhhmmss")+imageName)
Me.ImageSmall.ImageUrl = "dezaistudio"+dtmDate.ToString("yyyyMMddhhmmss")+imageName
Response.Write("")
TxtPicPath.Text = Me.ImageSmall.ImageUrl.ToString().Trim()
Catch
Response.Write("error")
End Try
End If
End If
End Sub
前提准备:
由于项目是采用SQL数据库,所以我们先在web.config中设置好数据库连接
之后在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
复制内容到剪贴板 程序代码
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
复制内容到剪贴板 程序代码
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
复制内容到剪贴板 程序代码
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#代码:
复制内容到剪贴板 程序代码
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("");
Response.End();
return false;
}
else
{
return true;
}
VB.Net 代码
复制内容到剪贴板 程序代码
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("")
Response.End()
Return False
Else
Return True
End If
3.新用户注册
用户控件:
TextBox:TxtMemberId TxtPwd TxtEmail
ListBox:LstIndustry
存储过程:Users_Insert
复制内容到剪贴板 程序代码
/*
作者: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#代码:
复制内容到剪贴板 程序代码
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代码
复制内容到剪贴板 程序代码
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
复制内容到剪贴板 程序代码
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("");
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("");
TxtPicPath.Text = this.ImageSmall.ImageUrl.ToString().Trim();
}
catch
{
Response.Write("error");
}
}
}
}
{
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("");
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("");
TxtPicPath.Text = this.ImageSmall.ImageUrl.ToString().Trim();
}
catch
{
Response.Write("error");
}
}
}
}
VB.Net代码
复制内容到剪贴板 程序代码
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
If ""<>Me.fileup.PostedFile.FileName Then
imagePath = Me.fileup.PostedFile.FileName
imageType = imagePath.Substring(imagePath.LastIndexOf(".")+1)
imageName=imagePath.Substring(imagePath.LastIndexOf("\\")+1)
If "jpg" <> imageType And "gif" <>imageType And "png" <>imageType And "PNG" <>imageType And "GIF" <>imageType And "JPG" <>imageType Then
Response.Write("")
Return
Else
Try
mPath=Server.MapPath("upfile")
Me.fileup.PostedFile.SaveAs(mPath+"\\"+"dezaistudio"+dtmDate.ToString("yyyyMMddhhmmss")+imageName)
Me.ImageSmall.ImageUrl = "dezaistudio"+dtmDate.ToString("yyyyMMddhhmmss")+imageName
Response.Write("")
TxtPicPath.Text = Me.ImageSmall.ImageUrl.ToString().Trim()
Catch
Response.Write("error")
End Try
End If
End If
End Sub
上一篇: Asp.net学习之第一个web service发布
下一篇: ASP.Net学习之常用模块整理(2)
文章来自: 本站原创
引用通告: 查看所有引用 | 我要引用此文章
Tags:
相关日志:
下一篇: ASP.Net学习之常用模块整理(2)
文章来自: 本站原创
引用通告: 查看所有引用 | 我要引用此文章
Tags:
相关日志:
评论: 0 | 引用: 509 | 查看次数: 46894
发表评论