YourController Sut = new YourController(); var request = Substitute.For<HttpRequestBase>(); var context = Substitute.For<HttpContextBase>(); request.HttpMethod.Returns("Get"); context.Request.Returns(request);
Sut.ControllerContext = new ControllerContext(context, new RouteData(), Sut);
///<summary> /// Detaches the database. ///</summary> privatevoidDetachDatabase() { using (var connection = new SqlConnection(LocalDbMasterConnectionString)) { connection.Open(); var cmd = connection.CreateCommand(); cmd.CommandText = string.Format("exec sp_detach_db '{0}'", this.DatabaseName); try { cmd.ExecuteNonQuery(); } catch { Console.WriteLine("Could not detach"); } } }
///<summary> /// Cleanups the database. ///</summary> ///<returns>System.String.</returns> privatestringCleanupDatabase() { var fileName = string.Concat(@"G:\",this.DatabaseName); try { var mdfPath = string.Concat(fileName, ".mdf"); var ldfPath = string.Concat(fileName, "_log.ldf");
var mdfExists = File.Exists(mdfPath); var ldfExists = File.Exists(ldfPath);
if (mdfExists) File.Delete(mdfPath); if (ldfExists) File.Delete(ldfPath); } catch { Console.WriteLine("Could not delete the files (open in Visual Studio?)"); } return fileName; }
voidMain() { for (int i = 0; i < 10; i++) { SqlConnection conn = new SqlConnection(this.Connection.ConnectionString); conn.Open(); SqlCommand command = new SqlCommand("select top 1 CustomerID from Customers", conn); var Result = command.ExecuteScalar().ToString(); Result.Dump(); } }
資料庫那邊用以下語法查詢目前連線DB得使用者與狀態
1 2
select*from sys.sysprocesses where status ='sleeping'and spid >50orderby login_time desc
for (int i = 0; i < 10; i++) { SqlConnection conn = new SqlConnection(this.Connection.ConnectionString); conn.Open(); SqlCommand command = new SqlCommand("select top 1 CustomerID from Customers", conn); var Result = command.ExecuteScalar().ToString(); Result.Dump(); conn.Close(); //有呼叫Close } }
//準備資料 var BuildingList = new List<Building>(); for (int i = 0; i < 5; i++) { BuildingList.Add(new Building { Age = 1 * (i+1), Pin = 1 * (i+1), CaseDate = DateTime.Now.AddDays(i +1) }); }
//排序欄位 switch (OrderByColumn) { case OrderByColumnEnum.Pin: switch (OrderBy) { case OrderByEnum.ASC: //因為裡面用到的欄位不同,又是強型別,只能寫死... BuildingList = BuildingList.OrderBy(x => x.Pin).ToList(); break; case OrderByEnum.DESC: BuildingList = BuildingList.OrderByDescending(x => x.Pin).ToList(); break; } break; case OrderByColumnEnum.Age: switch (OrderBy) { case OrderByEnum.ASC: BuildingList = BuildingList.OrderBy(x => x.Age).ToList(); break; case OrderByEnum.DESC: BuildingList = BuildingList.OrderByDescending(x =>x.Age).ToList(); break; } break; case OrderByColumnEnum.CaseDate: switch (OrderBy) { case OrderByEnum.ASC: BuildingList = BuildingList.OrderBy(x => x.CaseDate).ToList(); break; case OrderByEnum.DESC: BuildingList = BuildingList.OrderByDescending(x => x.CaseDate).ToList(); break; } break; } BuildingList.Dump(); }
//準備資料 var BuildingList = new List<Building>(); for (int i = 0; i < 5; i++) { BuildingList.Add(new Building { Age = 1 * (i+1), Pin = 1 * (i+1), CaseDate = DateTime.Now.AddDays(i +1) }); }
//排序欄位 var param = OrderByColumnEnum.Pin.ToString();
//透過PropertyInfo操作 var propertyInfo = typeof(Building).GetProperty(param); switch (OrderBy) { case OrderByEnum.ASC: BuildingList = BuildingList.OrderBy(x => propertyInfo.GetValue(x, null)).ToList(); break; case OrderByEnum.DESC: BuildingList = BuildingList.OrderByDescending(x => propertyInfo.GetValue(x, null)).ToList(); break; }