zoukankan      html  css  js  c++  java
  • 使用DataSet向GridView中添加内容

     

    在使用DataSet前要在开头添加

    1 using system.Data

    新建一个aspx文件,在工具栏中找到GridView控件,插入页面

    打开对应的.aspx.cs配置文件,插入以下代码

    1 DataSet ds = helper.getData("select * from tbArticle");
    2 GridView1.DataSource = ds.Tables[0].DefaultView;
    3 GridView1.DataBind();

     其中helper.getData()是为了方便,我自己写的一个类里面的函数,也可以写在外面

     1     public DataSet getData(string sqlStr)
     2     {
     3         string conStr = ConfigurationManager.ConnectionStrings["conStr"].ConnectionString;
     4         SqlConnection con = new SqlConnection(conStr);
     5         SqlCommand cmd = new SqlCommand(sqlStr, con);
     6         DataSet ds = new DataSet();
     7         SqlDataAdapter dapt = new SqlDataAdapter(cmd);
     8         dapt.Fill(ds);
     9         return ds;
    10     }
    其中ConfigurationManager.ConnectionStrings["conStr"].ConnectionString
    是为了方便把连接字符串写在了web.config文件里,也可以直接写连接字符串,以下是写在web.config里的代码
    要根据自己的服务器名称和数据库名称来修改
    1   <connectionStrings>
    2     <add name="conStr" connectionString="server=LAPTOP-O3CNDGCI;database=APONETDB;Trusted_Connection=true;" />
    3   </connectionStrings>

    以下是效果图:

     
  • 相关阅读:
    POJ 1088 滑雪
    POJ 2243 Knight Moves
    poj1847
    poj1995
    poj2230
    poj2007
    poj2376
    socket与TcpListener/TcpClient/UdpClient 的区别及联系
    利用DescriptionAttribute定义枚举值的描述信息
    可以关注的Android网上信息
  • 原文地址:https://www.cnblogs.com/cwgupup/p/12766566.html
Copyright © 2011-2022 走看看