2011年11月4日

C# ListView 使用方式

1.建立一個Windows Form專案
2.拉入ListView 控制項
3.填入ListView的Header

程式碼如下:

                this.listView1.Clear();//清除ListView所有資料

            this.listView1.Columns.Add("AAA", 60, HorizontalAlignment.Left);
            this.listView1.Columns.Add("BBB", 100, HorizontalAlignment.Left);
            this.listView1.Columns.Add("CCC", 80, HorizontalAlignment.Left);
4.將ListView中的MultiSelect設為True
設定為true後,則可利用ctrl 來多選
若要取得在ListView中被選取的Item方法如下:

利用foreach迴圈 ,並宣告ListViewItem,使用Selected判斷是否被選取!

                    foreach (ListViewItem tmpLstView in listView1.Items)
                    {
                        if (tmpLstView.Selected == true)==>被選取為true
                        {
                            strList1 = strList1 + tmpLstView.Text + ",";
                            strWorkID1 = strWorkID1 + tmpLstView.SubItems[1].Text + ",";
                        }
                    }      
取得ListView中特定一項Item中的細項資料方法為:
 tmpLstView.SubItems[0].Text ==>取得AAA對應的值
 tmpLstView.SubItems[1].Text ==>取得BBB對應的值
 tmpLstView.SubItems[1].Text ==>取得ccc對應的值

PLC WriteDeviceRandom方法

在MX component Programming 中Act元件
提供了WriteDeviceRandom方法
需要填入的資料如下:
1.Device List
2.Total Write Device count
3.Device Value
假設現在要寫入的Device有D200,D208,D201
在C#中DEVICE要寫入的資料為:
string strTmp = "D200" + '\n' + "D208" + '\n' + "D201" + '\n'
在VB.Net中則為:
dim strTmp as string = "D200" + vblf + "D208" + vblf + "D201" + vblf

2011年10月27日

C# Read XML file


C# 利用system.xml 中的class讀取XML的方式:


要讀取的XML內容如下:

<transaction>==>Root
<sqlcmd command="Insert into GlassEQPHistory(BCNo,LineID,NodeNo,EQName,Repdatetime,Funkey,Workid,WorkNo,Worktype,Pid,Groupflag,Eng_unit1,Eng_unit2,Overbakeflag,panel_angle ,criteria,Asmtype,Viewflag,Gapflag,Asmfinish,Sealfinish,Aufinish,RubbPass,SealPass,AUPass,LcdpPass,ChamberPass,Pair_no,NonMatchflg,RubxConfirmFlag,RubxPackFlag )values('7','RBAB0100','4','Rubx','20101218015044','Receive Data','F50CC011CL01','FA88','0','799','9','0','0','0','0','6','1','0','0','0','0','0','0','0','0','0','0','0','0','0','0')"/>
</transaction>



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.IO;
namespace Csharp_Console_ReadXML
{
    class Program
    {
        static void Main(string[] args)
        {
         //   XmlTextReader xRD= new XmlTextReader("C:\\4_Receive Data_20101218015044.xml");
             利用XMLDocument讀取;        
             XmlDocument xDL = new XmlDocument();
            xDL.Load("C:\\4_Receive Data_20101218015044.xml"); //Load XML檔

           
            讀取單一Node,利用SingleNode方法取得
             XmlNode xSingleNode = xDL.SelectSingleNode("//sqlcmd");
             XmlAttributeCollection xAT = xSingleNode.Attributes; //讀出該Node所有的Attribute
             for (int i = 0; i < xAT.Count; i++)
             {
                 if (xAT.Item(i).Name == "command")
                     Console.WriteLine(xAT.Item(i).Value); //讀出我們想要的attribute內容
             }

    '------------------這是分隔線--------------------------------------------------
若要讀取多個NodeList,使用SelectNodes方法
            XmlNodeList NodeList = xDL.SelectNodes("//sqlcmd");

            foreach (XmlNode item_File in NodeList)
            {
                Console.WriteLine(item_File.Name);
                XmlAttributeCollection xAT = item_File.Attributes;

                for (int i = 0; i < xAT.Count; i++)
                {
                    if (xAT.Item(i).Name == "command")
                        Console.WriteLine(xAT.Item(i).Value);
                }

                Console.WriteLine(item_File.InnerText);
            }

       
            Console.ReadLine();
        }
    }
}


C# Use OpenFileDialog

使用OpenFileDialog的方式
1.先建立一個實體
2.定義較常使用的屬性
Code Sample:


            OpenFileDialog ofd1 = new OpenFileDialog();
            ofd1.Title = "請選擇檔案";
            ofd1.InitialDirectory = "D:";
            ofd1.Filter = "txt files (*.log)|*.txt|All files (*.*)|*.*";

            if (ofd1.ShowDialog(this) == DialogResult.OK)
            {

............略
             }

SQLExpress連線出現18452 錯誤訊息

解決方式網路上有人分享
再次記錄,增加自己記憶力
1.進入SQL Express使用Windows驗證模式登入
2.點選已登入的Server,按下右鍵選擇屬性
3.選擇安全性,並在"伺服性驗證"將原先的
Windows驗證模式,改為SQL Server與Windows驗證模式

2011年7月22日

再來一個ACCESS!

連接二周都花時間在幫忙開發User所提出的軟體需求
看似簡單的Insert 語法,卻搞了我一陣子=.=
一般的Insert 語法為:
Insert into Table(欄位名稱1,欄位名稱2) values (欄位1的值,欄位2的值)
不過在Insert到ACCESS 2003時,總會出現錯誤不合法的Insert語法
爬了許多文之後,把Insert的語法改成如下:


Insert into Table([欄位名稱1],[欄位名稱1])values (欄位1的值,欄位2的值)

用中刮號將欄位名稱給"包"起來就OK囉
建議如果Table的欄位定義有空白、中文的,就用[]將欄位名稱包起來吧!

無言的網路

目前使用的是中嘉的BB寬頻6M/1M
今天收到業者寄來的帳單
說網路優惠價6M/1M合約價二個月1300元
算了算跟之前完全一樣
看來台南這邊只有在中華剛宣佈要將光世代降價時
採取了寬頻半價的優惠(其實也不是綁一年全部的價格半價,感覺上有點雞肋)
現在對於老客戶也是沒啥優惠
還好前幾天老婆抽空去申請了光世代~BB寬頻再沒幾天我就跟你Say bye bye囉

2011年7月10日

Access 使用OLEDB下Select 條件問題

最近幫人開發一隻小工具
會有需要查詢到ACCESS中的Table資料
不過下完資料後會出現"準則運算式的資料類型不符"的錯誤訊息
爬文後,解決方式如下:
原先下的sql 條件字串中是使用單引號'
改成#字號就可以正常取得table內資料
原先語法:
select * from Table where field1='2011/6/7'
改為:
select * from Table where field1=#2011/6/7#
就可正常取得資料