一、安裝Visual Studio 2015,我這裏安裝的是專業版。css
二、安裝.NET Core SDK相關html
下載列表: https://www.microsoft.com/net/download/coremysql
參考連接:http://www.cnblogs.com/fonour/p/5848933.htmlgit
http://www.cnblogs.com/linezero/p/NETCoreMySQL.htmlgithub
asp.net Mvc http://www.cnblogs.com/slark/p/mvc-5-get-started-view.htmlsql
添加MVC引用有兩種方法。數據庫
1 經過project.jsonjson
打開project.json文件,在dependencies部門增長Microsoft.AspNetCore.Mvc的引用,當你輸入時,Visual Studio會有自動的提示,很是方便。api
"dependencies": { "Microsoft.NETCore.App": { "version": "1.0.0", "type": "platform" }, "Microsoft.AspNetCore.Diagnostics": "1.0.0", "Microsoft.AspNetCore.Server.IISIntegration": "1.0.0", "Microsoft.AspNetCore.Server.Kestrel": "1.0.0", "Microsoft.Extensions.Logging.Console": "1.0.0", "Microsoft.AspNetCore.Mvc": "1.0.0" },
2 經過NuGet數組
經過NuGet管理器搜索Microsoft.AspNetCore.Mvc添加引用,或直接在程序包管理器控制檯輸入命令
Install-Package Microsoft.AspNetCore.Mvc
便可完成Mvc引用的添加。
添加MVC服務
修改Startup.cs的ConfigureServices方法以下
public void ConfigureServices(IServiceCollection services) { services.AddMvc(); }
添加Http請求管道處理
修改Startup.cs的Configure方法以下
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) { loggerFactory.AddConsole(); if (env.IsDevelopment()) { //開發環境異常處理 app.UseDeveloperExceptionPage(); } //使用Mvc,設置默認路由 app.UseMvc(routes => { routes.MapRoute( name: "default", template: "{controller=Login}/{action=Index}/{id?}" ); }); }
MySQL官方驅動:http://www.cnblogs.com/linezero/p/5806814.html
.NET Core 使用Dapper 操做MySQL 數據庫, .NET Core 使用Dapper。
目前官方沒有出.NET Core MySQL 驅動,可是已經有第三方進行改動封裝出.NET Core MySQL Connector 預覽版。
Dapper 也已經出了 .NET Core 預覽版。
Dapper dot net 是一個輕量型的ORM,可是性能很強大。
有了.NET Core MySQL Connector 咱們能夠直接使用ADO.NET 操做數據庫。
第三方 MySQL Connector: https://github.com/SapientGuardian/mysql-connector-net-netstandard
Dapper: https://github.com/StackExchange/dapper-dot-net
使用 NuGet 控制檯添加
Install-Package SapientGuardian.MySql.Data -Pre
Install-Package Dapper -Pre
{ "version": "1.0.0-*", "buildOptions": { "emitEntryPoint": true }, "dependencies": { "Microsoft.NETCore.App": { "type": "platform", "version": "1.0.1" }, //mysql 相關配置 "Dapper": "1.50.2", "MySql.Data.Core": "7.0.4-IR-191" }, "frameworks": { "netcoreapp1.0": { "imports": "dnxcore50" } } }
在Asp.Net Core 中使用
public static void Main(string[] args) { Encoding.RegisterProvider(CodePagesEncodingProvider.Instance); MySqlConnection con = new MySqlConnection("server=127.0.0.1;database=test;uid=root;pwd=123456;charset='gbk';SslMode=None");//新增數據 con.Execute("insert into user values(null, '測試', 'http://www.cnblogs.com/linezero/', 18)"); //新增數據返回自增id var id=con.QueryFirst<int>("insert into user values(null, 'linezero', 'http://www.cnblogs.com/linezero/', 18);select last_insert_id();"); //修改數據 con.Execute("update user set UserName = 'linezero123' where Id = @Id", new { Id = id }); //查詢數據 var list=con.Query<User>("select * from user"); foreach (var item in list) { Console.WriteLine($"用戶名:{item.UserName} 連接:{item.Url}"); } //刪除數據 con.Execute("delete from user where Id = @Id", new { Id = id }); Console.WriteLine("刪除數據後的結果"); list = con.Query<User>("select * from user"); foreach (var item in list) { Console.WriteLine($"用戶名:{item.UserName} 連接:{item.Url}"); } Console.ReadKey(); }
參考:http://www.cnblogs.com/savorboard/p/5592948.html
經過NuGet程序包管理器、控制檯、或直接修改project.json文件,添加對Microsoft.AspNetCore.Session中間件的引用
修改Startup.cs文件的的ConfigureServices方法,增長Session服務註冊
public void ConfigureServices(IServiceCollection services) { //添加mvc服務 services.AddMvc(); //添加session服務 services.AddSession(); }
修改Startup.cs文件的的Configure方法,請求管道中啓用Session
// 啓用Session app.UseSession();
//賦值 鍵值對形式儲存 這裏須要把數據轉換成Byte數組 HttpContext.Session.Set("UserInfo", ByteConvertHelper.Object2Bytes(null)); //或者session的值 將數據轉換成對象 ViewBag.Message = ByteConvertHelper.Bytes2Object(HttpContext.Session.Get("UserInfo")); //session 儲存字符串 HttpContext.Session.SetString("Test", "Ben Rules!"); //獲取session ViewBag.Message = HttpContext.Session.GetString("Test");
數組轉換
參考:http://www.cnblogs.com/fonour/p/5943401.html
/// <summary> /// 轉換Byte數組 /// </summary> public class ByteConvertHelper { /// <summary> /// 將對象轉換爲byte數組 /// </summary> /// <param name="obj">被轉換對象</param> /// <returns>轉換後byte數組</returns> public static byte[] Object2Bytes(object obj) { string json = JsonConvert.SerializeObject(obj); byte[] serializedResult = System.Text.Encoding.UTF8.GetBytes(json); return serializedResult; } /// <summary> /// 將byte數組轉換成對象 /// </summary> /// <param name="buff">被轉換byte數組</param> /// <returns>轉換完成後的對象</returns> public static object Bytes2Object(byte[] buff) { string json = System.Text.Encoding.UTF8.GetString(buff); return JsonConvert.DeserializeObject<object>(json); } /// <summary> /// 將byte數組轉換成對象 /// </summary> /// <param name="buff">被轉換byte數組</param> /// <returns>轉換完成後的對象</returns> public static T Bytes2Object<T>(byte[] buff) { string json = System.Text.Encoding.UTF8.GetString(buff); return JsonConvert.DeserializeObject<T>(json); } }