Sunday, November 30, 2008

各種資料庫連線字串範例

ODBC DSNLess connection(不需要 DSN 的連線)
MS Access ODBC DSNless connection
Driver={Microsoft Access Driver (*.mdb)};Dbq=c:\somepath\dbname.mdb;Uid=Admin;Pwd=pass;


dBase ODBC DSNless connection
Driver={Microsoft dBASE Driver (*.dbf)};DriverID=277;Dbq=c:\somepath\dbname.dbf;


Oracle ODBC DSNless connection
Driver={Microsoft ODBC for Oracle};Server=OracleServer.world;Uid=admin;Pwd=pass;


MS SQL Server DSNless connection
Driver={SQL Server};Server=servername;Database=dbname;Uid=sa;Pwd=pass;


MS Text Driver DSNless connection
Driver={Microsoft Text Driver (*.txt; *.csv)};Dbq=c:\somepath\;Extensions=asc,csv,tab,txt;Persist Security Info=False;


Visual Foxpro DSNless connection
Driver={Microsoft Visual FoxPro Driver};SourceType=DBC;SourceDB=c:\somepath\dbname.dbc;Exclusive=No;


MySQL DSNless connection
driver={mysql}; database=yourdatabase;uid=username;pwd=password;option=16386;

==================================================================

OLE DB 連線
MS Access OLE DB connection
Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\somepath\dbname.mdb;User Id=admin;Password=pass;


Oracle OLE DB connection
Provider=OraOLEDB.Oracle;Data Source=dbname;User Id=admin;Password=pass;


MS SQL Server OLE DB connection
Provider=SQLOLEDB;Data Source=machineName;Initial Catalog=dbname;User ID=sa;Password=pass;


MS Text Driver OLE DB connection
"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\yourpath;Extended Properties='text;FMT=Delimited'"

Tuesday, November 25, 2008

停用 Connection Pooling 機制

ADO.NET本身內建了連線管理機制,Connection Pooling,它會為應用程式所開啟的連線自動進行管理,提供連線集區統籌分配所需的連線,並且在適當的時機對其進行回收。Connection Pooling 是個不錯的構想,它特別適合用來處理大型的分散式資料庫系統,但是有一好就有一壞,在某些小型的資料庫系統當中,特別是那種獨立的桌上型小型資料庫系統,這個機制事實上並不需這種機制,在這種情形下,你可以選擇將其停用。Connection Pooling 在預設的情形下是開啟,如果你想要停用這個機制,一個很直接的方法如下:Data Source=myServerAddress;Initial Catalog=myDataBase;Integrated Security=SSPI;Pooling = False ; 在連線字串中,將其Pooling 屬性設為Flase即可。

如何從檔案讀取 XML

參考資料來源:http://support.microsoft.com/kb/307548/zh-tw

這個範例使用了一個名為 Books.xml 的檔案。您可以自行建立 Books.xml 檔案,或者使用下列資料夾中 .NET Software Development Kit (SDK) QuickStart 所附的範例檔案:
\Program Files\Microsoft Visual Studio .NET\FrameworkSDK\Samples\QuickStart\Howto\Samples\Xml\Transformxml\Cs
您必須將 Books.xml 複製到 [\Bin\Debug] 資料夾,該資料夾位在建立這個專案的資料夾之下。您也可以下載這個 Books.xml 檔案,下載的位置請參閱 〈參考〉 一節。
開啟 Visual Studio .NET。
建立一個新的 Visual C# .NET「主控台應用程式」(Console Application)。您可以直接到 〈完整程式碼清單〉 一節或是逐步進行下列步驟,建置這個應用程式。
請確定專案含有 System.Xml.dll 組件的參考。
在 System.Xml 命名空間上,指定 using 指示詞,您就無需日後在程式碼中取得 XmlTextReader 宣告資格。您必須將 using 指示詞使用在任何其他宣告之前。
using System.Xml;

建立一個 XmlTextReader 物件的執行個體 (Instance),並且將它填入 XML 檔案。通常, XmlTextReader 類別是使用於必須存取 XML 作為未經處理資料 (Raw Data),但不希望有 DOM 之額外負荷的情況下;因此, XmlTextReader 類別提供了一種更快速讀取 XML 的機制。 XmlTextReader 類別擁有不同的建構函式,用來指定 XML 資料的位置。下列程式碼會建立一個 XmlTextReader 類別的執行個體並且載入 Books.xml 檔案。請將下列程式碼加入到 Class1 的 Main 程序。
XmlTextReader reader = new XmlTextReader ("books.xml");

讀取 XML (請注意,這個步驟是示範外層的「while」迴圈,而下兩個步驟則是示範如何使用這個迴圈讀取 XML)。建立好 XmlTextReader 物件之後,請使用 Read 方法讀取 XML 資料。 Read 方法會在 XML 檔案中依序進行,直到它碰到檔案結尾為止,這時 Read 方法就會傳回一個「False」值。
while (reader.Read())
{
// Do some work here on the data.
Console.WriteLine(reader.Name);
}
Console.ReadLine();

檢查節點。為了處理 XML 資料,每筆記錄都有一個可以由 NodeType 屬性決定的節點型別。 Name 和 Value 屬性 (Property) 會傳回目前節點 (或記錄) 的節點名稱 (項目和屬性 (Attribute) 名稱) 和節點值 (節點文字)。 NodeType 列舉型別 (Enumeration) 決定了節點的型別。下列範例程式碼會顯示項目的名稱和文件類型。請注意這個範例不考慮項目屬性 (Attribute)。
while (reader.Read())
{
switch (reader.NodeType)
{
case XmlNodeType.Element: // The node is an element.
Console.Write("<" + reader.Name); Console.WriteLine(">");
break;
case XmlNodeType.Text: //Display the text in each element.
Console.WriteLine (reader.Value);
break;
case XmlNodeType. EndElement: //Display the end of the element.
Console.Write("");
break;
}
}

檢查這些屬性 (Attribute)。項目節點型別可以包括與它們關聯的節點屬性清單。 MovetoNextAttribute 方法會依序移經項目中的每一個屬性。使用 HasAttributes 屬性 (Property),測試節點是否有任何屬性 (Attribute)。 AttributeCount 屬性 (Property) 會傳回目前節點的屬性 (Attribute) 數目。
while (reader.Read())
{
switch (reader.NodeType)
{
case XmlNodeType.Element: // The node is an element.
Console.Write("<" + reader.Name); while (reader.MoveToNextAttribute()) // Read the attributes. Console.Write(" " + reader.Name + "='" + reader.Value + "'"); Console.Write(">");
Console.WriteLine(">");
break;
case XmlNodeType.Text: //Display the text in each element.
Console.WriteLine (reader.Value);
break;
case XmlNodeType. EndElement: //Display the end of the element.
Console.Write("");
break;
}
}

儲存並關閉專案。
回此頁最上方
完整程式碼清單
using System;
using System.Xml;

namespace ReadXMLfromFile
{
///
/// Summary description for Class1.
///

class Class1
{
static void Main(string[] args)
{
XmlTextReader reader = new XmlTextReader ("books.xml");
while (reader.Read())
{
switch (reader.NodeType)
{
case XmlNodeType.Element: // The node is an element.
Console.Write("<" + reader.Name); Console.WriteLine(">");
break;
case XmlNodeType.Text: //Display the text in each element.
Console.WriteLine (reader.Value);
break;
case XmlNodeType.EndElement: //Display the end of the element.
Console.Write("");
break;
}
}
Console.ReadLine();
}
}
}

如何在 64 位元版的 Windows 上切換 32 位元版的 ASP.NET 1.1 與 64 位元版的 ASP.NET 2.0

參考超結連:http://support.microsoft.com/kb/894435/zh-tw

IIS 6.0 同時支援 32 位元模式及 64 位元模式。但是,IIS 6.0 不支援同時在 64 位元版的 Windows 上執行兩種模式。ASP.NET 1.1 只能在 32 位元模式中執行。ASP.NET 2.0 可以在 32 位元模式或 64 位元模式中執行。因此,如果要同時執行 ASP.NET 1.1 和 ASP.NET 2.0,您必須在 32 位元模式中執行 IIS。在不同版本的 ASP.NET 之間切換前,請先造訪下列 Microsoft Developer Network (MSDN) 網站,以下載並安裝 ASP.NET 1.1 和 ASP.NET 2.0:
http://msdn2.microsoft.com/en-us/netframework/aa731542.aspx (http://msdn2.microsoft.com/en-us/netframework/aa731542.aspx) 例如,如果您執行的是 Microsoft Windows x64 Edition,請下載下列兩個可轉散發套件:
.NET Framework 2.0 版可轉散發套件 x64 (64 位元)
.NET Framework 1.1 版可轉散發套件安裝可轉散發套件之後,您就可以在不同版本的 ASP.NET 之間切換。如果要執行這項操作,請針對每個 ASP.NET 版本執行下列步驟:
ASP.NET 1.1 的 32 位元版本
loadTOCNode(3, 'moreinformation');

如果要執行 32 位元版的 ASP.NET 1.1,請依照下列步驟執行:
按一下 [開始],再按一下 [執行],輸入 cmd,然後按一下 [確定]。
輸入下列命令以啟用 32 位元模式:
cscript %SYSTEMDRIVE%\inetpub\adminscripts\adsutil.vbs SET W3SVC/AppPools/Enable32bitAppOnWin64 1
輸入下列命令以安裝 ASP.NET 1.1 的版本,以及在 IIS 根目錄和下列位置底下安裝指令碼對應:
%SYSTEMROOT%\Microsoft.NET\Framework\v1.1.4322\aspnet_regiis.exe -i
請確定在 Internet Information Services Manager 的 Web Service Extension 清單中,將 ASP.NET 1.1.4322 版的狀態設定為 Allowed。
ASP.NET 2.0 的 32 位元版本
loadTOCNode(3, 'moreinformation');

如果要執行 32 位元版的 ASP.NET 2.0,請依照下列步驟執行:
按一下 [開始],再按一下 [執行],輸入 cmd,然後按一下 [確定]。
輸入下列命令以啟用 32 位元模式:
cscript %SYSTEMDRIVE%\inetpub\adminscripts\adsutil.vbs SET W3SVC/AppPools/Enable32bitAppOnWin64 1
輸入下列命令以安裝 ASP.NET 2.0 (32 位元) 的版本,以及在 IIS 根目錄和下列位置底下安裝指令碼對應:
%SYSTEMROOT%\Microsoft.NET\Framework\v2.0.40607\aspnet_regiis.exe -i
請確定在 Internet Information Services Manager 的 Web Service Extension 清單中,將 ASP.NET 2.0.40607 版 (32 位元) 的狀態設定為 Allowed。
ASP.NET 2.0 的 64 位元版本
loadTOCNode(3, 'moreinformation');

如果要執行 64 位元版的 ASP.NET 2.0,請依照下列步驟執行:
按一下 [開始],再按一下 [執行],輸入 cmd,然後按一下 [確定]。
輸入下列命令以停用 32 位元模式:
cscript %SYSTEMDRIVE%\inetpub\adminscripts\adsutil.vbs SET W3SVC/AppPools/Enable32bitAppOnWin64 0
輸入下列命令以安裝 ASP.NET 2.0 的版本,以及在 IIS 根目錄和下列位置底下安裝指令碼對應:
%SYSTEMROOT%\Microsoft.NET\Framework64\v2.0.40607\aspnet_regiis.exe -i
請確定在 Internet Information Services Manager 的 Web Service Extension 清單中,將 ASP.NET 2.0.40607 版的狀態設定為 Allowed。注意 ASP.NET 2.0 的組建版本可能依目前發行的組建版本而有所不同。上述步驟適用於組建版本 2.0.40607。
回此頁最上方
Windows x64 版本的技術支援
loadTOCNode(2, 'moreinformation');

您的硬體製造商會提供有關 Microsoft Windows x64 版本的技術支援和協助。由於 Windows x64 版本是隨附在硬體中,所以您的硬體製造商會提供相關支援。您的硬體製造商可能已經利用特殊的元件自訂 Windows x64 版本安裝。特殊的元件可能包括了特定裝置驅動程式,或者可能包含了能夠使硬體發揮最大效能的選用設定。如果您需要有關 Windows x64 版本的技術協助,Microsoft 將會在合理的情況下提供協助。不過,您可能必須直接連絡製造商。因為硬體製造商已在硬體上預先安裝軟體,所以他們會是提供軟體技術支援服務的最佳人選。如需有關 Microsoft Windows XP Professional x64 Edition 的產品資訊,請造訪下列 Microsoft 網站:
http://www.microsoft.com/windowsxp/64bit/default.mspx (http://www.microsoft.com/windowsxp/64bit/default.mspx) 如需有關 Microsoft Windows Server 2003 x64 Edition 的產品資訊,請造訪下列 Microsoft 網站:
http://www.microsoft.com/windowsserver2003/64bit/x64/default.mspx (http://www.microsoft.com/windowsserver2003/64bit/x64/default.mspx)

Monday, November 3, 2008

SQL: ALTER TABLE Statement

The ALTER TABLE statement allows you to rename an existing table. It can also be used to add, modify, or drop a column from an existing table.
Renaming a table
The basic syntax for renaming a table is:
ALTER TABLE table_name RENAME TO new_table_name;
For example:
ALTER TABLE suppliers RENAME TO vendors;
This will rename the suppliers table to vendors.
Adding column(s) to a table
Syntax #1
To add a column to an existing table, the ALTER TABLE syntax is:
ALTER TABLE table_name ADD column_name column-definition;
For example:
ALTER TABLE supplier ADD supplier_name varchar2(50);
This will add a column called supplier_name to the supplier table.
Syntax #2
To add multiple columns to an existing table, the ALTER TABLE syntax is:
ALTER TABLE table_name
ADD (
column_1
column-definition,

column_2
column-definition,

...

column_n
column_definition );
For example:
ALTER TABLE supplier
ADD (
supplier_name
varchar2(50),

city
varchar2(45) );
This will add two columns (supplier_name and city) to the supplier table.
Modifying column(s) in a table
Syntax #1
To modify a column in an existing table, the ALTER TABLE syntax is:
ALTER TABLE table_name MODIFY column_name column_type;
For example:
ALTER TABLE supplier MODIFY supplier_name varchar2(100) not null;
This will modify the column called supplier_name to be a data type of varchar2(100) and force the column to not allow null values.
Syntax #2
To modify multiple columns in an existing table, the ALTER TABLE syntax is:
ALTER TABLE table_name
MODIFY (
column_1
column_type,

column_2
column_type,

...

column_n
column_type );
For example:
ALTER TABLE supplier
MODIFY (
supplier_name
varchar2(100)
not null,
city
varchar2(75)

);
This will modify both the supplier_name and city columns.
Drop column(s) in a table
Syntax #1
To drop a column in an existing table, the ALTER TABLE syntax is:
ALTER TABLE table_name DROP COLUMN column_name;
For example:
ALTER TABLE supplier DROP COLUMN supplier_name;
This will drop the column called supplier_name from the table called supplier.
Rename column(s) in a table(NEW in Oracle 9i Release 2)
Syntax #1
Starting in Oracle 9i Release 2, you can now rename a column.
To rename a column in an existing table, the ALTER TABLE syntax is:
ALTER TABLE table_name RENAME COLUMN old_name to new_name;
For example:
ALTER TABLE supplier RENAME COLUMN supplier_name to sname;
This will rename the column called supplier_name to sname.
Acknowledgements: Thanks to Dave M., Craig A., and Susan W. for contributing to this solution!
Practice Exercise #1:
Based on the departments table below, rename the departments table to depts.
CREATE TABLE departments
(
department_id
number(10)
not null,

department_name
varchar2(50)
not null,

CONSTRAINT departments_pk PRIMARY KEY (department_id)
);
Solution:
The following ALTER TABLE statement would rename the departments table to depts:
ALTER TABLE departments RENAME TO depts;
Practice Exercise #2:
Based on the employees table below, add a column called salary that is a number(6) datatype.
CREATE TABLE employees
(
employee_number
number(10)
not null,

employee_name
varchar2(50)
not null,

department_id
number(10),

CONSTRAINT employees_pk PRIMARY KEY (employee_number)
);
Solution:
The following ALTER TABLE statement would add a salary column to the employees table:
ALTER TABLE employees ADD salary number(6);
Practice Exercise #3:
Based on the customers table below, add two columns - one column called contact_name that is a varchar2(50) datatype and one column called last_contacted that is a date datatype.
CREATE TABLE customers
(
customer_id
number(10)
not null,

customer_name
varchar2(50)
not null,

address
varchar2(50),

city
varchar2(50),

state
varchar2(25),

zip_code
varchar2(10),

CONSTRAINT customers_pk PRIMARY KEY (customer_id)
);
Solution:
The following ALTER TABLE statement would add the contact_name and last_contacted columns to the customers table:
ALTER TABLE customers
ADD (
contact_name
varchar2(50),

last_contacted
date );
Practice Exercise #4:
Based on the employees table below, change the employee_name column to a varchar2(75) datatype.
CREATE TABLE employees
(
employee_number
number(10)
not null,

employee_name
varchar2(50)
not null,

department_id
number(10),

CONSTRAINT employees_pk PRIMARY KEY (employee_number)
);
Solution:
The following ALTER TABLE statement would change the datatype for the employee_name column to varchar2(75):
ALTER TABLE employees MODIFY employee_name varchar2(75);
Practice Exercise #5:
Based on the customers table below, change the customer_name column to NOT allow null values and change the state column to a varchar2(2) datatype.
CREATE TABLE customers
(
customer_id
number(10)
not null,

customer_name
varchar2(50),


address
varchar2(50),

city
varchar2(50),

state
varchar2(25),

zip_code
varchar2(10),

CONSTRAINT customers_pk PRIMARY KEY (customer_id)
);
Solution:
The following ALTER TABLE statement would modify the customer_name and state columns accordingly in the customers table:
ALTER TABLE customers
MODIFY (
customer_name
varchar2(50) not null,

state
varchar2(2) );
Practice Exercise #6:
Based on the employees table below, drop the salary column.
CREATE TABLE employees
(
employee_number
number(10)
not null,

employee_name
varchar2(50)
not null,

department_id
number(10),

salary
number(6),

CONSTRAINT employees_pk PRIMARY KEY (employee_number)
);
Solution:
The following ALTER TABLE statement would drop the salary column from the employees table:
ALTER TABLE employees DROP COLUMN salary;
Practice Exercise #7:
Based on the departments table below, rename the department_name column to dept_name.
CREATE TABLE departments
(
department_id
number(10)
not null,

department_name
varchar2(50)
not null,

CONSTRAINT departments_pk PRIMARY KEY (department_id)
);
Solution:
The following ALTER TABLE statement would rename the department_name column to dept_name in the departments table:
ALTER TABLE departments RENAME COLUMN department_name to dept_name;

Net Framework 讀取檔案變亂碼的處理方式

今天因為做長榮的案子,壓根沒想到 Microsoft .Net Framework 讀取檔案竟然會出現亂碼,以前用 VB 都鮮少遇過這種事。

重點還是在於如何解決,因為從 Windows 2000 之後的作業系統在檔案處理採用Unicode ,所以 .Net 的檔案處理也是預設為 Unicode ,但是文字檔大多還是以 ANSI 儲存,而且裡面的編碼還是用 Big5,所以才會造成中文亂碼的狀況,也就是在讀取檔案的時候就要指定編碼的樣式。
本來會造成亂碼的語法如下:
Dim reader As StreamReader = New StreamReader(FileName)
但是問題來了,System.Text.Encoding 裡面一堆,ASCII、UTF-8 等等的,要選哪個好?
其實很簡單,用 System.Text.Encoding.Default 告訴 StreamReader 目前作業系統的編碼即可。
[VB .Net] 的寫法
Dim reader As StreamReader = _New StreamReader(FileName, System.Text.Encoding.Default)
[C# .Net] 的寫法
StreamReader reader = _New StreamReader(FileName, System.Text.Encoding.Default)
結果如下:
System.Text.Encoding 可參考 MSDN:http://msdn.microsoft.com/library/en-us/cpref/html/frlrfsystemtextencodingclasstopic.asp

===========================================================

謝謝站長的技術分享,解決了小弟一個讀檔的問題。因為小弟需要從ANSI文字檔中取得某個起訖位罝的中文字元因為文字檔是ANSI格式->中文碼為兩碼 ,.net為unicode- 每個字都是一碼,先轉為byte格式,取出ANSI格式之資料(中文字算兩碼) 正確所在,再轉回unicode格式
Dim sendBytes As [Byte]() = System.Text.Encoding.Default.GetBytes(line)
Dim strValue As String = System.Text.Encoding.Default.GetChars(sendBytes, startPos, endPos - startPos)