代码
using
System;
using System.Data;
using System.Configuration;
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 System.Data.SqlClient;
/// <summary>
/// BasedDb 的摘要说明
/// </summary>
public class BaseDb
{
// 获取数据库连接字符串
private string connectionstring = System.Configuration.ConfigurationManager.AppSettings[ " connectionstring " ].ToString();
public BaseDb()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
public System.Data.DataSet getdatabydataset( string sqlstr)
{
// 功能:根据传入的SQL语句脚本,以Dataset对象形式返回数据集
// 以Dataset对象形式返回数据集
SqlConnection conn = new SqlConnection();
conn.ConnectionString = connectionstring;
// 进行错误处理,如果出现异常,则抛出异常
try
{
conn.Open();
}
catch (Exception e)
{
throw new Exception(e.Message, e);
}
// 创建Dataset对象,用于接收数据库返回记录集
DataSet ds = new DataSet();
SqlDataAdapter sda = new SqlDataAdapter(sqlstr, conn);
try
{
// 对Dataset记录集进行赋值
sda.Fill(ds);
}
catch (Exception err)
{
throw new Exception(err.Message, err);
}
// 调用方法关闭数据库连接
closeconnection(conn);
// 以Dataset形式返回记录集
return ds;
}
public System.Data.DataTable getdatabydatatable( string sqlstr)
{
// 功能:根据传入的SQL语句脚本,以DataTable 对象形式返回数据集
return getdatabydataset(sqlstr).Tables[ 0 ];
}
public string ExecuteSql( string sqlstr)
{
// 功能:执行Sql脚夫本操作,无返回记录集。该方法适用于新增、修改、删除等操作。
// 初始化异常描述变量
string errdesc = "" ;
SqlConnection conn = new SqlConnection();
conn.ConnectionString = connectionstring;
conn.Open();
// 创建命令对象
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
// 将Sql脚本语句赋给命令对象
cmd.CommandText = sqlstr;
try
{
cmd.ExecuteNonQuery();
}
catch (Exception err)
{
errdesc = err.Message.ToString();
throw new Exception(err.Message, err);
}
closeconnection(conn);
return errdesc;
}
private void closeconnection(SqlConnection conn)
{
// 功能:关闭数据库连接对象
conn.Close();
// 回收数据库连接所占系统资源
conn.Dispose();
}
public void Update( string strAdminPassword, string strAdminiName)
{
// 功能:更新数据
string strSql = " UPDATE [Administrator] SET [Admin_password]=' " + strAdminPassword + " ' WHERE [Admin_name] =' " + strAdminiName + " ' " ;
ExecuteSql(strSql);
// SqlConnection conn = new SqlConnection();
// conn.ConnectionString = connectionstring;
// conn.Open();
/// /创建命令对象
// SqlCommand cmd = new SqlCommand();
// cmd.Connection = conn;
/// /将Sql脚本语句赋给命令对象
// cmd.CommandText = strSql;
// try
// {
// cmd.ExecuteNonQuery();
// }
// catch (Exception err)
// {
// throw new Exception(err.Message, err);
// }
// closeconnection(conn);
}
}
using System.Data;
using System.Configuration;
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 System.Data.SqlClient;
/// <summary>
/// BasedDb 的摘要说明
/// </summary>
public class BaseDb
{
// 获取数据库连接字符串
private string connectionstring = System.Configuration.ConfigurationManager.AppSettings[ " connectionstring " ].ToString();
public BaseDb()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
public System.Data.DataSet getdatabydataset( string sqlstr)
{
// 功能:根据传入的SQL语句脚本,以Dataset对象形式返回数据集
// 以Dataset对象形式返回数据集
SqlConnection conn = new SqlConnection();
conn.ConnectionString = connectionstring;
// 进行错误处理,如果出现异常,则抛出异常
try
{
conn.Open();
}
catch (Exception e)
{
throw new Exception(e.Message, e);
}
// 创建Dataset对象,用于接收数据库返回记录集
DataSet ds = new DataSet();
SqlDataAdapter sda = new SqlDataAdapter(sqlstr, conn);
try
{
// 对Dataset记录集进行赋值
sda.Fill(ds);
}
catch (Exception err)
{
throw new Exception(err.Message, err);
}
// 调用方法关闭数据库连接
closeconnection(conn);
// 以Dataset形式返回记录集
return ds;
}
public System.Data.DataTable getdatabydatatable( string sqlstr)
{
// 功能:根据传入的SQL语句脚本,以DataTable 对象形式返回数据集
return getdatabydataset(sqlstr).Tables[ 0 ];
}
public string ExecuteSql( string sqlstr)
{
// 功能:执行Sql脚夫本操作,无返回记录集。该方法适用于新增、修改、删除等操作。
// 初始化异常描述变量
string errdesc = "" ;
SqlConnection conn = new SqlConnection();
conn.ConnectionString = connectionstring;
conn.Open();
// 创建命令对象
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
// 将Sql脚本语句赋给命令对象
cmd.CommandText = sqlstr;
try
{
cmd.ExecuteNonQuery();
}
catch (Exception err)
{
errdesc = err.Message.ToString();
throw new Exception(err.Message, err);
}
closeconnection(conn);
return errdesc;
}
private void closeconnection(SqlConnection conn)
{
// 功能:关闭数据库连接对象
conn.Close();
// 回收数据库连接所占系统资源
conn.Dispose();
}
public void Update( string strAdminPassword, string strAdminiName)
{
// 功能:更新数据
string strSql = " UPDATE [Administrator] SET [Admin_password]=' " + strAdminPassword + " ' WHERE [Admin_name] =' " + strAdminiName + " ' " ;
ExecuteSql(strSql);
// SqlConnection conn = new SqlConnection();
// conn.ConnectionString = connectionstring;
// conn.Open();
/// /创建命令对象
// SqlCommand cmd = new SqlCommand();
// cmd.Connection = conn;
/// /将Sql脚本语句赋给命令对象
// cmd.CommandText = strSql;
// try
// {
// cmd.ExecuteNonQuery();
// }
// catch (Exception err)
// {
// throw new Exception(err.Message, err);
// }
// closeconnection(conn);
}
}
//动态添加数据
添加数据的前台代码
代码
<
table style
=
"
z-index: 106; left: 346px; width: 333px; position: absolute; top: 64px
"
>
< tr >
< td style = " width: 104px " >
</ td >
< td >
</ td >
< td style = " width: 108px " >
</ td >
</ tr >
< tr >
< td style = " width: 104px; height: 21px; " >
管理员账号 </ td >
< td style = " height: 21px " >
密码 </ td >
< td style = " width: 108px; height: 21px; " >
</ td >
</ tr >
< tr >
< td style = " width: 104px " >
< asp:TextBox ID = " Adminname " runat = " server " Style = " z-index: 100; left: 6px; position: absolute;
top: 46px " Width= " 102px " MaxLength= " 20 " ></asp:TextBox>
</ td >
< td >
< asp:TextBox ID = " Adminpassword " runat = " server " Style = " z-index: 100; left: 116px;
position: absolute; top: 48px " Width= " 102px " MaxLength= " 20 " ></asp:TextBox>
</ td >
< td style = " width: 108px " >
< asp:Button ID = " Add " runat = " server " OnClick = " Add_Click " Style = " z-index: 100; left: 237px;
position: absolute; top: 47px " Text= " 添加 " Width= " 64px " />
</ td >
</ tr >
</ table >
< tr >
< td style = " width: 104px " >
</ td >
< td >
</ td >
< td style = " width: 108px " >
</ td >
</ tr >
< tr >
< td style = " width: 104px; height: 21px; " >
管理员账号 </ td >
< td style = " height: 21px " >
密码 </ td >
< td style = " width: 108px; height: 21px; " >
</ td >
</ tr >
< tr >
< td style = " width: 104px " >
< asp:TextBox ID = " Adminname " runat = " server " Style = " z-index: 100; left: 6px; position: absolute;
top: 46px " Width= " 102px " MaxLength= " 20 " ></asp:TextBox>
</ td >
< td >
< asp:TextBox ID = " Adminpassword " runat = " server " Style = " z-index: 100; left: 116px;
position: absolute; top: 48px " Width= " 102px " MaxLength= " 20 " ></asp:TextBox>
</ td >
< td style = " width: 108px " >
< asp:Button ID = " Add " runat = " server " OnClick = " Add_Click " Style = " z-index: 100; left: 237px;
position: absolute; top: 47px " Text= " 添加 " Width= " 64px " />
</ td >
</ tr >
</ table >
后台代码