您的位置:首页 > 编程语言 > ASP

ASP.NET动态网站制作(22)-- ADO.NET(1)

2015-10-30 13:52 519 查看
前言:这节课开始真正地学习WEB开发,ADO.NET就是一组允许.NET开发人员使用标准的、机构化的,甚至无连接的方式与数据交互的技术。所属的类库为:System.Data.dll。

内容

  1.ADO.NET就是实现后台页面(C#)和数据库交互的技术。

  2.新建一个web项目,其中有一个Web.config文件,它储存一些公共信息,如数据库连接信息等。在项目中添加一个Web窗体,实际上它就是一个HTML页面,只不过是与后台页面关联在一起的HTML。一个Web窗体包含前台文件(.aspx)和后台文件(.aspx.cs)。

  3.有三种控件:服务端控件、客户端控件和自定义控件。服务端控件的特点是其属性里有一个runat="server"。

  4.string="";和string=null;是不一样的,区别在于string="";在堆里开辟了空间,而string=null;没有开辟空间。

  5.使用string.Format()向字符串中传值。

  6.using有两个作用:一是引入命名空间,二是释放资源。凡是继承于IDisposable接口的都要释放资源。

  7.一个应用例子:

  Web.config文件:

<?xml version="1.0" encoding="utf-8"?>
<!--
有关如何配置 ASP.NET 应用程序的详细信息,请访问 http://go.microsoft.com/fwlink/?LinkId=169433 -->
<configuration>
<connectionStrings>
<add name="sq_zoe" connectionString="Database=sq_zoe;Server=.;Integrated Security=false;Uid=sa;Password=123;"providerName="System.Data.SqlClient"/>
</connectionStrings>
<system.web>
<compilation debug="true" targetFramework="4.5" />
<httpRuntime targetFramework="4.5" />
</system.web>
</configuration>


  Login.aspx文件:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Login.aspx.cs" Inherits="ADO.NET1.Login" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table>
<tr><td>用户名:</td><td>
<asp:TextBox ID="txtUserName" runat="server"></asp:TextBox></td></tr>
<tr><td>密码:</td><td>
<asp:TextBox ID="txtPwd" runat="server" TextMode="Password"></asp:TextBox></td></tr>
<tr><td>
<asp:Button ID="btnClear" runat="server" Text="清除" OnClick="btnClear_Click" /></td><td>
<asp:Button ID="btnLogin" runat="server" Text="登录" OnClick="btnLogin_Click" /></td></tr>
</table>
</div>
</form>
</body>
</html>


  Login.aspx.cs文件:

using System;
using System.Configuration;
using System.Data.SqlClient;
using System.Web.UI;

namespace ADO.NET1
{
public partial class Login : Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
/// <summary>
/// 清除
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void btnClear_Click(object sender, EventArgs e)
{
txtUserName.Text = "";
txtPwd.Text = "";
}
/// <summary>
/// 登录
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void btnLogin_Click(object sender, EventArgs e)
{
string username = txtUserName.Text.Trim();
string pwd = txtPwd.Text.Trim();
if (string.IsNullOrEmpty(username) || string.IsNullOrEmpty(pwd))
{
Response.Write("<script>alert('用户名或者密码不能为空');</script>");
}
else
{
string strCon=ConfigurationManager.ConnectionStrings["sq_zoe"].ToString();//电话号码
using (SqlConnection con = new SqlConnection(strCon))//电话
{
con.Open();//和数据库建立起了连接
string strSql = string.Format("select UserId from UserInfor where UserName='{0}' and Pwd='{1}'", username, pwd);
SqlCommand cmd = new SqlCommand(strSql, con);//执行sql语句
using (SqlDataReader read = cmd.ExecuteReader())//执行sql语句,将得到的结果赋值给read
{
if (read.HasRows)
{
//Response.Write("<script>alert('登录成功');</script>");
Response.Redirect("RNewsM.aspx");
}
else
{
Response.Write("<script>alert('用户名或者密码错误');</script>");
}
}
}
//read.Close();
//read.Dispose();
//con.Close();
//con.Dispose();
}
}
}
}


View Code
  第一个Web算是做好了。

后记:得多多练习。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: