偶滴点NET复习
☆☆☆☆☆:三颗星必须死背
☆:一颗星的看看
实验一
侧边栏表格☆
<a href="#"><img src="Content/page6/logo.png" </a>
<table border="0" class="auto-style13">
<tr>
<th scope="row"><img src="Content/page6/logo1.png" /></th>
<td>母婴</td>
<td>玩具</td>
<td>童装童鞋</td>
</tr>
</table>
导航栏☆
<div class="nav" style="z-index:3;position:relative">
<!-- 导航菜单 -->
<ul>
<li> <li><a href="#">首页</a>
<ul class="nav_2">
<li><a href="#">超市</a></li>
<li><a href="#">个人主页</a></li>
<li><a href="#">导航页</a></li>
</ul>
</li>
</ul>
</div>
实验二
首页表单☆☆☆
index.aspx
<form action="update.aspx">
username<input name="username" />
password<input name="password" />
<input type="submit" value="数据更新"/></form>
<form action="manage.aspx"><input type="submit" value="数据管理"/><br/></form>
ASP组件☆
update.aspx
<form id="form1" runat="server">
<div>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
<asp:TextBox ID="TextBox1" runat="server" OnTextChanged="TextBox1_TextChanged"></asp:TextBox>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />
</div>
</form>
update.aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
Label1.Text = "更新的用户名是:" + Request["username"];
TextBox1.Text = "更新的密码是:" + Request["password"];
Button1.Text = "执行更新";
}
protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection cn = new SqlConnection("Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename=F:\\VS2019-source\\hellonet\\ASPNET\\mydb2.mdf;Integrated Security=True;Connect Timeout=30");
cn.Open();
SqlTransaction ut = null;
int result = 0;
String username = Request["username"];
String password = Request["password"];
String sql = "update Member set password = '" + password + "' where username = '" + username + "'";
try
{
ut = cn.BeginTransaction(IsolationLevel.ReadCommitted);
SqlCommand comm = new SqlCommand(sql,cn,ut);
comm.ExecuteNonQuery();
ut.Commit();
}catch(Exception e1) { ut.Rollback(); Response.Write(e1.ToString()); }
}
manage.aspx ☆☆☆
<body>
<form id="form1" runat="server">
<div>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" OnSelecting="SqlDataSource1_Selecting" ConnectionString="<%$ ConnectionStrings:mydb2ConnectionString %>" DeleteCommand="DELETE FROM [Member] WHERE [username] = @username" InsertCommand="INSERT INTO [Member] ([username], [password], [email]) VALUES (@username, @password, @email)" SelectCommand="SELECT * FROM [Member]" UpdateCommand="UPDATE [Member] SET [password] = @password, [email] = @email WHERE [username] = @username" ProviderName="System.Data.SqlClient">
<DeleteParameters>
<asp:Parameter Name="username" Type="String" />
</DeleteParameters>
<InsertParameters>
<asp:Parameter Name="username" Type="String" />
<asp:Parameter Name="password" Type="String" />
<asp:Parameter Name="email" Type="String" />
</InsertParameters>
<UpdateParameters>
<asp:Parameter Name="password" Type="String" />
<asp:Parameter Name="email" Type="String" />
<asp:Parameter Name="username" Type="String" />
</UpdateParameters>
</asp:SqlDataSource>
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" BackColor="White" BorderColor="#E7E7FF" BorderStyle="None" BorderWidth="1px" CellPadding="3" DataKeyNames="username" DataSourceID="SqlDataSource1" GridLines="Horizontal">
<AlternatingRowStyle BackColor="#F7F7F7" />
<Columns>
<asp:CommandField ShowDeleteButton="True" ShowEditButton="True" ShowSelectButton="True" />
<asp:BoundField DataField="username" HeaderText="username" ReadOnly="True" SortExpression="username" />
<asp:BoundField DataField="password" HeaderText="password" SortExpression="password" />
<asp:BoundField DataField="email" HeaderText="email" SortExpression="email" />
</Columns>
<FooterStyle BackColor="#B5C7DE" ForeColor="#4A3C8C" />
<HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#F7F7F7" />
<PagerStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" HorizontalAlign="Right" />
<RowStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" />
<SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="#F7F7F7" />
<SortedAscendingCellStyle BackColor="#F4F4FD" />
<SortedAscendingHeaderStyle BackColor="#5A4C9D" />
<SortedDescendingCellStyle BackColor="#D8D8F0" />
<SortedDescendingHeaderStyle BackColor="#3E3277" />
</asp:GridView>
<asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False" BackColor="White" BorderColor="#E7E7FF" BorderStyle="None" BorderWidth="1px" CellPadding="3" DataKeyNames="username" DataSourceID="SqlDataSource1" GridLines="Horizontal" Height="50px" Width="125px">
<AlternatingRowStyle BackColor="#F7F7F7" />
<EditRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="#F7F7F7" />
<Fields>
<asp:BoundField DataField="username" HeaderText="username" ReadOnly="True" SortExpression="username" />
<asp:BoundField DataField="password" HeaderText="password" SortExpression="password" />
<asp:BoundField DataField="email" HeaderText="email" SortExpression="email" />
<asp:CommandField ShowInsertButton="True" />
</Fields>
<FooterStyle BackColor="#B5C7DE" ForeColor="#4A3C8C" />
<HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#F7F7F7" />
<PagerStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" HorizontalAlign="Right" />
<RowStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" />
</asp:DetailsView>
</div>
</form>
</body>
实验三
首页表单
index.aspx ☆☆☆
<form action="servicecall.aspx"><input name="searchs" /><input type="submit" value="搜索" /></form>s
servicecall.aspx
<form id="form1" runat="server">
<div>
<asp:TextBox ID="TextBox1" runat="server" Height="300px" OnTextChanged="TextBox1_TextChanged" Width="478px" TextMode="MultiLine"></asp:TextBox>
</div>
</form>
servicecall.aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
localhost.WebService1 myservice = new localhost.WebService1();
string str = "";
string result = myservice.myfindbystr(Request["searchs"]);
for (int i = 0; i < result.Length; i++)
{
str = str + result[i] + "第" + i + "行";
}
TextBox1.Text = result;
}
WebService
WebService1.asmx
[WebMethod]
public string myfindbystr(string username)
{
DataSet ds = new DataSet();
SqlConnection cn = new SqlConnection("Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename=F:\\VS2019-source\\hellonet\\ASPNET\\mydb3.mdf;Integrated Security=True;Connect Timeout=30");
cn.Open();
SqlDataAdapter da = new SqlDataAdapter("select * from Member", cn);
da.Fill(ds);
cn.Close();
string result = "";
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
//String str = ds.Tables[0].Rows[i][1].ToString() + ds.Tables[0].Rows[i][2].ToString();
string str = ds.Tables[0].Rows[i][0].ToString();
if (str.Contains(username))
{
result = result + ds.Tables[0].Rows[i][0].ToString() + " " + ds.Tables[0].Rows[i][2].ToString() + "\n";
}
}
return result;
}
实验四
模型 ☆☆☆
Models.Member.cs
public class Member
{
public String username { get; set; }
public String password { get; set; }
public String email { get; set; }
public Member() { }
public Member(String username,String password,String email) { this.username = username; this.password = password; this.email = email; }
}
控制器 ☆☆☆
UserController.cs
namespace Webmvc.Controllers
{
public class UserController : Controller
{
//获取所有数据集
private DataSet dbcontext()
{
localhost.WebService1 webservice = new localhost.WebService1();
return webservice.findall();
}
// GET: User
public ActionResult Index()
{
DataSet ds = dbcontext();
List<Member> getdata = new List<Member>();
for(int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
Member newone = new Member(ds.Tables[0].Rows[i][0].ToString(), ds.Tables[0].Rows[i][1].ToString(), ds.Tables[0].Rows[i][2].ToString());
getdata.Add(newone);
}
return View(getdata);
}
// GET: User/Details/5
public ActionResult Details(int id)
{
return View();
}
// GET: User/Create
public ActionResult Create()
{
Member newone = new Member();
return View(newone);
}
// POST: User/Create
[HttpPost]
public ActionResult Createp(Member crone)
{
//创建数据
DataSet ds = dbcontext();
ds.Tables[0].Rows[0][0] = crone.username;
ds.Tables[0].Rows[0][1] = crone.password;
ds.Tables[0].Rows[0][2] = crone.email;
localhost.WebService1 mydao = new localhost.WebService1();
mydao.persist(ds);
//重新获取数据
DataSet ds1 = dbcontext();
List<Member> getdata = new List<Member>();
for (int i = 0; i < ds1.Tables[0].Rows.Count; i++)
{
Member newone = new Member(ds1.Tables[0].Rows[i][0].ToString(), ds1.Tables[0].Rows[i][1].ToString(), ds1.Tables[0].Rows[i][2].ToString());
getdata.Add(newone);
}
return View(getdata);
}
// GET: User/Edit/5
public ActionResult Edit(String username)
{
Member updateins = new Member();
return View(updateins);
}
// POST: User/Edit/5
[HttpPost]
public ActionResult Editp(Member updateins)
{
try
{
DataSet ds = dbcontext();
ds.Tables[0].Rows[0][0] = updateins.username;
ds.Tables[0].Rows[0][1] = updateins.password;
ds.Tables[0].Rows[0][2] = updateins.email;
localhost.WebService1 mydao = new localhost.WebService1();
mydao.edit(ds);
return RedirectToAction("Index");
}
catch
{
DataSet ds1 = dbcontext();
List<Member> getdata = new List<Member>();
for (int i = 0; i < ds1.Tables[0].Rows.Count; i++)
{
Member newone = new Member(ds1.Tables[0].Rows[i][0].ToString(), ds1.Tables[0].Rows[i][1].ToString(), ds1.Tables[0].Rows[i][2].ToString());
getdata.Add(newone);
}
return View(getdata);
}
}
// GET: User/Delete/5
public ActionResult Delete(String username)
{
localhost.WebService1 mydao = new localhost.WebService1();
int result = mydao.remove1(username);
ViewBag.retu = result;
return View();
}
// POST: User/Delete/5
[HttpPost]
public ActionResult Delete(int id, FormCollection collection)
{
try
{
// TODO: Add delete logic here
return RedirectToAction("Index");
}
catch
{
return View();
}
}
}
视图
感觉不大会用
WebService
[WebMethod]
public int save()
{
int result1 = 0;
DataSet ds = new DataSet();
SqlConnection cn = new SqlConnection("Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename=F:\\VS2019-source\\hellonet\\ASPNET\\mydb3.mdf;Integrated Security=True;Connect Timeout=30");
try
{
cn.Open();
}
catch (Exception e1) { }
SqlDataAdapter da = new SqlDataAdapter("select * from Member", cn);
try
{
da.Fill(ds);
}
catch (Exception e2) { }
if (ds == null) { }
//cn.Close();
List<String> result = new List<String>();
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
String str = ds.Tables[0].Rows[i][0].ToString();
if (str.Equals("123"))
{
SqlTransaction ut = null;
String sql = "insert into Member values('" + ds.Tables[0].Rows[i][0] + "','" + ds.Tables[0].Rows[i][1] + "','" + ds.Tables[0].Rows[i][2] + ")";
try
{
ut = cn.BeginTransaction(IsolationLevel.ReadCommitted);
SqlCommand comm = new SqlCommand(sql, cn, ut);
result1 = comm.ExecuteNonQuery();
ut.Commit();
}
catch (Exception e1) { ut.Rollback(); Console.Write(e1.ToString()); }
}
}
cn.Close();
return result1;
}
[WebMethod]
public int remove(String username)
{
int result1 = 0;
DataSet ds = new DataSet();
SqlConnection cn = new SqlConnection("Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename=F:\\VS2019-source\\hellonet\\ASPNET\\mydb3.mdf;Integrated Security=True;Connect Timeout=30");
try
{
cn.Open();
}
catch (Exception e1) { }
SqlDataAdapter da = new SqlDataAdapter("select * from Member", cn);
try
{
da.Fill(ds);
}
catch (Exception e2) { }
if (ds == null) { }
//cn.Close();
List<String> result = new List<String>();
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
String str = ds.Tables[0].Rows[i][0].ToString();
if (str.Contains(username))
{
SqlTransaction ut = null;
//String sql = "delete from Member where username = '"+ ds.Tables[0].Rows[i][0] + "'";
String sql = "delete from Member where username = '" + ds.Tables[0].Rows[i][0] + "'";
try
{
ut = cn.BeginTransaction(IsolationLevel.ReadCommitted);
SqlCommand comm = new SqlCommand(sql, cn, ut);
result1 = comm.ExecuteNonQuery();
ut.Commit();
return result1;
}
catch (Exception e1) { ut.Rollback(); Console.Write(e1.ToString()); }
}
}
cn.Close();
return result1;
}
//查找所有
[WebMethod]
public DataSet findall()
{
DataSet ds = new DataSet();
SqlConnection cn = new SqlConnection("Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename=F:\\VS2019-source\\hellonet\\ASPNET\\mydb3.mdf;Integrated Security=True;Connect Timeout=30");
try
{
cn.Open();
}
catch (Exception e1) { }
SqlDataAdapter da = new SqlDataAdapter("select * from Member", cn);
try
{
da.Fill(ds);
}
catch (Exception e2) { }
return ds;
}
//修改
[WebMethod]
public int edit(DataSet updateins)
{
//连接
DataSet ds = new DataSet();
SqlConnection cn = new SqlConnection("Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename=F:\\VS2019-source\\hellonet\\ASPNET\\mydb3.mdf;Integrated Security=True;Connect Timeout=30");
try
{
cn.Open();
}
catch (Exception e1) { }
//编辑
SqlTransaction ut = null;
int result1 = 0;
String username = updateins.Tables[0].Rows[0][0].ToString();
String password = updateins.Tables[0].Rows[0][1].ToString();
String email = updateins.Tables[0].Rows[0][2].ToString();
String sql = "update Member set password = " + password + ",email = " + email + " where username = " + username;
try
{
ut = cn.BeginTransaction(IsolationLevel.ReadCommitted);
SqlCommand comm = new SqlCommand(sql, cn, ut);
result1 = comm.ExecuteNonQuery();
ut.Commit();
}
catch (Exception e1) { ut.Rollback(); Console.Write(e1.ToString()); }
return result1;
}
[WebMethod]
public int persist(DataSet ds)
{
SqlTransaction ut = null;
int result1 = 0;
SqlConnection cn = new SqlConnection("Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename=F:\\VS2019-source\\hellonet\\ASPNET\\mydb3.mdf;Integrated Security=True;Connect Timeout=30");
cn.Open();
String sql = "insert into Member values('"+ ds.Tables[0].Rows[0][0] + "','" + ds.Tables[0].Rows[0][1] + "','" + ds.Tables[0].Rows[0][2] + "')";
try
{
ut = cn.BeginTransaction(IsolationLevel.ReadCommitted);
SqlCommand comm = new SqlCommand(sql, cn, ut);
result1 = comm.ExecuteNonQuery();
ut.Commit();
}
catch (Exception e1) { ut.Rollback(); Console.Write(e1.ToString()); }
return result1;
}
[WebMethod]
public int remove1(String username)
{
int result1 = 0;
DataSet ds = new DataSet();
SqlConnection cn = new SqlConnection("Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename=F:\\VS2019-source\\hellonet\\ASPNET\\mydb3.mdf;Integrated Security=True;Connect Timeout=30");
try
{
cn.Open();
}
catch (Exception e1) { }
SqlTransaction ut = null;
//String sql = "delete from Member where username = '"+ ds.Tables[0].Rows[i][0] + "'";
String sql = "delete from Member where username = '" + username + "'";
try
{
ut = cn.BeginTransaction(IsolationLevel.ReadCommitted);
SqlCommand comm = new SqlCommand(sql, cn, ut);
result1 = comm.ExecuteNonQuery();
ut.Commit();
return result1;
}
catch (Exception e1) { ut.Rollback(); Console.Write(e1.ToString()); }
cn.Close();
return result1;
}