程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> 關於C語言 >> 在vs.net bate 2中的ado.net簡單編程(傻瓜版)

在vs.net bate 2中的ado.net簡單編程(傻瓜版)

編輯:關於C語言

這篇文章只適合初學者,如果你是大蝦,這篇文章就是浪費你的時間。

首先,文件->興建->項目。項目類型: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=d

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved