目錄:
0. 前言
1. 在線安裝器
2. 安裝VS
3. HelloWorld
4. ASP.NET MVC
5. 軟件下載
6. 結尾
工作原因,上下班背著我的雷神,一個月瘦了10斤,扛不住了,就把我的Mac放在公司。
Mac之前為了運行VS,還買了一個PD虛擬機。。。
現在,我終於喝到雞湯了——隨著Visual Studio 2017的發布,for Mac的版本也誕生了、
下載地址大家可以去visualstudio官網下載,不過可能會有點兒慢,我上傳了百度雲。文章最後提供下載。
Mac的安裝一貫很簡單,裝載鏡像,拽到App中就可以了,這裡為照顧新手,簡單說一下過程:
【PS:也是夠了,寫這篇文章,浏覽器崩了N次。重寫了多少遍。=_=】
下載得到VisualStudioforMacPreviewInstaller.dmg,大小23.9MB,這個是安裝程序,說白了就是在線安裝的。
(文章結尾有下載)
雙擊鏡像載入:
1 using System;
2 using System.Configuration;
3 using System.Data.SqlClient;
4
5 namespace HelloWorld
6 {
7 class MainClass
8 {
9 public static void Main(string[] args)
10 {
11 string connStr = ConfigurationManager.ConnectionStrings["ConnStr"].ConnectionString;
12
13 string sql = "select name from sysobjects";
14
15 SqlConnection conn = null;
16 SqlDataReader reader = null;
17
18 try
19 {
20 conn = new SqlConnection(connStr);
21 conn.Open();
22 SqlCommand comm = new SqlCommand(sql, conn);
23 reader = comm.ExecuteReader(System.Data.CommandBehavior.CloseConnection);
24 while (reader.Read())
25 {
26 Console.WriteLine(reader["name"]);
27 }
28 }
29 catch (Exception ex)
30 {
31 Console.WriteLine(ex.Message);
32 }
33 finally
34 {
35 if (reader != null)
36 reader.Close();
37 if (conn != null)
38 conn.Close();
39 }
40 }
41 }
42 }
ADO.NET測試代碼
1 using System;
2 namespace HelloWorldMVC
3 {
4 public class User
5 {
6 public int Id { get; set; }
7 public string Name { get; set; }
8 public string Email { get; set; }
9 }
10 }
修改Controllers/HomeController.cs控制器代碼,創建User數組並存入ViewBag中:
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Web; 5 using System.Web.Mvc; 6 using System.Web.Mvc.Ajax; 7 8 namespace HelloWorldMVC.Controllers 9 { 10 public class HomeController : Controller 11 { 12 public ActionResult Index() 13 { 14 var users = new[] 15 { 16 new User(){ Id=1001, Name="張董", Email="[email protected]" }, 17 new User(){ Id=1002, Name="卡特琳娜", Email="[email protected]" }, 18 new User(){ Id=1003, Name="盲僧", Email="[email protected]" }, 19 new User(){ Id=1004, Name="莫甘娜", Email="[email protected]" }, 20 new User(){ Id=1005, Name="賞金", Email="[email protected]" } 21 }; 22 23 ViewBag.Users = users; 24 25 return View(); 26 } 27 } 28 }
修改Views/Home/Index.cshtml代碼,顯示剛剛存入的數組信息:
1 @if(ViewBag.Users==null) 2 { 3 <p>暫無資料。</p> 4 } 5 else 6 { 7 <table> 8 <thead> 9 <tr> 10 <th>編號</th> 11 <th>姓名</th> 12 <th>郵箱</th> 13 </tr> 14 </thead> 15 <tbody> 16 @foreach(var item in ViewBag.Users) 17 { 18 <tr> 19 <td>@item.Id</td> 20 <td>@item.Name</td> 21 <td>@item.Email</td> 22 </tr> 23 } 24 </tbody> 25 </table> 26 }
點擊運行:
1 <!DOCTYPE html>
2 <html>
3 <head>
4 <meta charset="utf-8">
5 <title>@ViewBag.Title</title>
6 </head>
7 <body>
8 @RenderBody()
9 </body>
10 </html>
再次運行:
OK,完事兒~
在線安裝+VS安裝程序打包下載
哈哈,到此,案例都測試完了,感覺還不錯。
只是有一點,貌似自帶的沒有窗體應用程序。。。
好了,各位可以自己玩玩,有啥問題下面留言 咱們一起交流交流感情。哈哈。