码迷,mamicode.com
首页 > 数据库 > 详细

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

时间:2016-07-21 14:40:09      阅读:227      评论:0      收藏:0      [点我收藏+]

标签:

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();

 

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

标签:

原文地址:http://www.cnblogs.com/yinsheng/p/5691437.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!