10/10/2013

Send E-Mail via Hostgator WindowsShared Hosting

  1. 先在 Hostgator Back-end 中, Create 一個 email account
  2. 在 DNS 上做好 Domain Mapping, 確定該 email 可以被存取到.
  3. 在 ASP.net 的 Page 使用下列 Code
MailMessage oMail = new System.Net.Mail.MailMessage();
oMail.From = new MailAddress([mailfrom]);
oMail.To.Add([mailto]);
oMail.Subject = [subject];
oMail.IsBodyHtml = true;
oMail.BodyEncoding = Encoding.UTF8;
oMail.Priority = MailPriority.Normal;
oMail.Body = [content];

SmtpClient smtp = new SmtpClient();
smtp.Host = [your domain]; // ex, abc.barksortware.com
smtp.Port = 26;
 

// mailid: should include domain, ex, info@abc.barksoftware.com
smtp.Credentials = new System.Net.NetworkCredential(mailid, mailpw);

smtp.Send(oMail);

7/10/2013

Get size of all tables in MSSQL 2005 database

Original Doc: http://stackoverflow.com/questions/7892334/get-size-of-all-tables-in-database

SELECT 
    t.NAME AS TableName,
    p.rows AS RowCounts,
    SUM(a.total_pages) * 8 AS TotalSpaceKB, 
    SUM(a.used_pages) * 8 AS UsedSpaceKB, 
    (SUM(a.total_pages) - SUM(a.used_pages)) * 8 AS UnusedSpaceKB
FROM 
    sys.tables t
INNER JOIN      
    sys.indexes i ON t.OBJECT_ID = i.object_id
INNER JOIN 
    sys.partitions p ON i.object_id = p.OBJECT_ID AND i.index_id = p.index_id
INNER JOIN 
    sys.allocation_units a ON p.partition_id = a.container_id
WHERE 
    t.NAME NOT LIKE 'dt%' 
    AND t.is_ms_shipped = 0
    AND i.OBJECT_ID > 255 
GROUP BY 
    t.Name, p.Rows
ORDER BY 
    t.Name

5/15/2013

Similar Text

在 Programming 的時候, 有很多地方我們需要用到字串相似度的檢驗, 在 PHP 裡有現成的 Function 可以使用, 但是在其他地方就未必.

這裡有一個 JavaScript 的 Implementation. Click here!

在 MS SQL 上, 我根據這個 JavaScript 改了一個版本, 勉強可以使用


CREATE FUNCTION [dbo].[similar_text]
(
    @first   NVARCHAR(100),
    @second  NVARCHAR(100),
    @percent BIT = 1
)
RETURNS DECIMAL(10,3)
AS
BEGIN
    DECLARE @ret DECIMAL(10,3);
  
    DECLARE @s1 NVARCHAR(100);
    DECLARE @s2 NVARCHAR(100);
  
    DECLARE @pos1 INT;
    DECLARE @pos2 INT;
    DECLARE @max  INT;
    DECLARE @fl1  INT;
    DECLARE @fl2  INT;
    DECLARE @p    INT;
    DECLARE @q    INT;
    DECLARE @l    INT;
    DECLARE @sum  INT;

    SET @s1 = ISNULL(@first,'');
    SET @s2 = ISNULL(@second,'');
  
    IF (@s1='') OR (@s2='') BEGIN
        SET @ret = 0.00;
    END
    ELSE BEGIN
        SET @pos1 = 1;
        SET @pos2 = 1;
        SET @max  = 1;  
        SET @fl1  = len(@s1);
        SET @fl2  = len(@s2);
        SET @p    = 1;
      
        WHILE (@p<=@fl1) BEGIN
            SET @q = 1;
            WHILE (@q<=@fl2) BEGIN
                SET @l = 1;
                WHILE (@p+@l<=@fl1) AND (@q+@l<=@fl2) AND (SUBSTRING( @s1,@p+@l,1)=SUBSTRING( @s2,@q+@l,1)) BEGIN
                    IF (@l>=@max) BEGIN
                        SET @max = @l;
                        SET @pos1 = @p;
                        SET @pos2 = @q;
                    END;
                    SET @l = @l+1;
                END;
                SET @q = @q+1;
            END;
            SET @p = @p+1;
        END;
      
        SET @sum = @max;
        IF (@sum>1) BEGIN
            IF (@pos1>=1) AND (@pos2>=1) BEGIN
                SET @sum = @sum + dbo.similar_text(SUBSTRING(@s1,1,@pos2),SUBSTRING(@s2,1,@pos2),0);
            END;
          
            IF (@pos1+@max<=@fl1) AND (@pos2+@max<=@fl2) BEGIN
                SET @sum = @sum + dbo.similar_text(SUBSTRING(@s1,@pos1+@max,@fl1-@pos1-@max),SUBSTRING(@s2,@pos2+@max,@fl2-@pos2-@max),0);
            END;      
        END;
      
        IF (@percent=0)
            SET @ret = @sum;
        ELSE
            SET @ret = (@sum * 200.00) / (@fl1 + @fl2);
    END;
  
    RETURN @ret;
END


GO

3/08/2013

How to get IPV4 from IPV6 Environment?

Reference Link: http://www.4guysfromrolla.com/articles/071807-1.aspx  

using System;
using System.Net;

public class IPNetworking
{
  public static string GetIP4Address()
  {
    string IP4Address = String.Empty;

    foreach (IPAddress IPA in Dns.GetHostAddresses(HttpContext.Current.Request.UserHostAddress))
    {
      if (IPA.AddressFamily.ToString() == "InterNetwork")
      {
        IP4Address = IPA.ToString();
        break;
      }
    }

    if (IP4Address != String.Empty)
    {
      return IP4Address;
    }

    foreach (IPAddress IPA in Dns.GetHostAddresses(Dns.GetHostName()))
    {
      if (IPA.AddressFamily.ToString() == "InterNetwork")
      {
        IP4Address = IPA.ToString();
        break;
      }
    }

    return IP4Address;
  }
}

2/26/2013

SQL Server Backup File 太大, 無法 Copy 回來 Restore.

這次碰到的狀況是 SQL Server 的 Backup File 太大, 想要 Copy 到 Local 來做 Restore 不可行. 使用 UNC 透過網路來 Restore 也失敗, 所以想到一個方法, 因為 SQL Server 的 backup file 壓縮率都很高, 好幾百 MB 壓到最後可能只有 20MB. 所以先把 backup file 壓縮起來 Copy 到 local, 然後用 WinMount 這個軟體把 Compressed 檔案 Mount 進來當作一個 Hard Drive, 然後再讓 MS SQL 去做 Restore. 這樣就可以解決 Backup File 太大無法 Copy 回來 Restore 的問題.

1/21/2013

vboxdrv setup 出現不能 Build 的問題

有時候 Linux 更新 kernel 之後, VirtualBox 需要重新 Build 部份 module.

有時後會出現如下的 Message,

* Stopping VirtualBox kernel modules [ OK ]
* Recompiling VirtualBox kernel modules
* Look at /var/log/vbox-install.log to find out what went wrong

再檢查 vbox-install.log, 裡面訊息如下

Makefile:181: *** Error: unable to find the sources of your current Linux kernel. Specify KERN_DIR=<directory> and run Make again. Stop.

表示有些 Kernel 的 Source 沒有安裝, 或是被清掉了. 用下免指令解決,

sudo apt-get install dkms build-essential linux-headers-generic