简单数据库操作
2008-03-06 09:13 澜心 阅读(275) 评论(0) 编辑 收藏 举报using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Logic;
public partial class edit : System.Web.UI.Page
{
private string ID
{
get
{
if (Request["ID"] != null)
return Request["ID"].ToString();
else
return "";
}
}
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
if (ID.Length > 0)
{
Users users = new Users();
users.LoadMe(int.Parse(ID));
txtName.Text = users.Name;
txtPassword.Text = users.Password;
}
}
}
protected void TextBox1_TextChanged(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
Users users = new Users();
users.Name = txtName.Text;
users.Password = txtPassword.Text;
if (ID.Length > 0)
{
users.ID = Convert.ToInt32(ID);
users.Update();
Page.RegisterStartupScript("ss", "<script>alert('修改成功!');window.opener.__doPostBack('Button1','');window.close();</script>");
}
else
{
users.Insert();
Page.RegisterStartupScript("ss", "<script>alert('添加成功!');window.opener.__doPostBack('Button1','');window.close();</script>");
}
//Page.RegisterStartupScript("sfsfsdf","<script>alert('添加成功!');</script>");
}
}
数据层类:主要用于数据访问using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Logic;
public partial class edit : System.Web.UI.Page
{
private string ID
{
get
{
if (Request["ID"] != null)
return Request["ID"].ToString();
else
return "";
}
}
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
if (ID.Length > 0)
{
Users users = new Users();
users.LoadMe(int.Parse(ID));
txtName.Text = users.Name;
txtPassword.Text = users.Password;
}
}
}
protected void TextBox1_TextChanged(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
Users users = new Users();
users.Name = txtName.Text;
users.Password = txtPassword.Text;
if (ID.Length > 0)
{
users.ID = Convert.ToInt32(ID);
users.Update();
Page.RegisterStartupScript("ss", "<script>alert('修改成功!');window.opener.__doPostBack('Button1','');window.close();</script>");
}
else
{
users.Insert();
Page.RegisterStartupScript("ss", "<script>alert('添加成功!');window.opener.__doPostBack('Button1','');window.close();</script>");
}
//Page.RegisterStartupScript("sfsfsdf","<script>alert('添加成功!');</script>");
}
}
using System;
using System.Data;
using System.Data.Sql;
using System.Data.SqlClient;
/// <summary>
/// DataAccess 的摘要说明
/// </summary>
public class DataAccess
{
string connectionStr = "";
SqlConnection conn;
SqlCommand comm;
SqlDataAdapter sqlDataAdapter;
public DataAccess()
{
connectionStr = System.Configuration.ConfigurationSettings.AppSettings["connnectionstring"];
conn = new SqlConnection(connectionStr);
}
/// <summary>
/// 执行命令,返回受影响的行数
/// </summary>
/// <param name="sql"></param>
/// <returns></returns>
public int ExecuteNoQuery(string sql)
{
conn.Open();
//comm = new SqlCommand();
//comm.Connection = conn;
//comm.CommandText = sql;
comm = new SqlCommand(sql, conn);
int result = comm.ExecuteNonQuery();
comm.Dispose();
conn.Close();
return result;
}
/// <summary>
/// 返回已经填充数据的DataSet
/// </summary>
/// <param name="sql"></param>
/// <returns></returns>
public DataSet ExecuteDataSet(string sql)
{
conn.Open();
comm = new SqlCommand(sql, conn);
sqlDataAdapter = new SqlDataAdapter(comm);
DataSet ds = new DataSet();
sqlDataAdapter.Fill(ds);
conn.Close();
return ds;
}
public string ExecuteScalar(string sql)
{
conn.Open();
//comm = new SqlCommand();
//comm.Connection = conn;
//comm.CommandText = sql;
comm = new SqlCommand(sql, conn);
object obj = comm.ExecuteScalar();
comm.Dispose();
conn.Close();
return obj.ToString();
}
}
逻辑层 主要是实体类using System.Data;
using System.Data.Sql;
using System.Data.SqlClient;
/// <summary>
/// DataAccess 的摘要说明
/// </summary>
public class DataAccess
{
string connectionStr = "";
SqlConnection conn;
SqlCommand comm;
SqlDataAdapter sqlDataAdapter;
public DataAccess()
{
connectionStr = System.Configuration.ConfigurationSettings.AppSettings["connnectionstring"];
conn = new SqlConnection(connectionStr);
}
/// <summary>
/// 执行命令,返回受影响的行数
/// </summary>
/// <param name="sql"></param>
/// <returns></returns>
public int ExecuteNoQuery(string sql)
{
conn.Open();
//comm = new SqlCommand();
//comm.Connection = conn;
//comm.CommandText = sql;
comm = new SqlCommand(sql, conn);
int result = comm.ExecuteNonQuery();
comm.Dispose();
conn.Close();
return result;
}
/// <summary>
/// 返回已经填充数据的DataSet
/// </summary>
/// <param name="sql"></param>
/// <returns></returns>
public DataSet ExecuteDataSet(string sql)
{
conn.Open();
comm = new SqlCommand(sql, conn);
sqlDataAdapter = new SqlDataAdapter(comm);
DataSet ds = new DataSet();
sqlDataAdapter.Fill(ds);
conn.Close();
return ds;
}
public string ExecuteScalar(string sql)
{
conn.Open();
//comm = new SqlCommand();
//comm.Connection = conn;
//comm.CommandText = sql;
comm = new SqlCommand(sql, conn);
object obj = comm.ExecuteScalar();
comm.Dispose();
conn.Close();
return obj.ToString();
}
}
using System;
using System.Data;
using System.Collections.Generic;
using System.Text;
namespace Logic
{
public class Users
{
DataAccess da = new DataAccess();
属性,分别对应数据库中的字段
/// <summary>
/// 根据主键,去数据库中查询,并用查询到的数据填充该类
/// </summary>
/// <param name="ID"></param>
public void LoadMe(int ID)
{
string sql = string.Format("select * from users where 1=1 and ID = {0}", ID);
DataSet ds = da.ExecuteDataSet(sql);
DataTable dt = ds.Tables[0];
DataRow row = dt.Rows[0];
m_Name = row["name"].ToString();
m_Password = row["password"].ToString();
}
public bool Insert()
{
string sql = string.Format("insert into users(name,password) values('{0}','{1}')",m_Name,m_Password);
int result = da.ExecuteNoQuery(sql);
return (result == 1);
}
public bool Update()
{
string sql = string.Format("update users set name = '{0}',password = '{1}' where 1=1 and ID = {2}", m_Name, m_Password,m_ID);
int result = da.ExecuteNoQuery(sql);
return (result == 1);
}
public bool Delete()
{
string sql = string.Format(" delete from users where 1=1 and ID = {0}", m_ID);
int result = da.ExecuteNoQuery(sql);
return (result == 1);
}
public Users()
{
}
public bool Login(string name, string password)
{
StringBuilder sb = new StringBuilder();
sb.Append("select count(*) from users where 1=1 ");
sb.AppendFormat(" and name = '{0}' and password = '{1}' ", name,password);
string result = da.ExecuteScalar(sb.ToString());
return (result == "1");
}
}
}
using System.Data;
using System.Collections.Generic;
using System.Text;
namespace Logic
{
public class Users
{
DataAccess da = new DataAccess();
属性,分别对应数据库中的字段
/// <summary>
/// 根据主键,去数据库中查询,并用查询到的数据填充该类
/// </summary>
/// <param name="ID"></param>
public void LoadMe(int ID)
{
string sql = string.Format("select * from users where 1=1 and ID = {0}", ID);
DataSet ds = da.ExecuteDataSet(sql);
DataTable dt = ds.Tables[0];
DataRow row = dt.Rows[0];
m_Name = row["name"].ToString();
m_Password = row["password"].ToString();
}
public bool Insert()
{
string sql = string.Format("insert into users(name,password) values('{0}','{1}')",m_Name,m_Password);
int result = da.ExecuteNoQuery(sql);
return (result == 1);
}
public bool Update()
{
string sql = string.Format("update users set name = '{0}',password = '{1}' where 1=1 and ID = {2}", m_Name, m_Password,m_ID);
int result = da.ExecuteNoQuery(sql);
return (result == 1);
}
public bool Delete()
{
string sql = string.Format(" delete from users where 1=1 and ID = {0}", m_ID);
int result = da.ExecuteNoQuery(sql);
return (result == 1);
}
public Users()
{
}
public bool Login(string name, string password)
{
StringBuilder sb = new StringBuilder();
sb.Append("select count(*) from users where 1=1 ");
sb.AppendFormat(" and name = '{0}' and password = '{1}' ", name,password);
string result = da.ExecuteScalar(sb.ToString());
return (result == "1");
}
}
}
List页面
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
//using Data;
using Logic;
public partial class List : System.Web.UI.Page
{
DataAccess da = new DataAccess();
protected void Page_Load(object sender, EventArgs e)
{
///第一次加载的时候执行,点击按钮等请求的时候不再执行该段代码
if (!Page.IsPostBack)
{
BindGridView();
}
}
public void BindGridView()
{
string sql = "select * from users ";
DataSet ds = da.ExecuteDataSet(sql);
GridView1.DataSource = ds;
GridView1.DataBind();
}
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
}
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
string cmd = e.CommandName.ToString();
string ID = e.CommandArgument.ToString();
Users users = new Users();
users.ID = int.Parse(ID);
users.Delete();
BindGridView();
}
protected void Button1_Click(object sender, EventArgs e)
{
BindGridView();
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
LinkButton deleteBtn = (LinkButton)e.Row.FindControl("LbDeleteButton");
deleteBtn.Attributes.Add("onclick", "return confirm('确定删除吗?');");
}
}
}
编辑页面using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
//using Data;
using Logic;
public partial class List : System.Web.UI.Page
{
DataAccess da = new DataAccess();
protected void Page_Load(object sender, EventArgs e)
{
///第一次加载的时候执行,点击按钮等请求的时候不再执行该段代码
if (!Page.IsPostBack)
{
BindGridView();
}
}
public void BindGridView()
{
string sql = "select * from users ";
DataSet ds = da.ExecuteDataSet(sql);
GridView1.DataSource = ds;
GridView1.DataBind();
}
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
}
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
string cmd = e.CommandName.ToString();
string ID = e.CommandArgument.ToString();
Users users = new Users();
users.ID = int.Parse(ID);
users.Delete();
BindGridView();
}
protected void Button1_Click(object sender, EventArgs e)
{
BindGridView();
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
LinkButton deleteBtn = (LinkButton)e.Row.FindControl("LbDeleteButton");
deleteBtn.Attributes.Add("onclick", "return confirm('确定删除吗?');");
}
}
}