连接数据库的配置文件
<connectionStrings>
<add name="scon" connectionString="server=.;uid=sa;pwd=sa;database=Stumanager;"/>
</connectionStrings>
sqlhelp类
string scon = System.Configuration.ConfigurationManager.ConnectionStrings["scon"].ConnectionString;
public DataSet Reds(string strsql)
{
using(SqlConnection conn=new SqlConnection(scon))
{
SqlDataAdapter sda = new SqlDataAdapter(strsql,conn);
DataSet ds = new DataSet();
sda.Fill(ds);
return ds;
}
}
/// <summary>
/// 执行曾删改操作
/// </summary>
public int Rei(string strsql)
{
using(SqlConnection conn=new SqlConnection(scon))
{
conn.Open();
SqlCommand comm = new SqlCommand(strsql,conn);
int i = comm.ExecuteNonQuery();
return i;
}
}
绑定数据到GridView客户端代码
<script>
function queding()
{
if(window.confirm("你确定要删除吗?")==false)
{
window.event.returnValue=false;
}
}
</script>
<asp:GridView ID="gv" runat="server" AutoGenerateColumns="False"
CellPadding="4" ForeColor="#333333" GridLines="None"
onrowdatabound="gv_RowDataBound">
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<EditRowStyle BackColor="#999999" />
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<Columns>
<asp:BoundField DataField="Sid" HeaderText="学号"/>
<asp:BoundField DataField="Sname" HeaderText="姓名"/>
<asp:BoundField DataField="Sage" HeaderText="年龄"/>
<%--绑定性别 --%>
<asp:TemplateField HeaderText="性别">
<ItemTemplate>
<asp:Label ID="lb" runat="server"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Saddress" HeaderText="地址" />
<asp:BoundField DataField="Stel" HeaderText="电话"/>
<asp:BoundField DataField="Semail" HeaderText="邮箱"/>
<%--绑定照片--%>
<asp:TemplateField HeaderText="照片">
<ItemTemplate>
<asp:Image ID="img" runat="server" Width="40" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="编辑操作">
<ItemTemplate>
<a href="Edit.aspx?id=<%#Eval("Sid") %>">编辑</a>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="删除操作">
<ItemTemplate>
<asp:CheckBox runat="server" ID="che" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:LinkButton runat="server" ID="linkbut" οnclick="linkbut_Click">删除</asp:LinkButton>
<a href="insertinto.aspx">添加</a>
c# 代码包括数据绑定
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
binds();
}
}
public void binds()
{
string strsql = "select * from student";
SqlHelp sh = new SqlHelp();
DataSet ds = sh.Reds(strsql);
this.gv.DataSource = ds.Tables[0].DefaultView;
gv.DataBind();
for (int i = 0; i < ds.Tables[0].Rows.Count;i++ )
{
Label lb = (Label)this.gv.Rows[i].Cells[3].FindControl("lb");
if (ds.Tables[0].Rows[i]["Ssex"].ToString() == "0")
{
lb.Text = "女";
}
else
{
lb.Text = "男";
}
Image img = (Image)this.gv.Rows[i].Cells[7].FindControl("img");
img.ImageUrl = "img/" + ds.Tables[0].Rows[i]["Sphoto"].ToString();
}
}
/// <summary>
/// 删除按钮事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void linkbut_Click(object sender, EventArgs e)
{
string ids = "(";
for (int i = 0; i < gv.Rows.Count;i++ )
{
CheckBox che=(CheckBox)gv.Rows[i].Cells[8].FindControl("che");
if(che.Checked==true)
{
string id = gv.Rows[i].Cells[0].Text;
ids = ids + id + ",";
}
}
ids = ids.Substring(0,ids.Length-1);
ids = ids + ")";
string strsql = "delete student where Sid in "+ids;
SqlHelp sh = new SqlHelp();
sh.Rei(strsql);
binds();
}
protected void gv_RowDataBound(object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType==DataControlRowType.DataRow||e.Row.RowType==DataControlRowType.Separator)
{
linkbut.Attributes.Add("onclick","queding();");
}
}
修改数据C#代码
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
files.Attributes.Add("onchange","huan(this);");
if(object.Equals(Request.QueryString["id"],null)==false)
{
string id = Request.QueryString["id"].ToString();
string strsql = "select * from student where Sid="+id;
SqlHelp sh = new SqlHelp();
DataSet ds = sh.Reds(strsql);
for (int i = 0; i < ds.Tables[0].Rows.Count;i++ )
{
txtname.Text = ds.Tables[0].Rows[i]["Sname"].ToString();
txtage.Text = ds.Tables[0].Rows[i]["Sage"].ToString();
dropsex.Text = ds.Tables[0].Rows[i]["Ssex"].ToString();
txtaddress.Text = ds.Tables[0].Rows[i]["Saddress"].ToString();
txttel.Text = ds.Tables[0].Rows[i]["Stel"].ToString();
txtemail.Text = ds.Tables[0].Rows[i]["Semail"].ToString();
img.ImageUrl = "img/"+ds.Tables[0].Rows[i]["Sphoto"].ToString();
}
}
}
}
//修改按钮点击事件
protected void libtn_Click(object sender, EventArgs e)
{
string id = Request.QueryString["id"].ToString();
if (files.FileName != "" && id != "")
{
string filename = files.FileName;
Random ra = new Random();
filename = ra.Next(100000000).ToString() + "_" + filename;
string path = Server.MapPath("img") + "//" + filename;
files.SaveAs(path);
string strsql = string.Format("update student set Sname='{0}',Sage={1},Ssex={2},Saddress='{3}',Stel='{4}',Semail='{5}',Sphoto='{6}' where Sid={7}", txtname.Text, txtage.Text, dropsex.SelectedItem.Value, txtaddress.Text, txttel.Text, txtemail.Text, filename, id);
SqlHelp sh = new SqlHelp();
sh.Rei(strsql);
Response.Write("<script>window.alert('修改成功');window.location='Default.aspx';</script>");
}
else
{
string strsql = string.Format("update student set Sname='{0}',Sage={1},Ssex={2},Saddress='{3}',Stel='{4}',Semail='{5}' where Sid={6}", txtname.Text, txtage.Text, dropsex.SelectedItem.Value, txtaddress.Text, txttel.Text, txtemail.Text, id);
SqlHelp sh = new SqlHelp();
sh.Rei(strsql);
Response.Write("<script>window.alert('修改成功');window.location='Default.aspx';</script>");
}
下面是修改页面的客户端代码
<head runat="server">
<title>无标题页</title>
<script>
function huan(obj)
{
document.getElementById("img").src=obj.value;
}
</script>
</head>
<body>
<form id="form1" runat="server">
姓名:<asp:TextBox ID="txtname" runat="server"></asp:TextBox>
<br />
年龄:<asp:TextBox ID="txtage" runat="server"></asp:TextBox>
<br />
性别:<asp:DropDownList ID="dropsex" runat="server">
<asp:ListItem Value="0">女</asp:ListItem>
<asp:ListItem Value="1">男</asp:ListItem>
</asp:DropDownList>
<br />
地址:<asp:TextBox ID="txtaddress" runat="server"></asp:TextBox>
<br />
电话:<asp:TextBox ID="txttel" runat="server"></asp:TextBox>
<br />
邮箱:<asp:TextBox ID="txtemail" runat="server"></asp:TextBox>
<br />
照片:<asp:FileUpload ID="files" runat="server" /><asp:Image ID="img"
runat="server" Height="130px" Width="119px" />
<br />
<asp:LinkButton ID="libtn" runat="server" οnclick="libtn_Click">确定修改</asp:LinkButton>
</form>
</body>