(一)如何制作信息管理系统的注册页面
运用工具SQL 2010 Visual studio 2014
第一步新建数据库
1.新建数据库
2.在User数据库中新建表UserID
第二部创建注册页面
1. 在Vs2010新建visual studio项目解决方案User,右击User项目解决方案,点击添加;新建网站;右侧解决方案资源管理器中右击,添加新项;创建web窗体,命名为zhuce;
2. 新建的窗体为zhuce .aspx;在zhuce.aspx窗体右侧,打开工具箱,在工具箱里拖拽四个label控件,Text属性改为“账号”“密码”;拖拽四个Textbox文本框;再拉一个按钮button控件;Text命名为“注册”;
3. 在zhuce.aspx页面拖拽一个Sqldatasource控件,点击配置数据源,新建连接,选择数据库User,绑定数据库,
4. 注册页面前台设计界面
5. 添加类;修改ConnectionStrings连接的数据库名【“UserConnectionStrings”】,zhuce.aspx.cs;在资源解决方案管理器中添加 “类”,命名为DBhelp.cs; 添加DBHelp类包,代码如下
using System;
usingSystem.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
usingSystem.Data.SqlClient;
usingSystem.Configuration;
///<summary>
///DBHelp 的摘要说明
///</summary>
public class DBHelp
{
//自Á?定¡§义°?方¤?法¤¡§
//1.获?得Ì?web.config里¤?面?的Ì?连¢?接¨®字Á?符¤?串ä?
public SqlConnectiongetConn() //SqlConnection为a返¤¦Ì回?类¤¨¤型¨ª
{
//读¨¢取¨?配?置?文?件t里¤?的Ì?连¢?接¨®字Á?符¤?串ä?dengluTestConnectionString
string ConnString = ConfigurationManager.ConnectionStrings["UserConnectionString"].ConnectionString; //对?应®|web.connectionStrings>;访¤?问¨º数ºy据Y库a
//新?建¡§连¢?接¨®对?象¨®
SqlConnection con = newSqlConnection(ConnString);
//打䨰开a连¢?接¨®;ê?
con.Open();
return con;
}
//2.对?数ºy据Y库a增?删¦?改?查¨¦,ê?只?许¨ª传ä?入¨?一°?个?sql语®?句?
public int exec(String sql)
{
SqlConnection con = getConn();
SqlCommand cmd = newSqlCommand(sql, con);//两¢?个?参?数ºy srtring 连¢?接¨®数ºy据Y库a
int i = cmd.ExecuteNonQuery(); //增?删¦?改?用®?这a个?函¡¥数ºy
return i;
}
//3.根¨´据Y传ä?入¨?的Ì?sql 语®?句?,ê?返¤¦Ì回?数ºy据Y读¨¢取¨?器¡Â
public SqlDataReadergetReader(String sql)
{
SqlConnection con = getConn();
SqlCommand cmd = newSqlCommand(sql,con);//两¢?个?参?数ºy string 连¢?接¨®数ºy据Y库a
return cmd.ExecuteReader();
}
//4.根¨´据Y传ä?入¨?的Ì?Aql和¨ª表À¨ª名?返¤¦Ì回?数ºy据Y集¡¥,ê?专Á¡§用®?于®¨²gridview
public DataSetgetDataSet(String sql)
{
SqlConnection con = getConn();
SqlDataAdapter adp = newSqlDataAdapter(sql, con);
DataSet ds = new DataSet();
adp.Fill(ds);
returnds;
}
//4.根¨´据Y传ä?入¨?的Ì?sql语®?句?读¨¢取¨?,ê?返¤¦Ì回?的Ì?true或¨°false;ê?
public bool read(String sql)
{
bool result = true;
SqlConnection con = getConn();
SqlCommand cmd = newSqlCommand(sql, con);
SqlDataReader reader = cmd.ExecuteReader();
if (reader.Read())
{
result = true;
}
else
{
result = false;
}
reader.Close(); //读¨¢取¨?关?了¢?
con.Close(); //连¢?接¨®关?了¢?
return result;
}
//5. 根¨´据Ysql语®?句?返¤¦Ì回?首º¡Á行D首º¡Á列¢D;ê?
public boolgetScalar(String sql)
{
bool result = true;
SqlConnection con = getConn();
SqlCommand cmd = newSqlCommand(sql, con);
int i = Convert.ToInt32(cmd.ExecuteScalar());
//强?制?类¤¨¤型¨ª转Áa换?
if (i == 0)
{
result = false;
}
else
{
result = true;
}
return result;
}
//返¤¦Ì回?object
public objectExecuteScalar(String sql)
{
object obj = null;
SqlConnection con = getConn();
try
{
SqlCommand cmd = new SqlCommand(sql,con);
obj = cmd.ExecuteScalar();
}
finally
{
con.Close();
}
returnobj;
}
}
6.注册页面后台代码(第一步先添加命名空间)(注意修改web.config的<compilationdebug="true"targetFramework="4.0" />)
using System;
usingSystem.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
usingSystem.Data.SqlClient;
usingSystem.Configuration;
public partial class zhuce : System.Web.UI.Page
{
DBHelp db = new DBHelp();
protected voidPage_Load(object sender, EventArgs e)
{
}
protected voidButton1_Click(object sender, EventArgs e)
{
string name = TextBox1.Text;
string password = TextBox2.Text;
string apassword = TextBox3.Text;
string emal = TextBox4.Text;
SqlConnection con = db.getConn();
try
{
//编À¨¤辑-SQL查¨¦询¡¥语®?句?
string sql = string.Format("select * from UserID where Username='{0}';",name);
//执¡ä行D
//建立数ºy据Y库a读¨¢写¡ä器¡Â
SqlDataReader dr = db.getReader(sql);
if (!dr.Read())//验¨¦证¡è成¨¦功|
{
//编À¨¤辑-sql修T改?语®?句?
sql = "insert intoUserID(Username,Passward,Apassword,Emal)values('" + name + "','" + password + "','" + apassword + "','" + emal + "')";
if (db.exec(sql) >= 0)
{
Response.Write("<script>alert('注Á¡é册¨¢成¨¦功|!ê?请?点Ì?击¡Â“¡ã登Ì?录?”¡À跳¬?回?登Ì?录?界?面?')</script>");
}
else
{
Response.Write("<script>alert('注Á¡é册¨¢失º¡ì败㨹!ê?')</script>");
}
}
else
{
Response.Write("<script>alert('此ä?用®?户¡ì名?已°?被À?注Á¡é册¨¢!ê?')</script>");
}
}
finally
{
con.Close();
}
}
}
7.注册页面运行结果