2008年11月28日 星期五

使用 MySQL command line 刪除資料庫

mysql>drop database yourDbName;

ps.記得加上"分號"命令才會送出

2008年11月21日 星期五

搜尋並轉換欄位值

假設有某個欄位,名稱為"MediaType"。是Integer型態,存放的資料範圍是1-4,對應如下:
1:文字
2:圖片
3:影片
4:PowerPoint

若要SELECT出MediaType欄位則,一般的SQL語法如下:
"SELECT MediaType FROM TempTable"



若要將裡面的值轉換成其他文字,則轉換的SQL語法如下:
"SELECT CASE MediaType When '1' Then '文字' "When '2' Then '圖片' When '3' Then '影片' When '4' Then 'PowerPoint' End AS MediaType FROM TempTable"

新增額外的判斷欄位至資料表

public class ConditionColumn
{
 private DataTable table = new DataTable();
 public void Main()
 {
  table.Columns.Add(new DataColumn("MediaType"));
  DataRow dr1 = table.NewRow();
  DataRow dr2 = table.NewRow();
  DataRow dr3 = table.NewRow();
  DataRow dr4 = table.NewRow();
  dr1["MediaType"] = 1;
  dr2["MediaType"] = 2;
  dr3["MediaType"] = 2;
  dr4["MediaType"] = 4;
  table.Rows.Add(dr1);
  table.Rows.Add(dr2);
  table.Rows.Add(dr3);
  table.Rows.Add(dr4);
 }

 public void AddColumn()
 {
  table.Columns.Add("Comment", System.Type.GetType("System.String"), "IIF(MediaType=1,'文字',IIF(MediaType=2,'圖片','未知'))");
 }
}

呼叫 AddColumn 前的資料表

呼叫 AddColumn 後的資料表

2008年11月12日 星期三

簡單的 .NET Remoting 使用範例 (2)

當Server的服務完成之後,接下來就是Client的部分
Client也分成兩個檔案,為:Remote.cs 及 Client.cs

Remote.cs

namespace Remote
{
  interface IRemoteObject
  {
    bool Login(string account, string password);
   }
}

這裡Client端的Remote.cs 與 Server端的Remote.cs 最大的差別就是
,Client端的Remote.cs 只需要 IRemoteObject 而不需要實作,
這也表示Client端不會知道Server端是如何實作IRemoteObject 所宣告的功能
,而我們要的安全性也就達成了。

有一個必須要注意的地方,就是Client端與Server端的IRemoteObjectnamespace命名必須相同

Client.cs

using Remote;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;

namespace Client
{
 public class CClient
 {
  private string serverIP = "192.168.1.100"; //Server的IP

  public void Test()
  {
   TcpChannel channel = new TcpChannel(); //建立通道但不指定port

   try
   {
    //註冊通道,系統會自動找未使用的port
    ChannelServices.RegisterChannel(channel, false);
    //經由"tcp://192.168.1.100:7181/ServiceMessage"
    //從Server端實做一個IRemoteObject型態的物件
    IRemoteObject remoteObj = (IRemoteObject)Activator.GetObject(typeof(IRemoteObject), string.Format("tcp://{0}:7181/ServiceMessage", serverIP));

    //物件建立完成後,就可像一般物件一樣使用
    if(remoteObj.Login("root","1234")
     MessageBox.Show("登入成功");
    else
     MessageBox.Show("登入失敗");
   }
   catch
   {
    MessageBox.Show("無法與伺服器建立連線");
   }
   finally
   {
    ChannelServices.UnregisterChannel(channel);
   }
  }  
 }
}

當4個檔案都建立完成後,我們可以經由
Server 的 StartService() 方法來啟動服務
而使用 Client 的 Test() 方法來測試

簡單的 .NET Remoting 使用範例 (1)

在寫應用程式時,常常需要實作一些存取Server資料庫的行為
有時候我們會從ClientAP直接連接到Server的資料庫去作存取的動作

可是這麼作似乎有些安全性的考量,因此我們希望能夠透過一支中介的程式才幫我們做資料庫的存取,這時候使用 .Net Remoting 可以是一個簡單的解決方式。

程式的原始檔,總共分為4個。其中,Client端 與 Server端各占兩個。

在Server端的部分需要如下的檔案:

Remote.cs

using System;
using System.Data;
using MySql.Data.MySqlClient;

namespace Remote
{
 interface IRemoteObject
 {
  bool Login(string account, string password);
 }

 public class RemoteObject : MarshalByRefObject, IRemoteObject
 {
  #region IRemoteObject 成員

  bool IRemoteObject.Login(string account, string password)
  {
   //方法的實作
   //return true or false
   .
   .
   .   
  }
  
  #endregion
 }
}

這檔案中實作Login方法的地方並不是重點,
重點是我們宣告了一個 IRemoteObject 並且實作它。
實做出來的類別必須繼承 MarshalByRefObject

Server端的第2個檔案
Server.cs

using Remote;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;

namespace Server
{
 public class CServer
 {
  private TcpChannel channel = null;

  public void StartService()
  {
   try
   {
    //先建立一個通道。7181是port 可以隨便打
    channel = new TcpChannel(7181);
    //跟系統註冊此通道
    ChannelServices.RegisterChannel(channel);
    //跟系統註冊RemoteObject,及RemoteObject的路徑是ServiceMessage
    RemotingConfiguration.RegisterWellKnownServiceType(typeof(RemoteObject), "ServiceMessage", WellKnownObjectMode.SingleCall);
   }
   catch
   {
    MessageBox.Show("無法啟動服務");
    ChannelServices.UnregisterChannel(channel);
   }
  }
 }
}