非类型化的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("请输入要查询的产品名!");
        }
    }
}

阅读更多
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭