<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server">
</asp:GridView>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button4" runat="server" Text="查找" οnclick="Button4_Click" />
<asp:Button ID="Button1" runat="server" Text="更改" οnclick="Button1_Click" />
<asp:Button ID="Button3" runat="server" Text="删除" οnclick="Button3_Click" />
<asp:Label ID="Label1" runat="server"></asp:Label>
<br />
name:
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox><br />
price: <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox><br />
photoPath:<asp:TextBox ID="TextBox4" runat="server"></asp:TextBox>
<asp:Button ID="Button2" runat="server" Text="添加" οnclick="Button2_Click" />
<br />
<br />
<br />
</div>
</form>
</body>
</html>
这是HTML页面的代码
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
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
DataSet ds = new DataSet();
string strcnn = ConfigurationManager.ConnectionStrings["cnnstring"].ConnectionString;
using (SqlConnection sqlCnn = new SqlConnection(strcnn))
{
SqlCommand sqlCmm = sqlCnn.CreateCommand();
sqlCmm.CommandText = "Select * from pros";
SqlDataAdapter sqlDA = new SqlDataAdapter(sqlCmm);
sqlCnn.Open();
sqlDA.Fill(ds);
ds.Tables[0].Rows[0]["name"] = TextBox1.Text;
SqlCommandBuilder builder = new SqlCommandBuilder(sqlDA);
sqlDA.Update(ds);
this.GridView1.DataSource = ds;
this.GridView1.DataBind();
}
}
protected void Button2_Click(object sender, EventArgs e)
{
DataSet ds1 = new DataSet();
string strcnn = ConfigurationManager.ConnectionStrings["cnnstring"].ConnectionString;
using (SqlConnection sqlCnn = new SqlConnection(strcnn))
{
SqlCommand sqlCmm = sqlCnn.CreateCommand();
sqlCmm.CommandText = "Select * from pros";
SqlDataAdapter sqlDA = new SqlDataAdapter(sqlCmm);
sqlCnn.Open();
sqlDA.Fill(ds1);
DataRow dr = ds1.Tables[0].NewRow();
dr["name"] = TextBox2.Text;
dr["price"] = TextBox3.Text;
dr["photoPath"] = TextBox4.Text;
ds1.Tables[0].Rows.Add(dr);
SqlCommandBuilder builder = new SqlCommandBuilder(sqlDA);
sqlDA.Update(ds1);
this.GridView1.DataSource = ds1;
this.GridView1.DataBind();
}
}
protected void Button3_Click(object sender, EventArgs e)
{
DataSet ds2 = new DataSet();
string strcnn = ConfigurationManager.ConnectionStrings["cnnstring"].ConnectionString;
using (SqlConnection sqlCnn = new SqlConnection(strcnn))
{
SqlCommand sqlCmm = sqlCnn.CreateCommand();
sqlCmm.CommandText = "Select * from pros";
SqlDataAdapter sqlDA = new SqlDataAdapter(sqlCmm);
sqlCnn.Open();
sqlDA.Fill(ds2);
DataRow[] row = ds2.Tables[0].Select("name='"+TextBox1.Text+"'");
foreach (var item in row)
{
item.Delete();
}
SqlCommandBuilder builder = new SqlCommandBuilder(sqlDA);
sqlDA.Update(ds2);
this.GridView1.DataSource = ds2;
this.GridView1.DataBind();
}
}
protected void Button4_Click(object sender, EventArgs e)
{
DataSet ds3 = new DataSet();
string strcnn = ConfigurationManager.ConnectionStrings["cnnstring"].ConnectionString;
using (SqlConnection sqlCnn = new SqlConnection(strcnn))
{
SqlCommand sqlCmm = sqlCnn.CreateCommand();
sqlCmm.CommandText = "Select * from pros where name='"+TextBox1.Text+"'";
SqlDataAdapter sqlDA = new SqlDataAdapter(sqlCmm);
Response.Write(sqlDA.ToString());
sqlCnn.Open();
sqlDA.Fill(ds3);
SqlCommandBuilder builder = new SqlCommandBuilder(sqlDA);
sqlDA.Update(ds3);
this.GridView1.DataSource = ds3;
this.GridView1.DataBind();
}
}
}
这是.aspx的代码 能够简实现增删改查