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

来源:互联网 发布:球幕融合软件 编辑:程序博客网 时间:2024/05/15 08:58
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();        }    }}

 
原创粉丝点击