转至繁体中文版     | 网站首页 | 图文教程 | 资源下载 | 站长博客 | 图片素材 | 武汉seo | 武汉网站优化 | 
最新公告:     敏韬网|教学资源学习资料永久免费分享站!  [mintao  2008年9月2日]        
您现在的位置: 学习笔记 >> 图文教程 >> 站长学院 >> Web开发 >> 正文
在vs.net bate 2中的ado.net简单编程(傻瓜版)         ★★★★

在vs.net bate 2中的ado.net简单编程(傻瓜版)

作者:闵涛 文章来源:闵涛的学习笔记 点击数:674 更新时间:2009/4/23 10:48:00

     这篇文章只适合初学者,如果你是大虾,这篇文章就是浪费你的时间。

首先,文件->兴建->项目。项目类型:C#,ASP。NET应用程序。

在工具箱中拖一个sqldataadapter到webform窗口中。

向导先点下一步,新建连接,在连接标签下,选取一个可用的服务器和可用的sqlserver数据库,确定。

然后配置查询生成器(太简单就不说了)。完成。

在webform中点击sqldataadapter1在属性栏的下面点生成数据集。单选框用新建,确定。

至此,和数据库的连接工作基本搞定了,让我们来操作它吧。

拖一个datagrid到webform 窗口。属性生成器中选好数据源(我们只有一个dataset)也就这样了。

然后我们来看看代码。

切换到代码窗口,添加黄色的代码:

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
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 WebApplication1
{
 /// <summary>
 /// Summary description for WebForm1.
 /// </summary>
 public class WebForm1 : System.Web.UI.Page
 {
  protected System.Data.SqlClient.SqlDataAdapter sqlDataAdapter1;
  protected System.Data.SqlClient.SqlCommand sqlSelectCommand1;
  protected System.Data.SqlClient.SqlCommand sqlInsertCommand1;
  protected System.Data.SqlClient.SqlConnection sqlConnection1;
  protected System.Web.UI.WebControls.DataGrid DataGrid1;
  protected System.Web.UI.WebControls.Button Button1;
  protected System.Data.SqlClient.SqlDataAdapter sqlDataAdapter2;
  protected System.Data.SqlClient.SqlCommand sqlSelectCommand2;
  protected System.Data.SqlClient.SqlCommand sqlInsertCommand2;
  protected System.Data.SqlClient.SqlCommand sqlUpdateCommand1;
  protected System.Data.SqlClient.SqlCommand sqlDeleteCommand1;
  protected System.Data.SqlClient.SqlConnection sqlConnection2;
  protected System.Web.UI.WebControls.DataGrid DataGrid2;
  protected WebApplication1.DataSet1 dataSet11;
 
  public WebForm1()
  {
   Page.Init += new System.EventHandler(Page_Init);
  }

  private void Page_Load(object sender, System.EventArgs e)
  {
   // Put user code to initialize the page here
  }

  private void Page_Init(object sender, EventArgs e)
  {
   //
   // CODEGEN: This call is required by the ASP.NET Web Form Designer.
   //
   InitializeComponent();
 //填充数据集    

 sqlDataAdapter1.Fill (dataSet11);

//绑定
   DataGrid1.DataBind ();
   
  } #region Web Form Designer generated code
  /// <summary>
  /// Required method for Designer support - do not modify
  /// the contents of this method with the code editor.
  /// </summary>
  private void InitializeComponent()

  #endregion

  }

生成程序,如果正常的话你应该可以在datagrid看到你的数据了。

如果你对操作的数据有更高的要求的话(前提是会sql语句),你可以点饥前面绿色代码前的小加号,修改下面的兰色代码。
  private void InitializeComponent()
  {   
   this.sqlConnection1 = new System.Data.SqlClient.SqlConnection();
   this.dataSet11 = new WebApplication1.DataSet1();
   this.sqlInsertCommand1 = new System.Data.SqlClient.SqlCommand();
   this.sqlSelectCommand1 = new System.Data.SqlClient.SqlCommand();
   this.sqlDataAdapter1 = new System.Data.SqlClient.SqlDataAdapter();
   this.sqlDataAdapter2 = new System.Data.SqlClient.SqlDataAdapter();
   this.sqlSelectCommand2 = new System.Data.SqlClient.SqlCommand();
   this.sqlInsertCommand2 = new System.Data.SqlClient.SqlCommand();
   this.sqlUpdateCommand1 = new System.Data.SqlClient.SqlCommand();
   this.sqlDeleteCommand1 = new System.Data.SqlClient.SqlCommand();
   this.sqlConnection2 = new System.Data.SqlClient.SqlConnection();
   ((System.ComponentModel.ISupportInitialize)(this.dataSet11)).BeginInit();
   this.Button1.Click += new System.EventHandler(this.Button1_Click);
   //
   // sqlConnection1
   //
   this.sqlConnection1.ConnectionString = "data source=(local);initial catalog=xr;integrated security=SSPI;persist security " +
    "info=False;workstation id=XURUI;packet size=4096";
   //
   // dataSet11
   //
   this.dataSet11.DataSetName = "DataSet1";
   this.dataSet11.Locale = new System.Globalization.CultureInfo("zh-CN");
   this.dataSet11.Namespace = "http://www.tempuri.org/DataSet1.xsd";
   //
   // sqlInsertCommand1
   //
   this.sqlInsertCommand1.CommandText = "INSERT INTO store(zipcode, area, name) VALUES (@zipcode, @area, @name); SELECT zi" +
    "pcode, area, name FROM store";
   this.sqlInsertCommand1.Connection = this.sqlConnection1;
   this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@zipcode", System.Data.SqlDbType.Char, 10, System.Data.ParameterDirection.Input, true, ((System.Byte)(0)), ((System.Byte)(0)), "zipcode", System.Data.DataRowVersion.Current, null));
   this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@area", System.Data.SqlDbType.Char, 10, System.Data.ParameterDirection.Input, true, ((System.Byte)(0)), ((System.Byte)(0)), "area", System.Data.DataRowVersion.Current, null));
   this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@name", System.Data.SqlDbType.Char, 10, System.Data.ParameterDirection.Input, true, ((System.Byte)(0)), ((System.Byte)(0)), "name", System.Data.DataRowVersion.Current, null));
   //
   // sqlSelectCommand1
   //
   this.sqlSelectCommand1.CommandText = "SELECT zipcode, area, name FROM store ";
   this.sqlSelectCommand1.Connection = this.sqlConnection1;
   //
   // sqlDataAdapter1
   //
   this.sqlDataAdapter1.InsertCommand = this.sqlInsertCommand1;
   this.sqlDataAdapter1.SelectCommand = this.sqlSelectCommand1;
   this.sqlDataAdapter1.TableMappings.AddRange(new System.Data.Common.DataTableMapping[] {
                           new System.Data.Common.DataTableMapping("Table", "store", new System.Data.Common.DataColumnMapping[] {....................................

...........................

你可以根据自己的需要生成数据集。

加入新列你可以这样做:

   DataTable dt;//定义一个表(废话)
   dt=dataSet11.Tables ["你的表"];//给表付值

   DataRow dr;
   dr=dt.NewRow ();//新元组
   dr.BeginEdit ();
    dr["属性1"]=  ;

dr["属性2"]=;

dr["属性3"]=;

       dr.EndEdit ();
   dt.Rows.Add (dr);//加入元组
      DataGrid1.DataBind ();//把改变在datagrid中表示出来,不写也可以但是没现象了。
                  修改的代码:

dr= tblAuthors.Rows.Find("213-46-8915");//找到元组(缺省为找主码)
dr.BeginEdit();
dr["属性1"] = "342" ;

 dr("属性2")=“2342”;

dr.EndEdit()

删除代码:

drCurrent = tblAuthors.Rows.Find("993-21-3427");
drCurrent.Delete();

以上的操作只是对数据集 如果你要把改变保存回数据库还要添一句:

sqlDataAdapter1.Update (dataSet11,"store");

我的文章就此为止。

由于做设计的缘故,我现在对ADO. net很有兴趣,如果有人想讨论,email:

xurui_@hotmail.com


[Web开发]在VS.NET 2005中体验clickonce技术  
教程录入:mintao    责任编辑:mintao 
  • 上一篇教程:

  • 下一篇教程:
  • 【字体: 】【发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口
      注:本站部分文章源于互联网,版权归原作者所有!如有侵权,请原作者与本站联系,本站将立即删除! 本站文章除特别注明外均可转载,但需注明出处! [MinTao学以致用网]
      网友评论:(只显示最新10条。评论内容只代表网友观点,与本站立场无关!)

    同类栏目
    · Web开发  · 网页制作
    · 平面设计  · 网站运营
    · 网站推广  · 搜索优化
    · 建站心得  · 站长故事
    · 互联动态
    更多内容
    热门推荐 更多内容
  • 没有教程
  • 赞助链接
    更多内容
    闵涛博文 更多关于武汉SEO的内容
    500 - 内部服务器错误。

    500 - 内部服务器错误。

    您查找的资源存在问题,因而无法显示。

    | 设为首页 |加入收藏 | 联系站长 | 友情链接 | 版权申明 | 广告服务
    MinTao学以致用网

    Copyright @ 2007-2012 敏韬网(敏而好学,文韬武略--MinTao.Net)(学习笔记) Inc All Rights Reserved.
    闵涛 投放广告、内容合作请Q我! E_mail:admin@mintao.net(欢迎提供学习资源)

    站长:MinTao ICP备案号:鄂ICP备11006601号-18

    闵涛站盟:医药大全-武穴网A打造BCD……
    咸宁网络警察报警平台