每做完一个项目,就可以有很多的经验跟技巧学到,感谢公司能给我这么一个机会做这么一个大项目,让我从一个从刚学会一点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>
C#.Net
<script type="text/javascript">window.attachEvent("onload",function (){AutoSizeDIV('CODE_6266')})</script>
VB.Net
<script type="text/javascript">window.attachEvent("onload",function (){AutoSizeDIV('CODE_6635')})</script>
2.验证注册用户是否存在
用户控件:
TextBox: TxtMemberID
Label: LblChk
c#代码:
<script type="text/javascript">window.attachEvent("onload",function (){AutoSizeDIV('CODE_4316')})</script>
VB.Net 代码
<script type="text/javascript">window.attachEvent("onload",function (){AutoSizeDIV('CODE_4415')})</script>
3.新用户注册
用户控件:
TextBox:TxtMemberId TxtPwd TxtEmail
ListBox:LstIndustry
存储过程:Users_Insert
<script type="text/javascript">window.attachEvent("onload",function (){AutoSizeDIV('CODE_8197')})</script>
c#代码:
<script type="text/javascript">window.attachEvent("onload",function (){AutoSizeDIV('CODE_2112')})</script>
VB.Net代码
<script type="text/javascript">window.attachEvent("onload",function (){AutoSizeDIV('CODE_8085')})</script>
4.图片上传
c#.Net
<script type="text/javascript">window.attachEvent("onload",function (){AutoSizeDIV('CODE_8515')})</script>
VB.Net代码
<script type="text/javascript">window.attachEvent("onload",function (){AutoSizeDIV('CODE_7671')})</script>
前提准备:
由于项目是采用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
@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!";
}
}
{
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
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>alert(/"Invalid member id/");</script>");
Response.End();
return false;
}
else
{
return true;
}
{
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>alert(/"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>alert(/"Invalid member id/");</script>")
Response.End()
Return False
Else
Return True
End If
{
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>alert(/"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
作者: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();
}
{
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
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'>alert('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'>alert('upload succesful');</script>");
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("<script language='javascript'>alert('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'>alert('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
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("<script language='javascript'>alert('sorry!Please choose *.jpg or *.gif or *.png');</script>")
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("<script language='javascript'>alert('upload succesful');</script>")
TxtPicPath.Text = Me.ImageSmall.ImageUrl.ToString().Trim()
Catch
Response.Write("error")
End Try
End If
End If
End Sub
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("<script language='javascript'>alert('sorry!Please choose *.jpg or *.gif or *.png');</script>")
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("<script language='javascript'>alert('upload succesful');</script>")
TxtPicPath.Text = Me.ImageSmall.ImageUrl.ToString().Trim()
Catch
Response.Write("error")
End Try
End If
End If
End Sub