Web.config文件:
<appSettings>
<add key="connectionString" value="server=WAYGOING-12345;uid=sa;pwd=111111; database=111"/>
</appSettings>
类文件:
//conn
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
namespace datamtest
{
/// <summary>
/// conn 的摘要说明。
/// </summary>
public class conn
{
public conn()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
public SqlConnection connstr; //连接字符串
public string getconnstr() //获取连接字符串
{
string constr;
constr=System.Configuration.ConfigurationSettings.AppSettings["connectionString"];
return constr;
}
public void open() //打开数据库
{
string constr;
constr=getconnstr();
connstr=new SqlConnection(constr);
connstr.Open();
}
public void close() //关闭数据库
{
connstr.Dispose();
connstr.Close();
}
public void execsql(string sql) //执行不带返回值的sql语句,如添加、修改、删除
{
open();
SqlCommand cmd=new SqlCommand(sql,connstr);
cmd.ExecuteNonQuery();
close();
}
public DataSet dataset(string sql) //返回DataSet对象
{
open();
SqlDataAdapter rs=new SqlDataAdapter(sql,connstr);
DataSet ds=new DataSet();
rs.Fill(ds,"ds");
return ds;
}
public DataView dataview(string sql) //返回DataView对象
{
DataSet ds=new DataSet();
ds=dataset(sql);
DataView dv=new DataView(ds.Tables[0]);
return dv;
}
public SqlDataReader datareader(string sql) //返回DataReader对象
{
open();
SqlCommand cmd=new SqlCommand(sql,connstr);
SqlDataReader dr=cmd.ExecuteReader();
return dr;
}
}
}
调用文件:
//aspx文件
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace datamtest
{
/// <summary>
/// WebForm1 的摘要说明。
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid DataGrid1;
protected System.Web.UI.WebControls.Label Label1;
protected System.Web.UI.WebControls.TextBox TextBox1;
protected System.Web.UI.WebControls.TextBox TextBox2;
protected System.Web.UI.WebControls.TextBox TextBox3;
protected System.Web.UI.WebControls.TextBox TextBox4;
protected System.Web.UI.WebControls.TextBox TextBox5;
protected System.Web.UI.WebControls.Button Button1;
protected System.Web.UI.WebControls.Button Button2;
protected System.Web.UI.WebControls.TextBox TextBox6;
protected System.Web.UI.WebControls.Button Button3;
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
if(!IsPostBack)
{
bind();
}
}
private void bind()
{
conn myconn = new conn();
// 调用dataset函数
DataSet ds=new DataSet();
string sql = "select * from admininfo";
ds = myconn.dataset(sql);
DataGrid1.DataSource=ds.Tables["ds"].DefaultView;
DataGrid1.DataBind();
myconn.close();
//调用DataReader函数
Label1.Text="";
SqlDataReader dr = myconn.datareader(sql);
while(dr.Read())
{
Label1.Text += dr["adminId"] + " " + dr["adminName"] + " " + dr["PassWord"] + " " + dr["LastLogin"] + " " + dr["LastLoginIp"] + "<br>";
}
myconn.close();
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Button2.Click += new System.EventHandler(this.Button2_Click);
this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.Button3.Click += new System.EventHandler(this.Button3_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void Button1_Click(object sender, System.EventArgs e)
{
conn myconn = new conn();
string sql = "insert into admininfo (adminId,adminName,PassWord,LastLogin,LastLoginIp) values ('"+TextBox5.Text+"','"+TextBox4.Text+"','"+TextBox3.Text+"','2007-1-1','192.168.0.1')";
myconn.execsql(sql);
bind();
}
private void Button2_Click(object sender, System.EventArgs e)
{
conn myconn = new conn();
string sql = "update admininfo set PassWord = '" + TextBox1.Text + "' where id = " + TextBox2.Text;
myconn.execsql(sql);
bind();
}
private void Button3_Click(object sender, System.EventArgs e)
{
conn myconn = new conn();
string sql = "delete admininfo where id = " + TextBox6.Text;
myconn.execsql(sql);
bind();
}
}
}