2016年12月11日 星期日

MS SQL Limit 範例!

以每頁取 10 為範例:

0 ~ 10 的取法:

SELECT TOP 10 *
    FROM USER_TABLE
        WHERE ([USER_ID] NOT IN
           (SELECT TOP 0 [USER_ID]
                 FROM USER_TABLE
         ORDER BY [USER_ID]))
ORDER BY [USER_ID]

10 ~ 20 的取法:

SELECT TOP 10 *
    FROM USER_TABLE
        WHERE ([USER_ID] NOT IN
           (SELECT TOP 10 [USER_ID]
                 FROM USER_TABLE
         ORDER BY [USER_ID]))
ORDER BY [USER_ID]

20 ~ 30 的取法:

SELECT TOP 10 *
    FROM USER_TABLE
        WHERE ([USER_ID] NOT IN
           (SELECT TOP 20 [USER_ID]
                 FROM USER_TABLE
         ORDER BY [USER_ID]))
ORDER BY [USER_ID]

2016年11月21日 星期一

在MS SQL 實現 Limit 的做法


SELECT * FROM  (SELECT *, ROW_NUMBER() OVER (ORDER BY META_ID) AS ROW FROM UMETADATA) A WHERE ROW > 10 and ROW <= 20 

2016年11月16日 星期三

SqlParameter 遇到 LIKE 寫法


SqlParameter 遇到 LIKE 寫法

 String _sql = @"SELECT TABLE_NAME FROM SDE_LAYERS WHERE OWNER = 'DBO' AND TABLE_NAME LIKE @thisMapName + '%' ORDER BY TABLE_NAME DESC";
                           
DataTable dt = new DataTable();

 //1.SqlConnection
 using (SqlConnection cn = new SqlConnection(_connstr))
{
           cn.Open();
           //2.SqlCommand
           using (SqlCommand cmd = new SqlCommand(_sql, cn))
           {
                      cmd.Parameters.Clear();
                      cmd.Parameters.AddWithValue("@thisMapName", thisMapName);
                      //3.SqlDataAdapter
                      using (SqlDataAdapter da = new SqlDataAdapter(cmd))
                      {
                                 dt.BeginLoadData();
                                 da.Fill(dt);
                      }
            }

            cn.Close();
}

2016年11月15日 星期二

Round & FormatNumber 四捨五入的區別

資料來源:Round & FormatNumber 四捨五入的區別


VBScript Round() 為四捨五入函數,但是 Round() 四捨五入為近偶取整,與我們平常用的並不一樣,平時是以大於等於 5 的數值進位、小於 5 的數值捨去。FormatNumber() 的值比較符合平常用的值。使用 Round() 近偶取整則於偶數才進位與 FormatNumber() 四捨五入有差異由下表來分析差異點。

C# 刪除資料夾、檔案

string TASK_ID = "pp_hsiao";
string serverDir = Request.PhysicalApplicationPath + "\\upload\\mapzipfile\\" + TASK_ID;//路徑

///檢查 「檔案」 是否存在
if (File.Exists(serverDir + "\\" + TASK_ID + ".xls"))
{
                            //如果檔案存在
                            File.Delete(serverDir + "\\" + TASK_ID + ".xls");//刪除檔案
}

///檢查 「資料夾」 是否在在
if (Directory.Exists(serverDir + "\\" + TASK_ID))
{
                            //如果檔案存在
                            Directory.Delete(serverDir + "\\" + TASK_ID);//刪除檔案
}

String.IsNullOrEmpty 判斷 NULL 或 空值

在開發程式的時候常常發現,以前的人,資料庫很愛用 NULL 這個值,但這樣常常會發生讀取時的錯誤!

所以,String 在接資料庫資料時,可以加入以下的判斷,防止跳出錯誤!

String META_ID = "";
META_ID = String.IsNullOrEmpty(dt.Rows[0]["METADATA"].ToString()) ? "" : dt.Rows[0]["METADATA"].ToString();

利用Server.mapPath()取得網站相關路徑

資料來源:
利用Server.mapPath()取得網站相關路徑(HttpContext.Current.Server.MapPath,路徑,根目錄,目前應用程式,上一層,類別,vb,HttpContext.Current.Server.MapPath))




'假設目前程式路徑:c:\Inetpub\wwwroot\myApp\subDir\subDir2\abc.aspx '取得目前應用程式路徑,ex:c:\Inetpub\wwwroot\myApp\ Dim aaa As String = Server.MapPath("~\") '取得目前應用程式路徑,ex:c:\Inetpub\wwwroot\myApp\ Dim bbb As String = Server.MapPath("~/") '取得目前應用程式路徑,ex:c:\Inetpub\wwwroot\myApp\ Dim fff As String = Server.MapPath("~") '取得根目錄路徑,ex:c:\Inetpub\wwwroot\ Dim ccc As String = Server.MapPath("/") '取得目前頁面路徑,ex:c:\Inetpub\wwwroot\myApp\subDir\subDir2\ Dim ddd As String = Server.MapPath("./") '取得目前頁面上一層路徑,ex:c:\Inetpub\wwwroot\myApp\subDir\ Dim eee As String = Server.MapPath("../") '取得目前頁面上上一層路徑,ex:c:\Inetpub\wwwroot\myApp\ Dim hhh As String = Server.MapPath("../../")