类型化的数据集和非类型化数据完成增删改查的操作

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

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
              
    }

    private void nosdata(string name,string price,string photo)
    {
        DataSet sd = new DataSet();
        DataTable table = new DataTable();
        string sqlcon = ConfigurationManager.ConnectionStrings["con"].ConnectionString;
        using (SqlConnection con = new SqlConnection(sqlcon))
        {
            using (SqlCommand cmd = new SqlCommand("select * from pros", con))
            {
                SqlDataAdapter myreader = new SqlDataAdapter(cmd);
                myreader.Fill(sd);
                if (RadioButton1.Checked)
                {
                    if (name!=""&& price!=""&& photo!="")
                    {
                        DataRow row = sd.Tables[0].NewRow();
                        row["name"] = name;
                        row["price"] = price;
                        row["photoPath"] = photo;
                        sd.Tables[0].Rows.Add(row);
                    }
                    else
                    {

                        Label1.Text = "其中一项不能为空";
                    }
      
                    //这里是添加记录
                }
                else if (RadioButton3.Checked)
                {
                    DataRow[] s=sd.Tables[0].Select("name ='"+name+"'");
                    foreach (var item in s)
                    {
                        item["price"] = price;
                        item["photoPath"] = photo;
                    }
                    //这里是修改记录

                }
                else if (RadioButton2.Checked)
                {
                    DataRow[] s = sd.Tables[0].Select("name ='" + name + "'");
                    foreach (var item in s)
                    {
                        item.Delete();
                    }   //这里是删除记录
                }

                else if (RadioButton4.Checked)
                {
                   ///这里上查找用DataTable 
                    
                    DataColumn co1 = new DataColumn("id", Type.GetType("System.Int32"));
                    co1.AutoIncrement = true;
                    co1.AutoIncrementSeed = 1;
                    co1.AutoIncrementStep = 1;
                    table.Columns.Add(co1);
                    DataColumn co2 = new DataColumn("name", Type.GetType("System.String"));
                    table.Columns.Add(co2);
                    DataColumn co3 = new DataColumn("price", Type.GetType("System.String"));
                    table.Columns.Add(co3);
                    DataColumn co4 = new DataColumn("photoPath", Type.GetType("System.String"));
                    table.Columns.Add(co4);

                    DataRow[] s = sd.Tables[0].Select("name ='" + name + "'");
                    foreach (var item in s)
                    {
                        DataRow row = table.NewRow();
                        row["name"] = item["name"];
                        row["price"] = item["price"];
                        row["photoPath"] = item["photoPath"];
                        table.Rows.Add(row);
                    }    //这里是查找记录
                    ////Response.Write (myreader.ToString());
                }


                 if (RadioButton1.Checked || RadioButton3.Checked || RadioButton2.Checked)
                 {
                     SqlCommandBuilder builder = new SqlCommandBuilder(myreader);
                     myreader.Update(sd);
                     GridView1.DataSource = sd;
                     GridView1.DataBind();
                 }
                 else
                 {
                     GridView1.DataSource = table;
                     GridView1.DataBind();
                 
                 }

            }
        }
    }


    protected void Button1_Click(object sender, EventArgs e)
    {
        string name = txtname.Text;
        string price = txtprice.Text;
        string photo = txtphotomarp.Text;
        nosdata(name,price,photo);   //录入数据
      //  nosdata(name,price,photo);   //删除数据


    }


    protected void Button2_Click(object sender, EventArgs e)
    {
        string name=txtname.Text;
        string price=txtprice.Text;
        string photo = txtphotomarp.Text;
        yesdata(name,price,photo);

    }

    private void yesdata(string name,string price,string photo)
    {
        DataTable table = new DataTable();
        prosTableAdapter dt = new prosTableAdapter();
        DataSet1.prosDataTable ds = dt.GetData();
        dt.Fill(ds);
        if (RadioButton1.Checked)
        {
            if (name != "" && price != "" && photo != "")
            {
                DataSet1.prosRow row = ds.NewprosRow();
                row["name"] = name;
                row["price"] = price;
                row["photoPath"] = photo;
                ds.AddprosRow(row);    //这里是添加记录
            }
            else
            {

                Label1.Text = "其中一项不能为空间";
            }

        }
        else if (RadioButton3.Checked)
        {
            DataRow[] row = ds.Select("name='" + name + "'");
            foreach (var item in row)
            {
                item["name"] = name;
                item["price"] = price;
                item["photoPath"] = photo;
            }

        }
        else if (RadioButton2.Checked)
        {
            DataRow[] row = ds.Select("name='" + name + "'");
            foreach (var item in row)
            {
                item.Delete();  //这里是删除数据
            }
        }
        else if (RadioButton4.Checked)
        {

            DataColumn co1 = new DataColumn("id", Type.GetType("System.Int32"));
            co1.AutoIncrement = true;
            co1.AutoIncrementSeed = 1;
            co1.AutoIncrementStep = 1;
            table.Columns.Add(co1);
            DataColumn co2 = new DataColumn("name", Type.GetType("System.String"));
            table.Columns.Add(co2);
            DataColumn co3 = new DataColumn("price", Type.GetType("System.String"));
            table.Columns.Add(co3);
            DataColumn co4 = new DataColumn("photoPath", Type.GetType("System.String"));
            table.Columns.Add(co4);

            DataRow[] s = ds.Select("name='" + name + "'");
            foreach (var item in s)
            {
                DataRow row = table.NewRow();
                row["name"] = item["name"];
                row["price"] = item["price"];
                row["photoPath"] = item["photoPath"];
                table.Rows.Add(row);
            }    //这里是查找记录




        }

        if (RadioButton1.Checked || RadioButton3.Checked || RadioButton2.Checked)
        {

            dt.Update(ds);
            GridView1.DataSource = ds;
            GridView1.DataBind();
        }
        else
        {
            GridView1.DataSource = table;
            GridView1.DataBind();

        }
    }
}

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

类型化数据集应用实例

2009年02月01日 85KB 下载

使用类型化数据集访问数据信息

2007年12月14日 133KB 下载

数据集的使用方法和技巧

2008年01月01日 199KB 下载

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

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