• 求亲篇:数据库操作,SqlHelper,增删改查


    1.利用SqlHelper类

    2.简单的数据绑定输出

    string strSql = "select * from login";
    DataTable dt = SqlHelper.ExecuteDataSetText(strSql, null).Tables[0];//查询,数据集第一个
    GridView1.DataSource = dt;//数据源
    GridView1.DataBind();//数据绑定
    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
                <Columns>
                    <asp:BoundField DataField="pwd" HeaderText="pwd" />
                    <asp:BoundField DataField="username" HeaderText="username" />
                    <asp:BoundField DataField="qq" HeaderText="qq" />
                    <asp:BoundField DataField="email" HeaderText="email" />
                    <asp:BoundField DataField="tel" HeaderText="tel" />
                </Columns>
    </asp:GridView>

    3.数据的查询

         public void BindRNew()
            {
                string strSql = GetSqlStr();
                DataTable dt = SqlHelper.ExecuteDataSetText(strSql, null).Tables[0];//查询,获取数据集第一个
                GridView1.DataSource = dt;//数据源
                GridView1.DataBind();//数据绑定
            }
            public string GetSqlStr()
            {
                StringBuilder sb = new StringBuilder();
                sb.Append("select * from login where 1=1");
                if(!string.IsNullOrEmpty(TextBox1.Text.Trim()))
                {
                    sb.Append(string.Format(" and pwd like '%{0}%'", TextBox1.Text.Trim()));
                }
                if (DropDownList1.SelectedIndex>0)
                {
                    sb.Append(string.Format(" and username = '{0}'", DropDownList1.SelectedValue));
                }
                return sb.ToString();
            }
    
            protected void Button1_Click(object sender, EventArgs e)
            {
                BindRNew();
            }

    3.数据的添加

    string s1 = TextBox2.Text.Trim();
                string s2 = TextBox3.Text.Trim();
                string s3 = TextBox4.Text.Trim();
                string s4 = TextBox5.Text.Trim();
                string s5 = DropDownList2.SelectedIndex > 0 ? DropDownList2.SelectedValue : "";
                string strSql = string.Format("insert into login(pwd,username,qq,email,tel) values('{0}','{1}','{2}','{3}','{4}')", s1,s2,s3,s4,s5);
                if(SqlHelper.ExecteNonQueryText(strSql)>0)
                {
                    Response.Write("添加成功");
                }
                BindRNew();//重新加载页面

    4.数据的删除

    if(!string.IsNullOrEmpty(TextBox6.Text.Trim()))
                {
                    string pwd1 = (TextBox6.Text.Trim());
                    string strSql = string.Format("delete login where pwd='{0}'", pwd1);
                    if(SqlHelper.ExecteNonQueryText(strSql)>0)//所有增删操作用它
                    {
                        Response.Write("删除成功!");
                    }
                }
                BindRNew();
    

    5.数据的更新,修改

    //判断是否本来存在
                if(!string.IsNullOrEmpty(TextBox7.Text.Trim()))
                {
                    string pwd = TextBox7.Text.Trim();
                    string strSql1 = string.Format("select pwd from login where pwd='{0}'", pwd);
                    if (SqlHelper.Exists(strSql1))
                    {
                        string qq = TextBox8.Text.Trim();
                        string strSql2 = string.Format("update login set qq='{0}' where pwd='{1}'", qq, pwd);
                        if (SqlHelper.ExecteNonQueryText(strSql2) > 0)//所有增删操作用它
                        {
                            Response.Write("更新成功!");
                        }
                    }
                    else
                    {
                        Response.Write("该ID在数据库里面不存在");
                    }
                }
                BindRNew();
  • 相关阅读:
    Restful、SOAP、RPC、SOA、微服务之间的区别
    SOA(面向服务的架构.)、RPC(远程过程调用)思想
    facade层,service 层,domain层,dao 层设计
    Mac下配置alias,zsh终端命令别名
    .bash_profile 和.zshrc
    什么是零担物流?零担物流的五大特点
    零担是什么意思,零担物流和快递有什么区别
    idea vm options
    idea中 VM options配置
    ES配置生命周期策略
  • 原文地址:https://www.cnblogs.com/yinsheng/p/5691437.html
Copyright © 2020-2023  润新知