用vs2022中的gridview控件连接sql数据库选择数

用vs2022中的gridview控件连接sql数据库选择数,第1张

利用Sql控件配置数据源连接数据库

1、使用GridView控件绑定该数据源。

2、添加计数器添加SQL的常用统计然后在下面列出的项目里面选择用户连接就可以时时查询到数据库的连接数了。

using System

using System Data

using System Data SqlClient

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

public partial class Demo : System Web UI Page

{

     protected void Page_Load(object sender EventArgs e)

     {

         if (Page IsPostBack == false)

         {

             BindData()

         }

     }

     public void BindData()

     {

         string strSql = select UserID C_Name E_Name QQ from Demo_User

         DataTable dt = SqlHelper ExecuteDataset(SqlHelper CONN_STRING CommandType Text strSql null) Tables[ ]

         GridView DataSource = dt

         GridView DataKeyNames = new string[] { UserID }//主键

         GridView DataBind()

     }

     protected void GridView_PageIndexChanging(object sender GridViewPageEventArgs e)

     {

         GridView PageIndex = e NewPageIndex

         BindData()

     }

     protected void GridView_RowEditing(object sender GridViewEditEventArgs e)

     {

         GridView EditIndex = e NewEditIndex

         BindData()

     }

     protected void GridView_RowCancelingEdit(object sender GridViewCancelEditEventArgs e)

     {

         GridView EditIndex =

         BindData()

     }

     protected void GridView_RowUpdating(object sender GridViewUpdateEventArgs e)

     {

         string strSql = Update Demo_User set QQ=@QQ where UserID=@UserID

         SqlParameter[] para = {

                                 new SqlParameter( @QQ ((TextBox)(GridView Rows[e RowIndex] Cells[ ] Controls[ ])) Text ToString() Trim())

                                 new SqlParameter( @UserID (int)GridView DataKeys[e RowIndex] Value)

                                }

         SqlHelper ExecuteNonQuery(SqlHelper CONN_STRING CommandType Text strSql para)

         GridView EditIndex =

         BindData()

     }

}

<table align= center bgcolor= #c de border= cellpadding= cellspacing= width= % >

         <tr>

             <th colspan= >

                 GridView演示</th>

         </tr>

        <tr>

            <td colspan= >

               <asp:GridView ID= GridView runat= server Width= % AutoGenerateColumns= False AllowPaging= True OnPageIndexChanging= GridView_PageIndexChanging PageSize=

               OnRowCancelingEdit= GridView_RowCancelingEdit OnRowEditing= GridView_RowEditing OnRowUpdating= GridView_RowUpdating   >

                 <Columns>

                       <asp:BoundField DataField= UserID HeaderText= UserID ReadOnly= True />

                       <asp:BoundField DataField= C_Name HeaderText= 中文名字 ReadOnly= True />

                       <asp:BoundField DataField= E_Name HeaderText= 英文名字 ReadOnly= True />

                       <asp:BoundField DataField= QQ HeaderText= QQ帐号 />

                     <asp:CommandField HeaderText= 编辑 ShowEditButton= True />

                   </Columns>

                   <RowStyle HorizontalAlign= Center />

                   <PagerStyle HorizontalAlign= Right />

               </asp:GridView>

            </td>

        </tr>

</table>

lishixinzhi/Article/program/net/201311/11647


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/sjk/6675696.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-03-26
下一篇 2023-03-26

发表评论

登录后才能评论

评论列表(0条)

保存