Allow multiple statements in MySQL ODBC

August 7th, 2009 Phanix

While using MySQL ODBC to execute sql statements, only single sql statement is allowed for execution by default. If you want to execute multiple sql statements at once, you have to enable the multiple statements setting in MySQL ODBC.

mysql setting  ODBC multiple statement (by Phanix)

[C#, .NET] Run a program in tray (minimized window and no-shown in taskbar) with right clicked pop-up menu

July 30th, 2009 Phanix

程式執行後以最小化的方式執行,即不會出現在工作列上,而會出現在tray裡頭。而在 tray icon 上可以點滑鼠右鍵來彈出功能表單(menu),進行各功能選擇(如結束等)。

Read the rest of this entry / 繼續閱讀 »

.net 無視窗背景執行的程式

July 15th, 2009 Phanix

有時候為了要偷偷做一些事情, 所以會想要讓程式以無視窗在背景執行, 而要做出這樣的程式其實方法很簡單

  1. 在建立專案的時候選擇「主控台應用程式」(console application)
  2. 設定專案屬性(properties), 將輸出類型改為「windows 應用程式」

這樣就可以了, 當發現到這樣做就搞定的時候還有點訝異怎麼這麼容易…

C# 建立ODBC DSN 連線到 SQL Server

July 1st, 2009 Phanix

那個\0很重要。

using System.Runtime.InteropServices;

public bool DoDBConnect(string strDSN,string strUID,string strPWD)
{
	try
	{
		strConn = "DSN=" + strDSN + ";UID=" + strUID + ";PWD=" + strPWD + ";";
		cn = new OdbcConnection(strConn);
		cmd = new OdbcCommand();
		adp = new OdbcDataAdapter();

		cmd.CommandType = CommandType.Text;
		cmd.Connection = cn;
	}
	catch
	{
		return false;
	}

	return true;
}

[
DllImport("ODBCCP32.dll")
]
private static extern bool SQLConfigDataSource(IntPtr hwndParent, int fRequest, string lpszDriver, string lpszAttributes);

public bool CreateODBCDSN(string strDSN, string strServer, string strDB, string strDescription)
{
	string strSQL = "SERVER=" + strServer + "\0";
	strSQL += "DESCRIPTION=" + strDescription + "\0";
	strSQL += "DSN=" + strDSN + "\0";
	strSQL += "DATABASE=" + strDB + "\0";
	strSQL += "trusted_connection=no\0";
	return SQLConfigDataSource((IntPtr)0, 4, "SQL Server", strSQL);
}

在C#中define struct並以 .dll 方式給C++用

June 30th, 2009 Phanix

原本以為在 C# 中使用 struct 又要匯出成 .dll 給 c++ 需要有什麼特殊處理,結果發現其實不用…

namespace TESTCOMObject
{
    //其他程式碼
    //...

    public struct tag_Data
    {
        public string strName;
        public string strValue;
    }
}

而在 C++ 裡頭要使用就只要 import 之後就可以放心用了

TESTCOMObject::tag_Data aa[50];
aa[7].strName = "xxabc";
aa[7].strValue = "oo";
aa[17].strName = "xxabcyy";
aa[17].strValue = "oo";

TESTCOMObject::tag_Data bb;
bb.strName = "hahaha";
bb.strValue = "hehehe";

Execute Batch file in Silent mode (no cmd window) / 以無 dos 命令列視窗執行 .bat

June 29th, 2009 Phanix

有點繁瑣的方式,全部需要三個檔案來完成。

Read the rest of this entry / 繼續閱讀 »

Using XPath to select nodes with namespace in C# / 在C#中用XPath選取具有namespace之節點

May 9th, 2009 Phanix

處理 XML 文件資料時,利用 XPath 來選取文件中節點是蠻常見的方式,可是當這個節點是具有 namespace 時,該怎麼辦呢?

Read the rest of this entry / 繼續閱讀 »

[Note] Import Data in SQL Server 2005 (匯入資料)

January 9th, 2009 Phanix

Remeber to check the item “Integration Services” (SSIS) in the install process, or you will fail and get a error message like “product level is insufficient for components” while importing data from txt, excel, etc. files into a SQL server 2005 database.

Related: porduct level is insufficient for components

還是改用Regular expression好了

December 19th, 2008 Phanix

既然都有誤斬忠良的時候… 那 extract html text content 還是用 regular expression 好了,然後再特殊處理一下 <script> 和 <style>…

int i, j;
i = tb1.Text.ToLower().IndexOf("<script");
while (i >= 0)
{
    j = tb1.Text.ToLower().IndexOf("</script>", i);
    tb1.Text = tb1.Text.Substring(0, i) + tb1.Text.Substring(j + 9);
    i = tb1.Text.ToLower().IndexOf("<script");
}

i = tb1.Text.ToLower().IndexOf("<style");
while (i >= 0)
{
    j = tb1.Text.ToLower().IndexOf("</style>", i);
    tb1.Text = tb1.Text.Substring(0, i) + tb1.Text.Substring(j + 8);
    i = tb1.Text.ToLower().IndexOf("<style");
}
tb1.Text = Regex.Replace(tb1.Text, "<[^>]*>", " ");

[Memo] 整理一下最近寫程式用到的東西

December 4th, 2008 Phanix

免得以後要找很麻煩。都是 C# 的程式。

Threading 中作 output 到 textbox 中

delegate void SetTextCallback(TextBox tb, string text);

private void SetText(TextBox tb, string text)
{
    if (tb.InvokeRequired)
    {
        SetTextCallback d = new SetTextCallback(SetText);
        this.Invoke(d, new object[] { tb, text });
    }
    else
    {
        tb.Text = text;
    }
}

Extract text in <body> tag

strpage = ""; // Store The HTML Source
strtext = "";

// only fetch text between <body>
ibodystart = strpage.ToLower().IndexOf("<body");
ibodyend = strpage.ToLower().IndexOf("</body>");

if (ibodystart < 0) return;
if (ibodyend < 0) ibodyend = strpage.Length;

// j and k are used to quote text between continous tags
j = strpage.IndexOf(">", ibodystart);

sw = new StreamWriter([FILENAME], false, Encoding.UTF8);

#region filter out html tags, css and scripts, and then just keep plaintext
while (j > 0 && j < ibodyend)
{
    // j and k are used to quote text between continous tags
    k = strpage.IndexOf("<", j);
    
    
    // read text between tags, and store in strtmp
    if (k < 0)
    {
        strtmp = strpage.Substring(j + 1);
    }
    else
    {
        strtmp = strpage.Substring(j + 1, k - j - 1);
    }
    
    
    strtmp = HttpUtility.HtmlDecode(strtmp).Trim();
    
    // concate strtext and strtmp
    if (strtmp != "")
    {
        if (strtext == "")
        {
            sw.WriteLine(strtmp);
            strtext = strtmp;
        }
        else
        {
            sw.WriteLine(" " + strtmp);
        }
    }
    
    // find out next j
    if (k < 0)
    {
        j = -1;
    }
    else
    {
        //check comment
        if (strpage.Substring(k).Length <= 7)
        {
            j = -1;
        }
        else if (strpage.Substring(k, 4) == "<!--")
        {
            j = strpage.IndexOf("-->", k);
            if (j >= 0)
            {
                j = strpage.IndexOf(">", j);
            }
        }
        else if (strpage.ToLower().Substring(k, 7) == "<script")
        {
            j = strpage.ToLower().IndexOf("</script>", k);
            if (j >= 0)
            {
                j = strpage.IndexOf(">", j);
            }
        }
        else if (strpage.ToLower().Substring(k, 6) == "<style")
        {
            j = strpage.ToLower().IndexOf("</style>", k);
            if (j >= 0)
            {
                j = strpage.IndexOf(">", j);
            }
        }
        else
        {
            j = strpage.IndexOf(">", k);
        }
    }
}
#endregion

sw.Close();

Execute the other .exe with parameters from command line (without showing the window). This example uses WordNet.

Process p = new Process();
string strwn1, strwn2;

#region Call wn.exe for wordnet hypernym
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.CreateNoWindow = true;

// word 1
p.StartInfo.FileName = @"C:\Program Files\WordNet\2.1\bin\wn.exe";
p.StartInfo.Arguments = @"" + w1 + " -hypen"; // w1 is a word

p.Start();

strwn1 = p.StandardOutput.ReadToEnd();

p.WaitForExit();

// word 2
p.StartInfo.FileName = @"C:\Program Files\WordNet\2.1\bin\wn.exe";
p.StartInfo.Arguments = @"" + w2 + " -hypen"; // w2 is the other word

p.Start();

strwn2 = p.StandardOutput.ReadToEnd();

p.WaitForExit();

#endregion