2014年12月11日 星期四

VBNET 自然憑證簡易驗證


Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim a As String = "AB01234567891234"
        MessageBox.Show(chk_card(a))
    End Sub

    Public Overloads Function chk_card(ByVal card_code As String) As Boolean
        Dim chk_yn As Boolean = False
        Try
            Dim str_g As String = ""
            Dim y As Integer = 0
            For i As Integer = 1 To 16
                str_g = Mid(card_code, i, 1)
                Select Case i
                    Case 1 To 2
                        If Asc(str_g) > 90 Or Asc(str_g) < 65 Then
                            y = 1
                            Exit For
                        End If
                    Case 3 To 16
                        If Asc(str_g) > 57 Or Asc(str_g) < 48 Then
                            y = 1
                            Exit For
                        End If
                End Select
            Next
            If y = 0 Then
                chk_yn = True
            End If
            Return chk_yn
        Catch ex As Exception
            Return chk_yn
        End Try
    End Function

2014年12月4日 星期四

DataTable 轉換成 CSV 與 CSV 轉換成 DataTable

參考引用來源
--

DataTable 轉成 CSV 檔函式程式碼如下:
public void CreateCSVFile(DataTable dt, string strFilePath) // strFilePath 為輸出檔案路徑 (含檔名)
{
    StreamWriter sw = new StreamWriter(strFilePath, false);

    int intColCount = dt.Columns.Count;

    if (dt.Columns.Count > 0)
        sw.Write(dt.Columns[0]);
    for (int i = 1; i < dt.Columns.Count; i++)
        sw.Write("," + dt.Columns[i]);

    sw.Write(sw.NewLine);
    foreach (DataRow dr in dt.Rows)
    {
        if (dt.Columns.Count > 0 && !Convert.IsDBNull(dr[0]))
            sw.Write(Encode(Convert.ToString(dr[0])));
        for (int i = 1; i < intColCount; i++)
            sw.Write("," + Encode(Convert.ToString(dr[i])));
        sw.Write(sw.NewLine);
    }
    sw.Close();
}

public string Encode(string strEnc)
{
    return System.Web.HttpUtility.UrlEncode(strEnc);
}



讀取 CSV 轉成 DataTable 函式程式碼如下:
public DataTable ReadCSVFile(string strFilePath, string strFileName) // strFilePath 為檔案所在資料夾,strFileName 為檔案名稱
{
    OleDbConnection connection = new OleDbConnection(string.Format(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0}; Extended Properties=""text;HDR=Yes;FMT=Delimited"";", strFilePath));
    OleDbCommand command = new OleDbCommand("SELECT * FROM " + strFileName, connection);
    OleDbDataAdapter adapter = new OleDbDataAdapter(command);
    DataTable dt = new DataTable();

    adapter.Fill(dt);

    return dt;
}