asp.net中資料繫結的寫法

來源:互聯網
上載者:User
asp.net|資料

using System;
using System.Data;
using System.Data.SqlClient;
……
namespace XXXX.xxxxxxx
{
 /// <summary>
 /// xxxxx 的摘要說明。
 /// </summary>
 public class xxxxx : System.Web.UI.Page
 {
  protected System.Data.DataRow dr;
  protected System.Data.SqlClient.SqlConnection conn;
  protected System.Data.SqlClient.SqlCommand myCmd,mysqlcmd;
  private void Page_Load(object sender, System.EventArgs e)
  {
   string connstr="data source=xxx.xxx.xxx.xxx;uid=xxxx;pwd=****;database=xxxx";;
   this.conn=new System.Data.SqlClient.SqlConnection(connstr);
  }
//使用SqlDataReader綁定資料
  private void XXXX_Bound()
  {
   conn.Open();
   sql="SELECT * from xxxx ";
    
   myCmd = conn.CreateCommand();
   myCmd.CommandText =sql;
   SqlDataReader sqr=myCmd.ExecuteReader();
   
   while(sqr.Read())
   {
    tb_xxxx.Text=sqr[0].ToString().Trim();
    ……
    
    for(int i=0; i<ddl_xxxx.Items.Count; i++)
    {
     if(ddl_xxxx.Items[i].Value.Trim() == sqr[8].ToString().Trim())
     {
      ddl_xxxx.SelectedIndex = i;
     }
    }

    ……
   }
   conn.Close();
  }
//使用DataTable存取資料後綁定
  private void DataGrid2_Bound()
  {
   conn.Open();
   SqlCommand myCommand= conn.CreateCommand();

   myCommand.CommandText="sp_xxxxxx";
   myCommand.CommandType=CommandType.StoredProcedure;
   SqlParameter Para_Type = myCommand.Parameters.Add("@sql",SqlDbType.VarChar);
   Para_Type.Value ="";
   SqlDataReader sqldr1 = myCommand.ExecuteReader();
   DataTable dt=new DataTable();
   dt.Columns.Add(new DataColumn("XXXX", typeof(string)));
   ……
   while (sqldr1.Read())
   {    
    dr = dt.NewRow();
    for (int i=0; i<sqldr1.FieldCount; i++)
    {      
     dr[i] = sqldr1[i].ToString();
    }
    dt.Rows.Add(dr);
   }

   DataView Source = new DataView(dt);
   DataGrid2.DataSource=Source;
   l_count.Text="共有"+Source.Count.ToString()+"條記錄";
   
   DataGrid2.DataBind();
   conn.Close();
     
  }
//使用DataSet綁定資料
  public void BindGrid(String sortfield)
  {   
   string sqlstring ="SELECT 使用者名稱, 姓名 FROM ryxx";
   conn.Open();
   SqlDataAdapter myCommand = new SqlDataAdapter(sqlstring, conn);
   DataSet ds = new DataSet();
   myCommand.Fill(ds,"ryxx");
   DataView Source = ds.Tables["ryxx"].DefaultView;
   Source.Sort = sortfield;
   DataGrid1.DataSource=Source;
   DataGrid1.DataBind();
   conn.Close();
  }
//執行除Select外的SQL語句
  private void ddl_xxxx_Bind()
  {   
   string updateshry;
   updateshry="update ryxx set 使用者名稱='fdsas', 姓名='kjdsj'";
   SqlCommand comshry=new SqlCommand(updateshry,conn);
   conn.Open();
   try
   {
    Cm.ExecuteNonQuery(); 
   }
   catch(SqlException E)
   {
    this.Response.Write("<script language=javascript>alert('異常資訊:"+E.ToString()+"');</script>");
   }
   conn.Close();
  }
 }
}



相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.