精選文章

bcp大量匯出與匯入資料庫資料方法

--匯出-- bcp "select * from [資料庫名稱].[dbo].[資料表名稱]" queryout 匯出檔案名稱.txt -w -U "使用者帳號" -P "使用者密碼" " "...

2021年3月12日 星期五

ASP.NET 刷新頁面的方法

問題:ASP.NET 刷新頁面的方法
"ASP.NET 刷新頁面的方法"
解決方法:
方法1
Response.Redirect(Request.Url.ToString());
方法2
Response.AddHeader("Refresh", "0");

2020年9月15日 星期二

bcp大量匯出與匯入資料庫資料方法

--匯出--
bcp "select * from [資料庫名稱].[dbo].[資料表名稱]" queryout 匯出檔案名稱.txt -w -U "使用者帳號" -P "使用者密碼"
" " 裡面的語法可以根據需求自行修改

--匯入--
bcp [資料庫名稱].[dbo].[資料表名稱] in 匯入檔案名稱.txt -w -U "使用者帳號" -P "使用者密碼"


參考網址:
https://technet.microsoft.com/zh-tw/library/ms162802(v=sql.105).aspx

http://vito-note.blogspot.com/2013/07/blog-post_7.html

2019年10月1日 星期二

Selenium-瀏覽器的操作

說明:
以下是一些關於瀏覽器操作的語法與案例

1.訪問指定網址
以下二種方法都可以訪問指定的網址
driver.Navigate().GoToUrl("https://www.google.com/");

driver.Url ="https://www.google.com/"

2.回上一頁
driver.Navigate().Back();

3.到下一頁
driver.Navigate().Forward();

4.重新整理
driver.Navigate().Refresh();

5.瀏覽器最大化
driver.Manage().Window.Maximize();

6.瀏覽器最小化
driver.Manage().Window.Minimize();

7.瀏覽器自訂大小
driver.Manage().Window.Size = new System.Drawing.Size(800, 600);

8.關閉瀏覽器
driver.close(); //關閉當前瀏覽器視窗
driver.quit(); //退出驅動並關閉所有關聯的視窗

2019年9月26日 星期四

Selenium-使用不同的瀏覽器

說明:
在前篇初始語法裡,使用瀏覽器是Chrome,但Selenium支援很多不同的瀏覽器, 這裡要教如何改更瀏覽器,可以實現同個案例,可以使用不同的瀏覽器進行驗證。

驅動瀏覽器
在初始語法裡,是寫在 [ClassInitialize],
就是每次執行此測試類別時,都會去執行,驅動Chrome如下
driver = new ChromeDriver();
想要使用其他瀏覽器,只要更改此段語法即可,在更改其他瀏覽器時,在using也要新增該瀏覽器的提示詞。

以第1個範例的程式碼進行修改-使用Firefox
將語法修改如下:
using OpenQA.Selenium.Chrome; => using OpenQA.Selenium.Firefox;
driver = new ChromeDriver(); => driver = new FirefoxDriver();
並執行測試,結果發生錯誤訊息,原因是,沒有安裝Firefox驅動,可以到NuGet安裝「Selenium.Firefox.WebDriver」
相關的瀏覽器驅動可以到https://www.seleniumhq.org/download/,有下載網址。
錯誤訊息:
類別初始設定方法 UnitTestProject1.UnitTest1.InitializeClass 擲回例外狀況。OpenQA.Selenium.DriverServiceNotFoundException: OpenQA.Selenium.DriverServiceNotFoundException: The geckodriver.exe file does not exist in the current directory or in a directory on the PATH environment variable. The driver can be downloaded at https://github.com/mozilla/geckodriver/releases.。
使用IE
操作與Firefox相同,也要去下載驅動,都完成後執行測試,還是發生錯誤訊息,
需要去調整IE的安全性,操作如下:
工具->網際網路選項->安全性,
將「網際網路」、「近端內部網路」、「信任的網站」、「限制的網站」
以上四個區域的「啟用受保護模式(需要重新啟動Internet Explorer)」,
都打勾或都不勾

錯誤訊息:
System.InvalidOperationException: System.InvalidOperationException: Unexpected error launching Internet Explorer. Protected Mode settings are not the same for all zones. Enable Protected Mode must be set to the same value (enabled or disabled) for all zones. (SessionNotCreated)。

2019年9月25日 星期三

C# Google Maps API取得資料

問題:C# Google Maps API取得資料
把地址轉成JSON格式
    
public GoogleGeoCodeResponse ConvertAddressToLatLng(string addr)
        {
            string result = string.Empty;
            string googlemapkey = "你的API金鑰";         
            string MapUrl = String.Format("https://maps.googleapis.com/maps/api/geocode/json?");
            
            string url = MapUrl + "address={0}&key={1}";
            url = string.Format(url, addr, googlemapkey);

            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
            using (var response = request.GetResponse())
            using (StreamReader sr = new StreamReader(response.GetResponseStream()))
            {

                result = sr.ReadToEnd();
            }

            return JsonConvert.DeserializeObject(result);
        }

取得的JSON格式(20190925)
 
{
   "results" : [
      {
         "address_components" : [
            {
               "long_name" : "122號",
               "short_name" : "122號",
               "types" : [ "street_number" ]
            },
            {
               "long_name" : "Section 1, Chongqing South Road",
               "short_name" : "Section 1, Chongqing South Road",
               "types" : [ "route" ]
            },
            {
               "long_name" : "Bo'ai Special Zone",
               "short_name" : "Bo'ai Special Zone",
               "types" : [ "neighborhood", "political" ]
            },
            {
               "long_name" : "Zhongzheng District",
               "short_name" : "Zhongzheng District",
               "types" : [ "administrative_area_level_3", "political" ]
            },
            {
               "long_name" : "Taipei City",
               "short_name" : "Taipei City",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "Taiwan",
               "short_name" : "TW",
               "types" : [ "country", "political" ]
            },
            {
               "long_name" : "100",
               "short_name" : "100",
               "types" : [ "postal_code" ]
            }
         ],
         "formatted_address" : "No. 122號, Section 1, Chongqing South Road, Zhongzheng District, Taipei City, Taiwan 100",
         "geometry" : {
            "location" : {
               "lat" : 25.0400826,
               "lng" : 121.5119547
            },
            "location_type" : "ROOFTOP",
            "viewport" : {
               "northeast" : {
                  "lat" : 25.0414315802915,
                  "lng" : 121.5133036802915
               },
               "southwest" : {
                  "lat" : 25.03873361970849,
                  "lng" : 121.5106057197085
               }
            }
         },
         "place_id" : "ChIJJXNJjQqpQjQR-UfWtMFthwI",
         "plus_code" : {
            "compound_code" : "2GR6+2Q Zhongzheng District, 台北市 Taiwan",
            "global_code" : "7QQ32GR6+2Q"
         },
         "types" : [ "establishment", "point_of_interest", "tourist_attraction" ]
      }
   ],
   "status" : "OK"
}

設定的結構
部份有調整過,可以自行設計
 
public class GoogleGeoCodeResponse
        {

            public string status { get; set; }
            public string error_message { get; set; }
            public results[] results { get; set; }
        }
        public class results
        {
            public string formatted_address { get; set; }
            public geometry geometry { get; set; }
            public string[] types { get; set; }
            public address_component[] address_components { get; set; }
        }
        public class geometry
        {
            public string location_type { get; set; }
            public location location { get; set; }
        }

        public class location
        {
            public string status { get; set; }
            public string error_message { get; set; }
            public string lat { get; set; }
            public string lng { get; set; }
        }
        public class address_component
        {
            public string long_name { get; set; }
            public string short_name { get; set; }
            public string[] types { get; set; }
        }

處理地址轉成json後,輸出經緯度資料
 
public location GetLatLngByAddr(string addr)
        {
            location _result = new location();
            GoogleGeoCodeResponse _mapdata = new GoogleGeoCodeResponse();
            _mapdata = ConvertAddressToLatLng(addr);
            if (_mapdata.status == "OK")
            {
                    _result.lat = _mapdata.results[0].geometry.location.lat;
                    _result.lng = _mapdata.results[0].geometry.location.lng;
            }
            else
            {
                _result.error_message = _mapdata.error_message;
            }
            _result.status = _mapdata.status;
            return _result;
        }

2019年9月20日 星期五

Selenium-開始第1個範例 (for C# MSTest)

說明:
使用初始語法去修改,在T1內容裡輸入以下內容後執行測試, 執行成功的話,會直接關掉瀏覽器,建議可以把
CleanupClass()中的
driver.Close()
driver.Dispose()
註解掉

範例
會導向google首頁網址
    
 public void T1名稱()
        {
            try
            {
                driver.Navigate().GoToUrl("https://www.google.com");
            }
            catch (Exception ex)
            {
                verificationErrors.Append(ex);
            }
               
        }

結果影片

2019年9月19日 星期四

Selenium-初始語法 (for C# MSTest)

說明:
使用C# MSTest 進行Selenium時,會這樣設計初始語法再去新增或修改

 
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Support.UI;
using System.Diagnostics;

namespace UnitTestProject1
{
    [TestClass]
    public class UnitTest1
    {
        //初始設定
        private static IWebDriver driver;
        private StringBuilder verificationErrors;

        // ClassInitialize運行類別的第一個測試前運行代碼(在這個測試類別都會去執行)
        [ClassInitialize]
        public static void InitializeClass(TestContext testContext)
        {
            //自行設定路徑或直接從Nuget安裝
            driver = new ChromeDriver();
            //瀏覽器最大化
            driver.Manage().Window.Maximize();
        }
        // ClassCleanup運行完類別中的所有測試后再運行代碼
        // (就是這個測試類別裡的測試代碼都執行完畢後才執行)
        [ClassCleanup]
        public static void CleanupClass()
        {
            try
            {
                //driver.Quit();// quit does not close the window
                driver.Close();
                driver.Dispose();
            }
            catch (Exception)
            {
                // Ignore errors if unable to close the browser
            }
        }
        // TestInitialize在運行每個測試前先運行代碼(就是在TestMethod()前會執行的程式碼)
        [TestInitialize]
        public void InitializeTest()
        {
            verificationErrors = new StringBuilder();
        }
        // TestCleanup在運行完每個測試後運行代碼(就是在TestMethod()後會執行的程式碼)    
        [TestCleanup]
        public void CleanupTest()
        {
            //Trace.WriteLine(verificationErrors.ToString());
            Assert.AreEqual("", verificationErrors.ToString());
        }
        // TestMethod撰寫測試案例主要內容,[T1]是為了執行順序方便排序使用,[名稱]可以編輯
        [TestMethod]
        public void T1名稱()
        {

        }
        [TestMethod]
        public void T2名稱()
        {
            
        }

    }
}