顯示具有 C# 標籤的文章。 顯示所有文章
顯示具有 C# 標籤的文章。 顯示所有文章

2015年3月13日 星期五

C# ~ ODBC 資料庫連結 MYSQL

透過ODBC讀取MYSQL
 C#

1.using System.Data.Odbc;

2.        OdbcConnection odbcCon ;
           OdbcCommand odbcCmd;
           OdbcDataReader dataSet;
           string conString;

           odbcCon = new OdbcConnection(conString);
            odbcCon.Open();
            odbcCmd = new OdbcCommand();
            odbcCmd.Connection = odbcCon;
            odbcCmd.CommandText = "select EMP_no,EMP_NM,START_DT from bdm2emp";
            dataSet = odbcCmd.ExecuteReader();
            while (dataSet.Read())
            {
                row.Add(new object[] { dataSet[0],dataSet[1],dataSet[2]});
            }
            odbcCon.Close();
            dataSet.Close();

2014年10月22日 星期三

C# 最簡單的16進位轉2進位

 public string Hex2Int2Binary(string strHex)
 {
     int intNum = Convert.ToInt32(strHex, 16);
     string strBinary = Convert.ToString(intNum, 2);
     return strBinary;
 }

2014年10月7日 星期二

C# FTP 目錄底下檔案搜尋


// ftpPath:FTP 位置
//ftpUser:FTP帳號
//ftpPassword:FTP密碼
//strExtension:被搜尋的副檔名
//strFileList:回傳的檔案名稱(數量要先計算,要先new好該有的數量,另一篇會教如何計算數量)
計算方式

public bool CheckFTPFileList(string ftpPath, string ftpUser, string ftpPassword, string strExtension, ref string[] strFileList)
        {
            bool success = false;
            FtpWebRequest ftpWebRequest = null;
            WebResponse webResponse = null;
            StreamReader reader = null;
            try
            {
                string url = ftpPath;
                //new 一個FTP連線
                ftpWebRequest = (FtpWebRequest)FtpWebRequest.Create(new Uri(url));
                //建立FTP連線
                ftpWebRequest.Credentials = new NetworkCredential(ftpUser, ftpPassword);
                //選擇通訊協定方法
                ftpWebRequest.Method = WebRequestMethods.Ftp.ListDirectory;
                //完成要求動做後,關閉連線
                ftpWebRequest.KeepAlive = false;
                // 取得FTP回應
                webResponse = ftpWebRequest.GetResponse();
                //讀取FTP資訊
                reader = new StreamReader(webResponse.GetResponseStream());

                string line = "";
                int intFileCount = 0;
                while (line != null)
                {
                    //讀取目錄下檔案名稱及根目錄名稱
                    line = reader.ReadLine();
                   //如有包含指定副檔名,即可記錄
                    if (line.Contains(strExtension))
                    {
                        strFileList[intFileCount] = line;
                        intFileCount++;
                    }
                }
            }
            catch (Exception)
            {
                success = false;
            }
            finally
            {
                if (reader != null)
                {
                    reader.Close();
                }
                if (webResponse != null)
                {
                    webResponse.Close();
                }
            }
            return success;
        }

C# FTP根目錄底下,指定副檔名,檔案計算數量

//ftpPath:FTP位置
//ftpUser:FTP帳號
//ftpPassword:FTP密碼
//strExtension:副檔案名稱
//intFileCount:回傳計算結果

public bool CheckFTPCount(string ftpPath, string ftpUser, string ftpPassword, string strExtension, ref int intFileCount)
        {
            bool success = false;
            FtpWebRequest ftpWebRequest = null;
            WebResponse webResponse = null;
            StreamReader reader = null;
            try
            {
                string url = ftpPath;
                //new 一個FTP連線
                ftpWebRequest = (FtpWebRequest)FtpWebRequest.Create(new Uri(url));
                //建立FTP連線
                ftpWebRequest.Credentials = new NetworkCredential(ftpUser, ftpPassword);
                //選擇通訊協定方法
                ftpWebRequest.Method = WebRequestMethods.Ftp.ListDirectory;
                //完成要求動做後,關閉連線
                ftpWebRequest.KeepAlive = false;
                // 取得FTP回應
                webResponse = ftpWebRequest.GetResponse();
                //讀取FTP資訊
                reader = new StreamReader(webResponse.GetResponseStream());

                string line = "";
                intFileCount = 0;
                while (line != null)
                {
                    //讀取目錄下檔案名稱及根目錄名稱
                    line = reader.ReadLine();
                   //如有包含指定副檔名,即可記錄
                    if (line.Contains(strExtension))
                    {
                        intFileCount++;
                    }

                }
            }
            catch (Exception)
            {
                success = false;
            }
            finally
            {
                if (reader != null)
                {
                    reader.Close();
                }
                if (webResponse != null)
                {
                    webResponse.Close();
                }
            }
            return success;
        }

C# EASendMail Lib 程式寄信教學

下載處:EASendMail
// strReceiver :收件者
//strTitle:信件標題
//strBody:信件內容
//strUserName:信箱帳號
//strPW:信箱密碼
public void SendEmail(string strReceiver, string strTitle, string strBody, string strUserName, string strPW)
        {
            try
            {
                SmtpMail oMail = new SmtpMail("TryIt");
                EASendMail.SmtpClient oSmtp = new EASendMail.SmtpClient();

                // Set sender email address, please change it to yours
                oMail.From = "XXX@gamil.com";

                // Set recipient email address, please change it to yours
                oMail.To = strReceiver;

                // Set email subject
                oMail.Subject = strTitle;

                // Set email body
                oMail.TextBody = strBody;

                // Your SMTP server address
                SmtpServer oServer = new SmtpServer("smtp.gmail.com");

                // User and password for ESMTP authentication, if your server doesn't require
                // User authentication, please remove the following codes.          
                oServer.User = strUserName;
                oServer.Password = strPW;

                // If your smtp server requires TLS connection, please add this line
                // oServer.ConnectType = SmtpConnectType.ConnectSSLAuto;

                // If your smtp server requires implicit SSL connection on 465 port, please add this line
                oServer.Port = 465;
                oServer.ConnectType = SmtpConnectType.ConnectSSLAuto;

                try
                {
                    oSmtp.SendMail(oServer, oMail);
                }
                catch (Exception ep)
                {
                    MessageBox.Show(ep.Message);
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
來源參考