VS2017

VS2017 适用于 Windows 和 Mac 的 Visual Studio IDE。

这么多年,终于支持了。不容易。

不是很想了解 Visual Studio Code,因为使用MONO也可以简单的编译。

Install

VS2017 安装案例

  • Download preview

Download from here.

  • Install

一般接受协议之后会失败,后面需要选择要下载的东西。复制其中的链接,下载安装即可。

install

Hello World

  • Creat Project

install

  • HelloWorld.cs
  [c#]
1
2
3
4
5
6
7
8
9
10
11
12
using System; namespace HelloWorld { class MainClass { public static void Main(string[] args) { Console.WriteLine("Hello World!"); } } }

result

  [plaintext]
1
2
3
Hello World! Press any key to continue...

这个界面和 Mono 是好像的。

ADO.NET

  • 引入需要的外部DLL

目前的项目中只有一个 System 引用。如果要测试下 ADO.NET, 需要 System.DataSystem.ConfigurationMySQL.Data

可通过Reference->右键编辑->添加

1)System.Data

数据库访问

2)System.Configuration

系统配置

3) MySQL.Data

这个需要在 Package->右键编辑->添加,测试时(2017-04-02)还不能在Reference找到。

  • Create App.config

本测试使用mysql数据库

  [xml]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<?xml version="1.0" encoding="utf-8"?> <configuration> <connectionStrings> <add name="connStr" connectionString="Server=localhost;Database=blog;Uid=root;Pwd=123456;" providerName="MySql.Data.MySqlClient"/> </connectionStrings> <system.data> <DbProviderFactories> <remove invariant="MySql.Data.MySqlClient" /> <add name="MySQL Data Provider" invariant="MySql.Data.MySqlClient" description=".Net Framework Data Provider for MySQL" type="MySql.Data.MySqlClient.MySqlClientFactory, MySql.Data, Version=6.9.9.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" /> </DbProviderFactories> </system.data> </configuration>

其中 system.data 会在你安装 MySQL.Data 自动生成。

The type ‘XXX’ is defined in an assembly that is not reference

Troubleshooting Broken References

Asp.net中配置文件(8)

assemblies 元素(ASP.NET 设置架构)

  • MyConn.cs

简单的数据库链接测试,当然这里的表和数据库是我本地的。如果尝试,请自行替换。

  [c#]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
using System; using System.Configuration; using MySql.Data.MySqlClient; namespace HelloWorld { public class MysqlConn { public static void Main(string[] args) { //1. Get the connection for mysql string connStr = ConfigurationManager.ConnectionStrings["ConnStr"].ConnectionString; string sql = "select * from user"; MySqlConnection conn = null; MySqlDataReader reader = null; try { conn = new MySqlConnection(connStr); conn.Open(); MySqlCommand command = new MySqlCommand(sql, conn); reader = command.ExecuteReader(); while (reader.Read()) { if (reader.HasRows) { Console.WriteLine("username:{0}", reader.GetString("username")); } } }catch (Exception ex) { Console.WriteLine(ex.Message); } finally { if (conn != null) { conn.Close(); } if (reader != null) { reader.Close(); } } } } }

运行

  [plaintext]
1
2
3
4
5
username:DD username:张晓明 username:DD2 Press any key to continue...

原表的部分数据

  [plaintext]
1
2
3
4
5
6
7
8
9
mysql> select id, username, nickname, email, login_on from user; +----+------------+------------+----------------------+----------+ | id | username | nickname | email | login_on | +----+------------+------------+----------------------+----------+ | 1 | DD | ryo | xx@gmail.com | NULL | | 2 | 张晓明 | xiaoming | xiaoming@gmail.com | NULL | | 8 | DD2 | DD2 | xx@qq.com | NULL | +----+------------+------------+----------------------+----------+ 3 rows in set (0.00 sec)

ASP.NET MVC

测试一下MVC。

  • Create project

解决方案->右键Add->Add new project 选择 Other - ASP.NET MVC Project

配置可以选择默认,然后下一步。如果一切正常,如下:

accept

然后请选择Accept。你没有权利拒绝(/smile)。

  • Start

在新建的MVC项目右键,设置为启动项目。运行。

会自动打开页面,显示如下几个大字:

(由此可见,这 VS2017 就是在 mono 的基础上封装了一层外壳。)

  [plaintext]
1
Welcome to ASP.NET MVC 5.2 on Mono!
  • Add model

Models 文件夹下添加实体类 User.

  [c#]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
using System; namespace HelloWorldMVC { public class User { private string id; private string name; public User(string id, string name) { this.id = id; this.name = name; } string Id { get { return id; } } string Name { get { return name; } } } }
  • Edit Controllers/HomeController.cs
  [c#]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using System.Web.Mvc.Ajax; namespace HelloWorldMVC.Controllers { public class HomeController : Controller { public ActionResult Index() { var users = new[] { new User("001", "天下第一"), new User("002", "我才是第一"), new User("003", "楼上二智障") }; ViewBag.Users = users; return View(); } } }
  • Edit Views/Home/Index.cshtml
  [html]
1
2
3
4
5
6
7
8
9
10
11
<table> <tbody> @foreach(var item in ViewBag.Users) { <tr> <td>@item.Id</td> <td>@item.Name</td> </tr> } </tbody> </table>

显示结果如下:

  [plaintext]
1
2
3
001 天下第一 002 我才是第一 003 楼上二智障

神奇的没有乱码。如果乱码,在Views/Shared/Layout.cshtml修改如下:

  [html]
1
2
3
4
5
6
7
8
9
10
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>@ViewBag.Title</title> </head> <body> @RenderBody() </body> </html>