如何用asp.net和access开发简单工资查询系统
1、打开visual web developer express 2008 速成版,点击建立网站,选择asp.net 网站,语言选择 C#如图1。
2、建立网站后,系统自动建立default.aspx、default.aspx.cs以及webcongfig,现在网站已经建立起来,下一步就是往里面添加功能模块。
3、建立access数据库。选择空白数据库。数据库命名为gzxc.accdb然后建立我们需要的表,user表和gz表。
都是图形化的操作,在此不一一累述。
4、当然在实际使用中也可以直接将exl文件导入access中。选择外部数据----exl----选择导入的数据源-----一步一步操作。
5、建立好数据库后,就要构造数据库连接类。选择c#类模板,名字改为sqlhelper点击确定,建立成功后,会生成sqlhelper.cs文件。
具体代码如下:
public class sqlhelper
{
public static DataTable ExecutQuery(string Sql)
{
DataTable Dt = DataTable();
string ConStr = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\我的文档\Visual Studio 2008\WebSites\WebSite5\gzcx.accdb;Persist Security Info=False";
OleDbConnection objConnection = new OleDbConnection(ConStr);
objConnection.Open();
OleDbCommand cmd = new OleDbCommand(sql, objConnection);
OleDbDataReader sdr = cmd.ExecuteReader();
dt.Load(sdr);
sdr.Close();
objConnection.Close();
return dt;
}
}
在此需要注意的是需要引用access连接函数 using System.Data.OleDb;
并且需要namespace 一个命名空间,这样别的类才能够引用。
次类传入查询sql语句,返回的是一个DataTable。
@是防止系统对/进行转义。
6、建立好数据库连接类后,开发登陆模块。打开default.aspx,设计视图,添加两个textbox和一个button。然后双击button按钮,进入后台编辑,
代码如下:
string yanzheng=TextBox1.Text.ToString();
int changdu=yanzheng.Length;
string SqlStr1 = "select * from [user] where 身份证号码='" + TextBox1.Text + "' and 密码='" + TextBox2.Text + "'";
string SqlStr2 = "select * from [tb_gz] where 身份证号码='" + TextBox1.Text + "'";
if (changdu==18)
{
DataTable dt = sqlhelper.ExecuteQuery(SqlStr1);
if (dt.Rows.Count > 0)
{
Session["usernameid"] = TextBox1.Text.ToString();
Response.Write("<script>alert('输入正确!')</script>");
Response.Redirect("printxinxi.aspx");
}
else
{ Response.Write("<script>alert('用户名或者密码错误,请重新输入!')</script>"); }
}
else
{
Response.Write("<script>alert('请输入正确身份证号码')</script>");
}
}
protected void TextBox1_TextChanged(object sender, EventArgs e)
{
}
protected void TextBox2_TextChanged(object sender, EventArgs e)
{
}
此类需要引用accesslianjie命名空间。
通过session进行页面传值。
7、建立printxinxi.aspx,转到设计页面,拖入gridview控件。
打开printxinxi.aspx.cs,
protected void Page_Load(object sender, EventArgs e)
{
string name = Session["usernameid"].ToString();
string SqlStr2 = "select * from [gz] where 身份证号码='" + name + "'";
GridView1.DataSource = sqlhelper.ExecuteQuery(SqlStr2);
GridView1.DataBind();
}
8、在浏览器中查看,大功告成