zoukankan      html  css  js  c++  java
  • C#数据库操作的几种读值对比

    11程序的编写总能体现其灵活性,或许编程的乐趣莫不如此,而往往大都的初学者应用起又总显得不很灵活,究其因,感性认识到理性认识需要有个过程,而其中一个最简单的方法就是我们都要学会比较,这样或许我们能发现很多让我们感兴趣的问题:

    下面就传递来的ID值,我们根据此ID然后从数据库中获得我们想要的东西,我列举了几种方法,共享给所有的初学者,如果对你有帮助,幸甚倍至!!!!!

    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 System.Data.SqlClient;

    public partial class edit : System.Web.UI.Page

    {

         protected void Page_Load(object sender, EventArgs e)

         {

             if (!IsPostBack)

             {

                 int tid = Convert.ToInt32(Request.QueryString["id"]);

                 if (tid != null)

                 {

                    //从类中取得数据库的连接

                     Class1 cl = new Class1();

                     SqlConnection conn = cl.sqlc();

                     //以下为数据库连接

                     //string connstring = ConfigurationManager.ConnectionStrings["TreeConn"].ConnectionString;

                     //SqlConnection conn = new SqlConnection(connstring);

                     //conn.Open();

                     //以下是利用ExecuteScalar()方法:

                     SqlCommand comm = new SqlCommand("select ColunmnName from t_Column where ColumnId=" + tid + "", conn);

                     Label1.Text = comm.ExecuteScalar().ToString();

            

                     //以下为dataset方法

                     DataSet ds = new DataSet();

                    SqlDataAdapter sda = new SqlDataAdapter("select ColunmnName from t_Column where ColumnId=" + tid + "", conn);

                     sda.Fill(ds, "name");

                     Label1.Text = ds.Tables["name"].Rows[0]["ColunmnName"].ToString();

                    // 以下是利用ExecuteReader()方法:

                   SqlCommand comm = new SqlCommand("select ColunmnName from t_Column where ColumnId=" + tid + "", conn);

                     SqlDataReader sdr=comm.ExecuteReader();

                     if (sdr.Read())

                     {

                         Label1.Text = sdr["ColunmnName"].ToString();

                         Label2.Text = sdr["ColunmnName"].ToString();

                     }

                 }

                // Button del = new Button();

                 //del = (Button)Page.FindControl("Button1");

                // del.Attributes.Add("onclick", "return confirm('确定修改吗?');");

             }

         }

    }

  • 相关阅读:
    GBDT(MART)
    C#中数组中Skip、Take和Concat的用法
    VUE中对获取到的数组进行排序
    el-date-picker只能选择今天
    Vue获取时间
    执行Add-Migration Initial报错
    Vue中使用for循环绑定值
    Element UI——日期时间选择器el-date-picker开始时间与结束时间约束解决方案
    el-date-picker日期组件
    缓存的问题
  • 原文地址:https://www.cnblogs.com/liudong/p/1211972.html
Copyright © 2011-2022 走看看