2011年7月27日 星期三

[基本功] 字串補0

正如我老闆講的:我的基本功還要再加強,每次都忘記...


字串左邊補0:
dim oDate as string ="970101".PadLeft (7,"0")


右邊補0:
dim oDate as string ="970101".PadRight(7,"0")

在LINQ下直接執行SQL語法

Dim _ctx As MyDBContext
_ctx = New MyDBContext(ConfigurationManager.AppSettings("連線字串"))


1.
_ctx.Database.ExecuteSqlCommand("truncate table 人事檔")


2.
_ctx.Database.ExecuteSqlCommand("delete from 人事檔 where counter=1")

2011年7月18日 星期一

讓 IIS 6 支援 silverlight

環境 :IIS6 + .Net 4.0 + silverlight 
若開網頁的時候,出現『找不到網頁』
但確實是有該網頁時:


可以檢查一下 IIS6 的 Mine 類型是否有加入
xaml / xap  這兩個副檔名


沒有的話,就加入吧~


副檔名      MINE類型
.xaml         application/xaml+xml
.xap          application/x-silverlight-app

2011年7月16日 星期六

建置錯誤:FileTracker : error FTK1011

今日將舊版VS2005專案升級至VS2010後,建置時發生一個錯誤:
FileTracker : error FTK1011


有3種解法:
1.將該專案的Framewotk升級至4.0
2.檢查專案目錄是不是中文,是的話,就改成英文吧
3.我最愛的一種解法,可以維持2.0環境又可用VS2010維護又可用中文目錄:
 (1)用系統管理員權限執行NotePad,開啟:
      C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\Microsoft.Common.Targets
 (2)找到 <TrackFileAccess Condition="'$(TrackFileAccess)' == ''">true</TrackFileAccess>
 (3)將 true 改成  false ,存檔,重新開啟專案,建置成功~~

2011年7月13日 星期三

.Net 讀文字檔兼拆字串兼備份檔案兼刪檔

Dim t, t1(), t2 As String


For Each f As String In My.Computer.FileSystem.GetFiles( txt文字檔目錄.Text, FileIO.SearchOption.SearchTopLevelOnly, "*.txt")
            Dim fi As FileInfo = My.Computer.FileSystem.GetFileInfo(f)
            t = My.Computer.FileSystem.ReadAllText(fi.FullName, System.Text.Encoding.Default)
            t1 = t.Split(",")


            aaa = t1(0)
            bbb = t1(1)


           My.Computer.FileSystem.CopyFile(fi.FullName, txt文字檔備份目錄.Text & "\" & fi.Name, True)
           My.Computer.FileSystem.DeleteFile(fi.FullName)
Next

VB6拆字串

1.很累的作法:


   t = UCase(Trim$(Command$))
   oNum = Mid(t, 1, InStr(t, ",") - 1)
   oText = Mid(t, InStr(t, ",") + 1, Len(t))


2.比較簡單的作法:
    cmdStr = UCase(Trim$(Command$))
    Dim str() As String
    str = Split(cmdStr, ",")
    oNum = str(0)
    oText = str(1)

2011年7月12日 星期二

備份還原SQL連線別名

匯出、匯入
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\MSSQLServer\Client\ConnectTo
在IIS架好網站後若出現:HTTP 錯誤 500.21 - Internal Server Error
處理常式 "PageHandlerFactory-Integrated" 的模組清單中有錯誤的模組  "ManagedPipelineHandler"

其實是因為系統若先安裝好了.Net Framework 4.0,之後才安裝IIS 的話,就會有此錯誤

處理方式:
CMD -->以系統管理員身份執行
輸入 --> %windir%\Microsoft.NET\Framework\v4.0.30319\aspnet_regiis.exe -i
這樣可以重新註冊FrameWork 4,之後就可以正常使用了。

2011年7月1日 星期五

使用 NLOG 做紀錄

1.NLog.dll 加入參考
2.在 <configuration> 區段中,加入:


  <configSections>
    <section name="nlog" type="NLog.Config.ConfigSectionHandler, NLog"/>
  </configSections>


  <nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <targets>
      <target name="file" xsi:type="File" fileName="${basedir}/Log_Data/appName_${date:format=yyyy-MM-dd}.log" layout="${message} ${exception}"/>
    </targets>
    <rules>
      <logger name="*" minlevel="Debug" writeTo="file"/>
    </rules>
  </nlog>


3.程式碼:
Imports NLog


Public _logger As NLog.Logger = NLog.LogManager.GetCurrentClassLogger()


_logger.Info("要記錄的內容")