问答文章1 问答文章501 问答文章1001 问答文章1501 问答文章2001 问答文章2501 问答文章3001 问答文章3501 问答文章4001 问答文章4501 问答文章5001 问答文章5501 问答文章6001 问答文章6501 问答文章7001 问答文章7501 问答文章8001 问答文章8501 问答文章9001 问答文章9501

asp+asscss搭建网站?中最初连接数据库的一般是哪个asp?

发布网友 发布时间:2022-04-10 06:15

我来回答

4个回答

懂视网 时间:2022-04-10 10:37

System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.Runtime.Serialization; using System.Text; namespace Entity.Core { /// <summary> /// DB表基础属性 /// </summary> public abstract class BaseEntity<T> { public BaseEntity() { CreteTime = DateTime.Now; } /// <summary> /// 主键Id /// </summary> [DataMember] [Key] public T Id { get; set; } /// <summary> /// DB版号,Mysql详情参考;http://www.cnblogs.com/shanyou/p/6241612.html /// </summary> //1602977721//Mysql不允许byte[]类型上标记TimeStamp/RowVersion,这里使用DateTime类型配合标记ConcurrencyCheck达到并发控制 [ConcurrencyCheck] public DateTime RowVersion { get; set; } /// <summary> /// 创建时间 /// </summary> public DateTime CreteTime { get; set; } } }

Product:

using Entity.Core;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Text;

namespace Entity.Table
{
 /// <summary>
 /// 商品类
 /// </summary>
 public class Product : BaseEntity<long>
 {
 /// <summary>
 /// 名称
 /// </summary>
 [StringLength(20)]
 [Required]
 public string Name { get; set; }

 /// <summary>
 /// 描述
 /// </summary>
 [StringLength(500)]
 [Required]
 public string Description { get; set; }

 /// <summary>
 /// 类别
 /// </summary>
 [Range(1, int.MaxValue)]
 public int Category { get; set; }

 /// <summary>
 /// 原价
 /// </summary>
 [Required]
 public decimal Price { get; set; }

 /// <summary>
 /// 现价
 /// </summary>
 public decimal Discount { get; set; }
 }
}

添加数据层DAL:

右键添加>新建项目>.NET Core 类库

技术分享

添加引用:

Microsoft.EntityFrameworkCore(也可加入Microsoft.AspNetCore.All,但会有用不到的功能造成浪费)

Microsoft.EntityFrameworkCore.Tools(迁移支持)

Pomelo.EntityFrameworkCore.MySql(Mysql支持)具体使用细则,请参考:Pomelo.EntityFrameworkCore.MySql使用细则

<Project Sdk="Microsoft.NET.Sdk">

 <PropertyGroup>
 <TargetFramework>netcoreapp2.0</TargetFramework>
 </PropertyGroup>

 <ItemGroup>
 <PackageReference Include="Microsoft.EntityFrameworkCore" Version="2.0.0" />
 <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="2.0.0" PrivateAssets="All" />
 <PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Version="2.0.0-rtm-10062" />
 </ItemGroup>

 <ItemGroup>
 <DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="2.0.0" />
 </ItemGroup>

 <ItemGroup>
 <ProjectReference Include="..EntityEntity.csproj" />
 </ItemGroup>

</Project>

 

技术分享

添加DbContext数据上下文

using Entity.Table;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DAL
{
 public class ProductContext : DbContext
 {
 //https://docs.microsoft.com/en-us/aspnet/core/data/ef-mvc/complex-data-model
 public ProductContext(DbContextOptions<ProductContext> options) : base(options)
 {
  //在此可对数据库连接字符串做加解密操作
 }

 public DbSet<Product> Courses { get; set; }


 protected override void OnModelCreating(ModelBuilder modelBuilder)
 {
   base.OnModelCreating(modelBuilder);
 }
 }
}

ASP.Net Core API项目中引用刚创建的DAL类库

添加Service服务层

右键添加>新建项目>.NetCore 类库

 技术分享

添加引用:

添加Entity和DAL引用,其次再添加第三方数据仓储Microsoft.EntityFrameworkCore.UnitOfWork(最新)

 <ItemGroup>
 <PackageReference Include="Microsoft.EntityFrameworkCore.UnitOfWork" Version="2.0.1" />
 </ItemGroup>

 <ItemGroup>
 <ProjectReference Include="..DALDAL.csproj" />
 <ProjectReference Include="..EntityEntity.csproj" />
 </ItemGroup>

 文件目录如下:

技术分享

IProductService:

using System;
using System.Collections.Generic;
using System.Text;

namespace Service.ProductService
{
 public interface IProductService
 {
 string Test();
 }
}

ProductService:

using Entity.Table;
using Microsoft.EntityFrameworkCore;

namespace Service.ProductService
{
 public class ProductService : IProductService
 {
 private readonly IUnitOfWork _unitOfWork;
 public ProductService(IUnitOfWork unitOfWork)
 {
  _unitOfWork = unitOfWork;
 }

 public string Test()
 {
  var repo = _unitOfWork.GetRepository<Product>();
  repo.Insert(new Product
  {
  Category = 1,
  Description = "此商品为澳洲代购,买不了吃亏买不了上当",
  Discount = (decimal)899.21,
  Price = (decimal)98.2,
  Name = "澳洲袋数粉",
  });
  _unitOfWork.SaveChanges();//提交到数据库
  var result = repo.GetFirstOrDefault()?.Name ?? string.Empty;
  return result;
 }
 }
}

ASP.Net Core API添加刚创建的Service类库引用

<ItemGroup>
 <ProjectReference Include="..DALDAL.csproj" />
 <ProjectReference Include="..ServiceService.csproj" />
 </ItemGroup>

在 ASP.Net Core API控制器中使用service数据库

向Controller注入需要使用的接口

namespace ASP.Net_Core_API.Controllers
{
 [Route("api/[controller]")]
 public class ValuesController : Controller
 {
 private IProductService _productService;

 public ValuesController(IProductService productService)
 {
  _productService = productService;
 }
 // GET api/values
 [HttpGet]
 public IEnumerable<string> Get()
 {
  var result = _productService.Test();
  return new string[] { "value1", result };
 }
 }
}
 

Startup文件中加入Mysql支持和对应的需要的注入的service还有UnitOfWork的支持

完整文件如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Microsoft.EntityFrameworkCore;
using Entity.Table;
using DAL;
using Service.ProductService;

namespace ASP.Net_Core_API
{
 public class Startup
 {
 public Startup(IConfiguration configuration)
 {
  Configuration = configuration;
 }

 public IConfiguration Configuration { get; }

 // This method gets called by the runtime. Use this method to add services to the container.
 public void ConfigureServices(IServiceCollection services)
 {
  services.AddDbContext<ProductContext>(options =>
  options.UseMySql(Configuration.GetConnectionString("MySqlConnection")));

  services.AddUnitOfWork<ProductContext>();
  services.AddScoped(typeof(IProductService), typeof(ProductService));

  services.AddMvc();
 }

 // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
 public void Configure(IApplicationBuilder app, IHostingEnvironment env)
 {
  if (env.IsDevelopment())
  {
  app.UseDeveloperExceptionPage();
  }

  app.UseMvc();
 }
 }
}

配置appsettings.json中Mysql连接字符串

{
 "ConnectionStrings": {
 "MySqlConnection": "Server=localhost;database=NetCore_WebAPI-Mysql;uid=root;pwd=111111;"
 },
 "Logging": {
 "IncludeScopes": false,
 "Debug": {
  "LogLevel": {
  "Default": "Warning"
  }
 },
 "Console": {
  "LogLevel": {
  "Default": "Warning"
  }
 }
 }
}

迁移数据库:

 打开程序包管理器控制台:工具>NuGet包管理器>程序包管理器控制台,默认项目选中包含了DbCOntext的程序集,这里是DAL,程序包源选择全部

输入:

PM>add-migration init 待执行后输出"To undo this action,use Remove-Migration"表示生成了迁移代码 然后再输入: PM>update-database 待执行后输出"Done"表示已成功更新数据库

完整操作如下

技术分享

Tip:如果是非第一次迁移,就不能再输入PM>add-migration init,而是输入:

PM>add-migration "对本次迁移的说明"

例如,本次对Entity的某张表添加了Name属性.迁移时输入PM>add-migration AddName

输入以上待执行后,依旧输入以下即可

PM>update-database

会发现在DAL程序家下成功生成了以下目录

技术分享

再打开数据库成功依据实体Entity生成了Product表

技术分享

 

搭建连接MySql的三层架构的ASP.NetCore2.0的WebApi

标签:一个   UI   img   上下文   描述   readonly   搭建   pos   interface   

热心网友 时间:2022-04-10 07:45

一般常用与ASP配合使用的数据库有:ACCESS、SQL等,下面是常用的数据库链接方式:
一、ASP的对象存取数据库方法
  在ASP中,用来存取数据库的对象统称ADO(Active Data Objects),主要含有三种对象:
  Connection、Recordset 、Command
  Connection:负责打开或连接数据
  Recordset:负责存取数据表
  Command:负责对数据库执行行动查询命令
  二、连接各数据库的驱动程序
  连接各数据库可以使用驱动程序,也可以使用数据源,不过我建议大家使用驱动程序,因为使用驱动程序非常
方便、简单,而使用数据源比较麻烦。
  ODBC链接
  适合数据库类型 链接方式
access "Driver={microsoft access driver(*.mdb)};dbq=*.mdb;uid=admin;
pwd=pass;"
dBase "Driver={microsoft dbase driver(*.dbf)};driverid=277;dbq=------------;"
Oracle "Driver={microsoft odbc for oracle};server=oraclesever.world;uid=admin;
pwd=pass;"
MSSQL server "Driver={sql server};server=servername;database=dbname;uid=sa;
pwd=pass;"
MS text "Driver={microsoft text driver(*.txt; *.csv)};dbq=-----;extensions=asc,csv,tab,txt;Persist SecurityInfo=false;"
Visual Foxpro "Driver={microsoft Visual Foxpro driver};sourcetype=DBC;sourceDB=*.dbc;
Exclusive=No;"
MySQL "Driver={mysql};database=yourdatabase;uid=username;pwd=yourpassword;
option=16386;"
  OLEDB链接
  适合的数据库类型 链接方式
access "Provider=microsoft.jet.oledb.4.0;data source=your_database_path;user id=admin;
password=pass;"
Oracle "Provider=OraOLEDB.Oracle;data source=dbname;user id=admin;password=pass;"
MS SQL Server "Provider=SQLOLEDB;data source=machinename;initial catalog=dbname;
userid=sa;password=pass;"
MS text "Provider=microsof.jet.oledb.4.0;data source=your_path;Extended Properties′text;
FMT=Delimited′"

  而我们在一般情况下使用Access的数据库比较多,在这里我建议大家连接Access数据库使用下面的方法:
dim conn
set conn = server.createobject("adodb.connection")
conn.open = "provider=microsoft.jet.oledb.4.0;" & "data source = " & server.mappath
("../db/bbs.mdb") 其中../db/bbs.mdb是你的数据库存放的相对路径!如果你的数据库和ASP文件在
同一目录下,你只要这样写就可以了:

热心网友 时间:2022-04-10 09:03

一般常用的是conn.asp追问其他几个有什么作用?

追答看不到内容,谁能知道有什么用,你看一下内容,分析一下呗

热心网友 时间:2022-04-10 10:37

一般是conn.asp或者inc.asp 其它应该不会用
声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com
我用银行卡网购了。怎么被注销了? 装电线的时候,不同区域该用什'么规格的电线? SY0401-98《输油输气管道线路工程施工及验收规范》废止后,用什么规范... 汇率一直变动,对中国工业品出口竞争力产生了影响,但是应该用什么... ...整个盘面流出资金大于流入资金,但是股票仍然升.. 请问股市或个股资金的流入与流出是什么意思啊? 股市之中的"资金流入"和"资金流出"分别是什么意思? 什么是股市中的资金流入和流出现象,如何理解 耕升gtx titan x 显卡怎么样 gtxtitanx是游戏显卡吗 英特尔第四代酷睿i5-4210h @ 2.90GHz双核 ,显卡gtx960的电脑怎么样 第四代智能英特尔酷睿i5处理器哪个型号的好 4代酷睿i5-4200M这个CPU怎么样 “凡士林”有什么用? 凡士林有什么用 凡士林 用法 凡士林有什么用啊 凡士林妙用 聚酯纤维 是什么玩意儿? 饮料销售培&#x022E; 手上长老年斑怎么办 什么是销售技巧PPTb 手指夹里不时会有白色小斑,怎么回事? 聚酯纤维和PU有甚么区分哪一个好 内裤上面百分之88显示黏胶是什么成分 手上这种斑点怎么弄 手指上有白斑? 为什么路边的快速充电会损害电动车的寿命? 电动车快速充电好不好 41.5%聚酯纤维 38.5%粘胶 是什么 怎么炒鱿鱼。 创青春大赛在系里答辩,答的不好,会被淘汰吗? 2015经济学院创青春立项答辩会新闻稿 创青春的大赛内容 大二下应该参加大学生创新创业吗? MBA专项赛 网络信息专项赛是什么意思 一个女人虚荣心太强会有什么后果? 虚荣心太强会给女人带来哪些危害? 爱慕虚荣的女人,真的不适合走到最后吗? 一个女生的虚荣心太强会怎么样? 知否:那些爱慕虚荣的女人,最后都怎么样了? 虚荣心强的女人会怎样呢??比如说… 虚荣心的危害有哪些? 虚荣心太强的女人能不能娶? 一个特别虚荣的女生怎么办 一个女人的虚荣心表现在哪里,举例 家里条件不好还爱慕虚荣的女人下场啥样 女人的虚荣心有多可怕? 11代cpu待机温度 虚荣心强的女人怎么样