使用VS2013与内置数据库进行连接
来源:互联网 发布:次声波软件 编辑:程序博客网 时间:2024/05/22 01:31
一、页面的布置
1、首先需要将页面做好(本例的工程名为demo,框架用的是.NET Framework 4.5)。示例页面代码如下:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="demo.Default" %><!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"> 欢迎来到校园活动预约系统 <table> <tr><td>用户名:</td><td><asp:TextBox ID="username" runat="server"></asp:TextBox></td></tr> <tr><td>密码:</td><td><asp:TextBox ID="userpwd" runat="server" TextMode="Password"></asp:TextBox></td></tr> <tr><td>身份:</td><td><asp:DropDownList ID="DropDownListLevel" runat="server"></asp:DropDownList></td></tr> <tr><td colspan="2"><asp:Button ID="ButtonOK" runat="server" Text="登 录" OnClick="ButtonOK_Click" /></td></tr> <tr><td colspan="2"><asp:Label ID="LabelLoginInf" runat="server" Font-Bold="True" ForeColor="Red"></asp:Label></td></tr> </table> </form></body></html>
2、本例是登录测试,首先查询数据库用户名、密码、用户类型是否一一对应。如果对应,提示登录成功,并转入相应页面;如果不对应,则提示用户名或密码错误,请重新输入。
二、引用数据库类代码
1、这里选用BaseClass.cs文件作为数据库操作类,也可选用网上常用的SQLHelper类文件。将文件放入App_Code文件夹中,没有的话新建一个。示例代码如下所示:
using System;using System.Data;using System.Configuration;using System.Linq;using System.Web;using System.Web.Security;using System.Web.UI;using System.Web.UI.HtmlControls;using System.Web.UI.WebControls;using System.Web.UI.WebControls.WebParts;using System.Xml.Linq;using System.Data.SqlClient;/// <summary>///BaseClass 的摘要说明/// </summary>namespace GROUP.Manage{ public class BaseClass:System.Web.UI.Page { String strConn; //构造函数 public BaseClass() { // //TODO: 在此处添加构造函数逻辑 // strConn = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString; } //从数据库中读取数据,返回DataTable对象 public DataTable ReadTable(String strSql) { DataTable dt = new DataTable(); SqlConnection Conn = new SqlConnection(strConn); Conn.Open(); SqlDataAdapter Cmd = new SqlDataAdapter(strSql, Conn); Cmd.Fill(dt); Conn.Close(); return dt; } //从数据库中读取数据,返回DataSet对象 public DataSet ReadDataSet(String strSql) { DataSet ds = new DataSet(); SqlConnection Conn = new SqlConnection(strConn); Conn.Open(); SqlDataAdapter Cmd = new SqlDataAdapter(strSql, Conn); Cmd.Fill(ds); Conn.Close(); return ds; } //从数据库中读取数据,返回DataSet对象(需要表名) public DataSet GetDataSet(String strSql,String tableName) { DataSet ds = new DataSet(); SqlConnection Conn = new SqlConnection(strConn); Conn.Open(); SqlDataAdapter Cmd = new SqlDataAdapter(strSql, Conn); Cmd.Fill(ds,tableName); Conn.Close(); return ds; } //执行SQL查询,并返回一个Reader对象 public SqlDataReader readrow(String sql) { SqlConnection Conn = new SqlConnection(strConn); Conn.Open(); SqlCommand Comm = new SqlCommand(sql, Conn); SqlDataReader Reader = Comm.ExecuteReader(); if (Reader.Read()) { Comm.Dispose(); return Reader; } else { Comm.Dispose(); return null; } } //返回查询结果第一行某一个字段的值 public string ReadStr(String strSql, int flag) { DataSet ds = new DataSet(); String str; SqlConnection Conn = new SqlConnection(strConn); Conn.Open(); SqlDataAdapter Cmd = new SqlDataAdapter(strSql, Conn); Cmd.Fill(ds); str = ds.Tables[0].Rows[0].ItemArray[flag].ToString(); Conn.Close(); return str; } //执行SQL更新语句 public void execsql(String strSql) { SqlConnection Conn = new SqlConnection(strConn); SqlCommand Comm = new SqlCommand(strSql,Conn); Conn.Open(); Comm.ExecuteNonQuery(); Conn.Close(); } }}
2、主要使用的方法有两个,一个是ReadTable方法(此方法是从数据库中读取相应的查询语句组成的表,即查询操作),另一个是execsql方法(此方法用于插入、更新、删除语句)。
三、设计数据库
(1)首先从视图菜单中打开SQL Server对象资源管理器。
(2)单击SQL Server,将下面的数据库打开,在“数据库”项下新建一个数据库作为本例使用的数据库。
(3)新建一个表,作为本例使用的表。数据库输入之后,在下面更改表名,然后点更新,对数据库进行保存操作。如下图所示:
(4)更新好后,数据库表中就会多出刚才的表。
(5)增加数据。右击刚才新建的表,选择查看数据。此处增加的是用户名为admin,密码为123456。如下所示
四、数据库连接
在解决方案资源管理器中打开Web.config配置文件。插入以下代码
<connectionStrings>
<add name="ConnectionString" connectionString="...此处内容选取新建好的数据库的属性的连接字符串..."/>
</connectionStrings>
连接字符串查找(右击数据库,选属性,从右侧的属性列表里找连接字符串):
五、在页面的cs文件中调用数据库操作代码
(1)首先修改引用操作类代码文件。将引用的操作代码cs文件属性的内容改为编译。然后对本例解决方案进行生成操作。
(2)在页面的cs文件中使用using引用数据库操作类的命名空间。
(例如此处是using GROUP.Manage;)
(3)书写代码,调用数据库,获取数据。(示例代码如下)
BaseClass BaseClass1 = new BaseClass(); protected void Page_Load(object sender, EventArgs e) { GetDPListValue(); } protected void GetDPListValue() { DataSet ds = new DataSet(); ds.Tables.Add("DPList"); ds.Tables["DPList"].Columns.Add("UserLevelNum", typeof(int)); ds.Tables["DPList"].Columns.Add("UserLevelText", typeof(string)); ds.Tables["DPList"].Rows.Add(new object[] { 1, "管理员" }); ds.Tables["DPList"].Rows.Add(new object[] { 2, "组织者" }); ds.Tables["DPList"].Rows.Add(new object[] { 3, "参与者" }); DropDownListLevel.DataSource = ds.Tables["DPList"]; DropDownListLevel.DataValueField = "UserLevelNum"; DropDownListLevel.DataTextField = "UserLevelText"; DropDownListLevel.DataBind(); } protected void ButtonOK_Click(object sender, EventArgs e) { string status = DropDownListLevel.SelectedValue.ToString().Trim(); //获取下拉列表的值 string strsql; strsql = "select * from [AMUser] where UserName='" + username.Text + "' and UserPassword='" + userpwd.Text + "' and UserLevel='" + status + "' and IsDeleted='0'"; DataSet ds = new DataSet(); ds = BaseClass1.GetDataSet(strsql, "amuser"); if (ds.Tables["amuser"].Rows.Count == 0) { LabelLoginInf.Text = "输入的用户名或密码错误!"; } else { Session["UserName"] = username.Text.ToString(); Session["UserID"] = ""; String level = DropDownListLevel.SelectedValue.ToString().Trim(); //下面可以根据不同的需要(根据用户等级)进入相应的页面 LabelLoginInf.Text = "登录成功!"; Response.Write("<script>alert('登录成功!即将进入系统……');window.location.href='#';</script>"); } }
六、完成并进行网页测试
0 0
- 使用VS2013与内置数据库进行连接
- C#:使用VS内置DataConnectionDialog连接数据库
- vs2013 连接mysql数据库
- VS2013连接mysql数据库
- vs2013连接mysql数据库
- VS2013连接MySQL数据库
- 与数据库oracle进行连接
- R与数据库进行连接
- VS2013使用EF6与mysql数据库
- VS2013使用EF6与mysql数据库
- VS2013使用EF6与mysql数据库
- VS2013使用EF6与mysql数据库
- VS2013使用EF6与mysql数据库
- vs2013连接mysql数据库并使用EntityFramework技术db first
- vs2013下使用mysql connector连接mysql数据库
- 使用VS2013 + EF6 + .NET4.5 连接Mysql数据库
- Flash与ASP进行连接数据库操作
- 使用JavaBean进行数据库的连接(转载)
- Sicily: 101.Alphacode
- Xamarin.Forms的基本页面和基本视图
- UIbutton 上设置文字图片垂直居中对齐
- 结构和联合--结构体内存和位段内存开辟规则
- 模板方法模式
- 使用VS2013与内置数据库进行连接
- 从零开始学习EasyDarwin(RTSP篇之协议分析篇)
- Bloom Filter概念和原理
- NSSM使用
- 探秘Java中String、StringBuilder以及StringBuffer
- 简单的获取Android手机屏幕的像素
- 天梯赛练习 是否完全二叉搜索树
- 北京阿凡达雪景
- replica character liquid crystal display control