This Is A FineDay

  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  93 随笔 :: 0 文章 :: 69 评论 :: 0 Trackbacks

 

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 MySql.Data.MySqlClient;

public  partial  class  news : System.Web.UI.Page
{
 
    
protected   void  Page_Load(object sender, EventArgs e)
    
{

         DataTable    dt 
=   new  DataTable();
        string connStr 
=  String.Format( " server={0};user id={1}; password={2}; database=test; pooling=false " ,
                
" 127.0.0.1 " " root " " 123 " );
        MySqlConnection conn 
=   new  MySqlConnection(connStr);
        conn.Open();
        MySqlDataAdapter msda 
=   null ;
        MySqlCommandBuilder cb 
=   null ;
        
try
        
{
            
// 查询
            msda  =   new  MySqlDataAdapter( " select * from test " , conn);
            cb 
=   new  MySqlCommandBuilder(msda);   
            msda.Fill(dt);
        }

        
catch  (MySqlException ex)
        
{
            System.Console.WriteLine(
" Failed to populate mysql data:  "   +  ex.Message);
        }

        
finally
        
{
            
if  (conn  !=   null ) conn.Close();
        }


        
// 更新
        conn.Open();
        DataRow row1 
=  dt.Rows[ 0 ];
        row1[
" name " =   " 00 " ;
        row1[
" password " =   " 00 " ;
        msda.Update(dt);
        dt.AcceptChanges();
        conn.Close();

        
// 新增
        conn.Open();
        DataRow row2 
=  dt.NewRow();
        row2[
" id " =   6 ;
        row2[
" name " =   " 6 " ;
        row2[
" password " =   " 6 " ;
        dt.Rows.Add(row2);
        msda.Update(dt);
        dt.AcceptChanges();
        conn.Close();

        
// 删除
        conn.Open();
        DataRow row3 
=  dt.Rows[ 1 ];
        row3.Delete();
        msda.Update(dt);
        dt.AcceptChanges();
        conn.Close();
        
        
// 填充页面
         this .DataList1.DataSource  =  dt.DefaultView;
        
this .DataList1.DataBind();

    }

  
}

posted on 2008-09-23 23:59 Peter Pan 阅读(182) 评论(0)  编辑  收藏 所属分类: DotNet

只有注册用户登录后才能发表评论。


网站导航: