按照通常的套路,首先創建一個空白的解決方案,需要用到.netcore sdk命令:
dotnet new sln -o dotnetcore_tutrorial
這個時候可以看到在目標目錄下生成了一個同名的.sln文件,這個和使用vs是一樣的,在我們實際開發過程中,通常要建立運行項目(web項目或者console項目),多個類庫項目,以及單元測試項目。
首先建立一個類庫項目,並將該項目加入到解決方案中:
dotnet new classlib -o DotNetTurorial.Common
dotnet sln add DotNetTurorial.Common/DotNetTurorial.Common.cspro
ps:最好把類庫項目創建在dotnetcore_tutrorial目錄下,這樣可以保證.sln文件和項目文件在同一個目錄下
通過同樣的方式創建控制台項目和單元測試項目
dotnet new console -o DotNetTurorial.ConsoleApp dotnet sln add DotNetTurorial.ConsoleApp/DotNetTurorial.ConsoleApp.csproj dotnet new xunit -o DotNetTurorial.UnitTest dotnet sln add DotNetTurorial.UnitTest/DotNetTurorial.UnitTest.csproj
現在整個項目的結構已經建立完成,我們用vscode打開解決方案對應的文件夾,目錄結構如下:
接下來需要添加項目引用,也就是console項目需要引用類庫項目:
<Project Sdk="Microsoft.NET.Sdk"> <PropertyGroup> <OutputType>Exe</OutputType> <TargetFramework>netcoreapp2.0</TargetFramework> </PropertyGroup> <ItemGroup> <ProjectReference Include="..\DotNetTurorial.Common\DotNetTurorial.Common.csproj"/> </ItemGroup> </Project>
同樣的方法添加到測試項目:
<Project Sdk="Microsoft.NET.Sdk"> <PropertyGroup> <TargetFramework>netcoreapp2.0</TargetFramework> <IsPackable>false</IsPackable> </PropertyGroup> <ItemGroup> <PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.3.0-preview-20170427-09" /> <PackageReference Include="xunit" Version="2.2.0" /> <PackageReference Include="xunit.runner.visualstudio" Version="2.2.0" /> </ItemGroup> <ItemGroup> <ProjectReference Include="..\DotNetTurorial.Common\DotNetTurorial.Common.csproj"/> </ItemGroup> </Project>
配置好以后,切換到Console項目所在目錄,執行dotnet restore 初始化項目,執行dotnet build 編譯項目
接下來實現一個簡單的業務邏輯,通過console程序添加學生信息,並把數據存入mysql中:
操作數據需要用到幾個nuget包,需要在項目文件中手動配置(dotnetcore2.0 不需要再引入NETStandard.Library)。
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Dapper" Version="1.50.2"/>
<PackageReference Include="NETStandard.Library
" Version="1.6.0"/>
<PackageReference Include="SapientGuardian.MySql.Data" Version="6.9.813"/>
</ItemGroup>
</Project>
這里建議安裝一個vscode插件nuget package manager,可以通過該插件快速安裝nuget包,安裝完成以后在項目文件中右鍵,選擇命令面板
在命令面板中輸入nuget,可以根據包名搜索出該包的不同版本,添加以后使用dotnet resotore 命令還原nuget庫到依賴
在common項目中增加一個數據庫操作類:
using System; using Dapper; using MySql.Data.MySqlClient; namespace DotNetTurorial.Common { public class SQLHelper { public const string _conStr="server=127.0.0.1;port=3306;user id=root;password=123456;database=dotnetcore;pooling=false"; public static int AddStudent(string name,int gender,string phone) { MySqlConnection connect=new MySqlConnection(_conStr); var ret = connect.Execute("insert into student(name,gender,phone) values(@name,@gender,@phone)",new {name=name,gender=gender,phone=phone}); return ret; } } }
在console項目中增加輸入相關控制代碼:
using System; using DotNetTurorial.Common; namespace DotNetTurorial.ConsoleApp { class Program { static void Main(string[] args) { //Console.OutputEncoding = Encoding.UTF8; // 設置控制台編碼 AddUser(); while(Console.ReadLine()!="exit"){ AddUser(); } } static void AddUser() { Console.WriteLine("please enter name:"); var name=Console.ReadLine(); Console.WriteLine("please enter gender,1 for male and 0 for female."); var gender = Console.ReadLine(); Console.WriteLine("please enter phone:"); var phone = Console.ReadLine(); var ret = SQLHelper.AddStudent(name,Convert.ToInt32(gender),phone); if(ret>0) { Console.WriteLine("success"); } } } }
在終端中輸入dotnet run 運行程序
如果需要調試,需要安裝C#擴展:
如果調試有輸入到console程序,需要修改下:launch.json,注釋掉"Console":"internalConsole", 增加:"externalConsole":true, 否則無法數據
{ // Use IntelliSense to find out which attributes exist for C# debugging // Use hover for the description of the existing attributes // For further information visit https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md "version": "0.2.0", "configurations": [ { "name": ".NET Core Launch (console)", "type": "coreclr", "request": "launch", "preLaunchTask": "build", // If you have changed target frameworks, make sure to update the program path. "program": "${workspaceRoot}/DotNetTurorial.ConsoleApp/bin/Debug/netcoreapp2.0/DotNetTurorial.ConsoleApp.dll", "args": [], "cwd": "${workspaceRoot}/DotNetTurorial.ConsoleApp", // For more information about the 'console' field, see https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md#console-terminal-window //"console": "internalConsole", "externalConsole":true, //使用外置的控制台 "stopAtEntry": false, "internalConsoleOptions": "openOnSessionStart" }, { "name": ".NET Core Attach", "type": "coreclr", "request": "attach", "processId": "${command:pickProcess}" } ] }