C#WebForm实现增删改查
上⽂,我们使⽤pb和c#WinForm使⽤分别实现了增删改查,本⽂,我们C#WebForm技术来实现⼀下。⾸先,说明⼀点,WinForm 和WebForm基本是⼀样,代码基本雷同,就是使⽤的控件不⼀样,页⾯排版上有点不同。数据库仍然采⽤sqlserver,表仍然采⽤
table_test,字段不说了,参考前两篇⽂章。
⾸先,在vs中创建⼀个ASP.NET空⽹站,test
其次,和上⽂⼀样,创建数据库操作类db.cs
web程序,会把类db.cs放在App_code⽂件夹中,代码如下:
using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
/// <summary>
///db 的摘要说明
/// </summary>
public class db
{
public static string connectionstring = "server=localhost;database=db;uid=sa;pwd=password";
public db()
{
//
/
/TODO: 在此处添加构造函数逻辑
//
}
public static SqlConnection OpenDB()
{
try
{
SqlConnection oConn = new SqlConnection(connectionstring);
oConn.Open();
return oConn;
}
catch
{
throw;
}
}
public static void CloseDB(SqlConnection oConn)
{
try
{
oConn.Close();
}
catch
{
throw;
}
}
public static DataSet Execute(string strCommandString)
{
SqlConnection oConn = OpenDB();
DataSet oDataSet = new DataSet();
SqlDataAdapter oDataAdapter = new SqlDataAdapter(strCommandString, oConn); oDataAdapter.Fill(oDataSet);
CloseDB(oConn);
return oDataSet;
}
catch
{
throw;
}
}
public static int ExecuteNonQuery(string strCommandString)
{
int li_count = 0;
SqlConnection oConn = null;
SqlCommand oComm = null;
try
{
oConn = new SqlConnection(connectionstring);
oComm = new SqlCommand();
oConn.Open();
oComm.Connection = oConn;
oComm.CommandText = strCommandString;
li_count = oComm.ExecuteNonQuery();
return li_count;
}
catch
{
return 0;
}
finally
{
if (oConn != null) oConn.Close();
}
}
public static int ExecuteReid(string strCommandString)
{
int li_count = 0;
SqlConnection oConn = null;
SqlCommand oComm = null;
try
{
oConn = new SqlConnection(connectionstring);
oComm = new SqlCommand();
oConn.Open();
oComm.Connection = oConn;
oComm.CommandText = strCommandString;
li_count = Convert.ToInt32(oComm.ExecuteScalar().ToString());
return li_count;
}
catch
{
return 0;
}
finally
{
if (oConn != null) oConn.Close();
}
第三步:排版test.Aspx页⾯
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="test.aspx.cs" Inherits="test" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="/1999/xhtml">
<head runat="server">
<title></title>
<link href="css/demo.css" type="text/css" rel="stylesheet">
</head>
<body>
<form id="form1" runat="server">
<div>
<table id="Table1" cellspacing="0" cellpadding="0" border="0" >
<tr>
<td valign="middle" nowrap align="left">
<span class="spannone">查询条件:</span>
<asp:TextBox ID="txt_cx" runat="server" Width="202px" CssClass="tbnone"></asp:TextBox>
<asp:Button ID="bt_cx" runat="server" CssClass="btnone" Text="查询" OnClick="bt_cx_Click">
</asp:Button>
</td>
<td valign="middle" nowrap align="right">
<asp:Button ID="bt_add" runat="server" CssClass="btnone" Text="增加" OnClick="bt_add_Click">
</asp:Button>
<asp:Button ID="bt_del" runat="server" CssClass="btnone" Text="删除" OnClick="bt_del_Click">
</asp:Button>
<asp:Button ID="bt_save" runat="server" CssClass="btnone" Text="保存" OnClick="bt_save_Click">
</asp:Button>
</td>
</tr>
</table>
<hr class="hr1">
AllowPaging="True" DataKeyNames="dbid" PageSize="5" OnPageIndexChanging="gv_1_PageIndexChanging" onselectedindexchanging="gv_1_S <HeaderStyle Wrap="False" CssClass="dghead" ForeColor="White" />
<RowStyle Wrap="False" CssClass="dgitem" />
<FooterStyle BackColor="White" ForeColor="#000066" />
<Columns>
<asp:TemplateField SortExpression="xm1" HeaderText="姓名">
<ItemTemplate>
<asp:Label ID="xm1" runat="server" Text='<%# Eval("xm1")%>' CssClass="tdleft"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField SortExpression="xm2" HeaderText="性别">
<ItemTemplate>
<asp:Label ID="xm2" runat="server" Text='<%# Eval("xm2")%>' CssClass="tdleft"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField SortExpression="xm3" HeaderText="电话">
<ItemTemplate>
<asp:Label ID="xm3" runat="server" Text='<%# Eval("xm3")%>' CssClass="tdleft"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderStyle Wrap="False" Width="15px" />
<ItemTemplate>
<asp:ImageButton ID="ImageButton1" runat="server" CausesValidation="False" CommandName="select"
ImageUrl="edit.gif" ToolTip="编辑" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
<AlternatingRowStyle CssClass="dgalteritem" />
</asp:GridView>
</div>
<table border="0" cellpadding="0" cellspacing="0" bordercolor="#111111" width="100%" class="px12">
<tr>
<td >
</td>
</tr>
<tr height="30">
<td width="3%" background="topbg.jpg" align=center><IMG height="16" src="right.GIF"></td>
<td background="topbg.jpg" width="80"><b>信息编辑</b></td>
<TD background="topbg.jpg" align="right">
text align center</TD>
</tr>
</table>
<div class="divhid">
<asp:textbox id="dbid" runat="server" cssclass="tbinput"></asp:textbox>
</div>
<div>
<table>
<tr>
<td colspan="2">
</td>
</tr>
<tr>
<td width="25%" height="20" class="tdlabel">
姓名:
</td>
<td width="75%">
<asp:TextBox ID="xm1" runat="server" CssClass="tbinput"></asp:TextBox>
</td>
<td>
</td>
</tr>
<tr>
性别:
</td>
<td width="75%" height="20">
<asp:RadioButtonList ID="xm2" runat="server" CssClass="tbinput" RepeatColumns="2" Width="100px">
<asp:ListItem Value="男" Selected="True">男</asp:ListItem>
<asp:ListItem Value="⼥">⼥</asp:ListItem>
</asp:RadioButtonList>
</td>
<td>
</td>
</tr>
<tr>
<td width="25%" height="20" class="tdlabel">
电话:
</td>
<td width="75%">
<asp:TextBox ID="xm3" runat="server" CssClass="tbinput"></asp:TextBox>
</td>
<td>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
第四步:配置好demo.css
.hr1
{
width: 100%;
height: 1px;
background-color: green;
}
.btnone
{
font-size: 9pt;
color: black;
font-family: 宋体, Arial;
text-align: center;
}
.tbinput
{
border-right: buttonface 1px solid;
border-top: buttonface 1px solid;
font-size: 9pt;
border-left: buttonface 1px solid;
width: 100%;
color: black;
border-bottom: buttonface 1px solid;
font-style: normal;
font-family: 宋体, Arial;
font-variant: normal;
text-decoration: none;
border-color: #93BEE2 #93BEE2 #93BEE2 #93BEE2 ;
border: 1px #93BEE2 solid;
}
.spanerror
{
font-size: 9pt;
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论