求親篇:數據庫操作,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();

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM