gridview 动态数据操作

自定义数据库操作类:如下:
ExpandedBlockStart.gif 代码
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);
         
    }
}
 
 
//动态添加数据
 
添加数据的前台代码
ExpandedBlockStart.gif 代码
< 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 >

 

 
 
 

 

 

 

后台代码

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值