非类型化的DataSet完成用户信息的增删改查操作

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Configuration;
using System.Data.SqlClient;

public partial class _Default : System.Web.UI.Page
{
    DataSet ds = new DataSet();
    protected void Page_Load(object sender, EventArgs e)
    {      
        string str = ConfigurationManager.ConnectionStrings["sqlcnn"].ConnectionString;
        using(SqlConnection sqlcnn=new SqlConnection(str))
        {
            SqlCommand sqlcmm = sqlcnn.CreateCommand();
            sqlcmm.CommandText = "select * from good";
            SqlDataAdapter adapter = new SqlDataAdapter(sqlcmm);
            sqlcnn.Open();
            adapter.Fill(ds);
            //增
            //DataRow row = ds.Tables[0].NewRow();
            //row["id"] = 11;
            //row["name"] = "联想Z470";
            //row["price"] = 4800;
            //row["picture"] = "images/联想Z470.jpg";
            //ds.Tables[0].Rows.Add(row);

            //删
            //ds.Tables[0].Rows[10].Delete();

            //改
            //ds.Tables[0].Rows[10]["name"] = "联想Y470";
            SqlCommandBuilder builder = new SqlCommandBuilder(adapter);
            adapter.Update(ds);
        }
        this.GridView1.DataSource = ds;
        this.GridView1.DataBind();
    }
    public void MsgBox(string msg)
    {
        string strScript;
        strScript = "<script language=javascrpt type=" + "text/javascript" + ">";
        strScript += "alert('" + msg + "');";
        strScript += "</script>";
        System.Web.HttpContext.Current.Response.Write(strScript);
    }
    protected void btn_cha_Click(object sender, EventArgs e)
    {    
        DataSet ds1 = new DataSet();
        string str = this.TextBox1.Text;
        string str1 = ConfigurationManager.ConnectionStrings["sqlcnn"].ConnectionString;
        if (TextBox1.Text != "")
        {
            using (SqlConnection sqlcon = new SqlConnection(str1))
            {
                SqlCommand sqlcmm = new SqlCommand();
                sqlcmm.Connection = sqlcon;
                sqlcmm.CommandText = "select * from good where name=@name";
                SqlParameter param = new SqlParameter("@name", str);
                sqlcmm.Parameters.Add(param);
                SqlDataAdapter sqldat = new SqlDataAdapter(sqlcmm);
                sqlcon.Open();
                sqldat.Fill(ds1);
                this.GridView1.DataSource = ds1;
                this.GridView1.DataBind();

                if (ds1.Tables[0].Rows.Count>0)
                {
                    SqlCommandBuilder build = new SqlCommandBuilder(sqldat);
                    sqldat.Update(ds1);
                }
                else
                {
                    MsgBox("您输入的产品名不存在,请重新输入!");
                }
              
            }
        }
        else
        {
            MsgBox("请输入要查询的产品名!");
        }
    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值