VB.net技巧更新(一)

'窗體位置設置
ChildForm.WindowState = FormWindowState.Maximized
ChildForm.FormBorderStyle = Windows.Forms.FormBorderStyle.None
ChildForm.MaximizeBox = False
ChildForm.MinimizeBox = False
ChildForm.StartPosition = FormStartPosition.Manual
ChildForm.WindowState = FormWindowState.Maximized

'以下函數在命名空间:Microsoft.VisualBasic
IsNumeric 函数 (Visual Basic)
IsArray 函数 (Visual Basic)
IsDate 函数 (Visual Basic)
IsDBNull 函数
IsError 函数
IsNothing 函数
IsReference 函数
Object 数据类型
TypeName 函数 (Visual Basic)

'兩個輸出測試
Console.Write("456")
Debug.Write("456")

SystemColors.Control'系統色

'讀取配置數據庫信息(需要添加引用System.Configuration命名空間)
 StrCon = System.Configuration.ConfigurationManager.ConnectionStrings("EasyERP.My.MySettings.EasyDBConnectionString").ToString()
ConTimeOut = System.Configuration.ConfigurationManager.AppSettings("ConTimeOut").ToString()


''' <summary>
''' 導出數據到EXCEL文件,精確控制到單元格
''' </summary>
''' <param name="Dtab"></param>
''' <returns></returns>
''' <remarks></remarks>

 Dim b As String = New String("-", 44)'賦給B變量一個字符串--

'復制表的數據和結構
  Dim b As DataTable
b = CType(Grid1.DataSource, DataTable).Copy()


'table表中求和
Dim b As DataTable
b = CType(Grid1.DataSource, DataTable).Copy
Dim sum As Object = b.Compute("Sum(Total)", "PoNo = '09SS004'")
MsgBox(sum, MsgBoxStyle.Information, "求和")

'取得相差時間的日期
Dim c As Object = DateDiff(DateInterval.Day, CDate("2008 - 2 - 3"), CDate("2008 - 2 - 8"))

 '關閉EXCEL進程
         For Each c As Process In Process.GetProcessesByName("EXCEL")
            If c.MainWindowTitle.Trim() <> "" Then
                c.Kill()
            End If
        Next

'調用存儲過程
Dim cmd As SqlClient.SqlCommand = Nothing
        Dim Result As Boolean = False
        Try
            cmd = New SqlClient.SqlCommand("ApprowOrder", cnNPMDB)
            If cnNPMDB.State <> ConnectionState.Open Then cnNPMDB.Open()
            With cmd
                .CommandType = CommandType.StoredProcedure
                .Parameters.AddWithValue("@OrderType", 1) '0:板单 1:订单 2:合同 3:制单 4:发票
                .Parameters.AddWithValue("@OrderNO", OrderNO)
                .Parameters.AddWithValue("@ApType", IIf(Me.TypeStyle = EnumStyle.Approve, "A", "C"))
                .Parameters.AddWithValue("@ApMan", ActiveUser.UID)
                .Parameters.Add("@Return", SqlDbType.NVarChar, 200)
                .Parameters("@Return").Direction = ParameterDirection.Output
                .Parameters.Add("@iReturn", SqlDbType.Int)
                .Parameters("@iReturn").Direction = ParameterDirection.ReturnValue
            End With
            cmd.ExecuteNonQuery()
            cnNPMDB.Close()
            Result = IIf(cmd.Parameters("@iReturn").Value = -1, False, True)
            MessageBox.Show(cmd.Parameters("@Return").Value & "", "提示", MessageBoxButtons.OK, IIf(Result = False, MessageBoxIcon.Error, MessageBoxIcon.Information))
            cmd.Dispose()
        Catch ex As Exception
            If Not cmd Is Nothing Then cmd.Dispose()
            If cnNPMDB.State <> ConnectionState.Open Then cnNPMDB.Close()
            MessageBox.Show("錯誤描述:" + ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
        Return Result

'檢查文件是否存在,如果不存在,建立該文件
Public ReadOnly TempPath As String = Application.StartupPath + "\temp\"
 If IO.Directory.Exists(TempPath) = False Then
                IO.Directory.CreateDirectory(TempPath)
            End If

'髮送郵件
 Public Function SendMail(ByVal Address As String, ByVal Title As String, ByVal Body As String, ByVal Attachment As String) As Boolean
        Dim Result As Boolean = False
        Dim mySmtpClient As Net.Mail.SmtpClient = Nothing
        Dim m As Net.Mail.MailMessage = Nothing
        Dim ss(), ss1() As String, i As Integer
        ss1 = Nothing : ss = Nothing
        Address = Address.Trim : Attachment = Attachment.Trim
        If Address = "" Then Return False
        ss = Split(Address, ";")
        If Attachment <> "" Then ss1 = Split(Attachment, ";")

        Try
            mySmtpClient = New Net.Mail.SmtpClient("newplazasrv02")
            mySmtpClient.UseDefaultCredentials = True
            mySmtpClient.Credentials = New Net.NetworkCredential("easysoft", "a123456")
            m = New Net.Mail.MailMessage
            m.From = New Net.Mail.MailAddress("easysoft@newplaza.com.hk")
            m.ReplyTo = New Net.Mail.MailAddress("*@*.*", "<請不要直接回復此地址>")
            m.Subject = Title
            m.Body = Body
            For i = 0 To ss.Length - 1
                If ss(i).Trim <> "" Then m.To.Add(ss(i))
            Next
            If Attachment <> "" Then
                If Not ss1 Is Nothing Then
                    For i = 0 To ss1.Length - 1
                        If IO.File.Exists(ss1(i)) = True Then
                            m.Attachments.Add(New Net.Mail.Attachment(ss1(i)))
                        End If
                    Next
                End If
            End If
            mySmtpClient.Send(m)
            'mySmtpClient.Send("easysoft@newplaza.com.hk", Address, "FLS", "fjlsjf")
            Result = True
        Catch ex As Exception
            MessageBox.Show("錯誤:" + ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
        If Not m Is Nothing Then m.Dispose()
        Return Result
    End Function

'類型轉換(介绍基于继承或实现的类型转换操作)
  Dim con As New TextBox
        If TypeOf con Is TextBox Then
            Dim cbo As Control = DirectCast(con, Control)
            MsgBox(cbo.GetType().Name + "已成功轉換成基類控件")
        End If


'存儲過程
 '登录
    '參數說明: sysLimit 系統可登錄上限
    Public Shared Function Login(ByVal Uid As String, _
                                 ByVal Pwd As String, _
                                 ByVal sysLimit As Integer) As Integer
        Dim cmdpub As SqlCommand = Nothing
        Dim iRt As Integer = -1, msg As String = ""
        Try
            cmdpub = New SqlCommand("prSafeLogin", cnNPMDB)
            With cmdpub
                .CommandType = CommandType.StoredProcedure
                .Parameters.AddWithValue("@UID", Uid)
                .Parameters.AddWithValue("@Psd", Pwd)
                .Parameters.AddWithValue("@UpperLimit", sysLimit)
                '返回參數
                .Parameters.Add("@iReturn", SqlDbType.Int)
                .Parameters("@iReturn").Direction = ParameterDirection.ReturnValue
                '返回值
                .Parameters.Add("@result", SqlDbType.NVarChar, 200)
                .Parameters("@result").Direction = ParameterDirection.Output
            End With
            If cnNPMDB.State <> ConnectionState.Open Then cnNPMDB.Open()
            cmdpub.ExecuteNonQuery()
            cnNPMDB.Close()
            '獲取返回值
            iRt = cmdpub.Parameters("@iReturn").Value
            '獲取返回參數
            If Not IsDBNull(cmdpub.Parameters("@result").Value) Then
                msg = cmdpub.Parameters("@result").Value
            End If
            cmdpub.Dispose()
        Catch ex As Exception
            If cnNPMDB.State = ConnectionState.Open Then cnNPMDB.Close()
            If Not cmdpub Is Nothing Then cmdpub.Dispose()
            MessageBox.Show("错误描述:" + ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try

        '如果登錄失敗,返回失敗信息
        If iRt < 0 Then
            MessageBox.Show(msg, "登錄失敗", MessageBoxButtons.OK, MessageBoxIcon.Warning)
        End If
        '返回
        Return iRt
    End Function

'事務處理
  ''' <summary>
    ''' 返回数据表
    ''' </summary>
    ''' <param name="SQl">SQL语句</param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Private Function GetDataTable(ByVal SQl As String, ByVal IsTran As Boolean) As DataTable
        Try
            Dim Dtab As New DataTable
            SQLCon = New SqlConnection(ConnString)
            If IsTran = True Then SQLTran = SQLCon.BeginTransaction()
            SQLCmd = New SqlCommand(SQl, SQLCon)
            If IsTran = True Then SQLCmd.Transaction = SQLTran
            SQLDa = New SqlDataAdapter(SQLCmd)
            If SQLCon.State = ConnectionState.Closed Then SQLCon.Open()
            SQLDa.Fill(Dtab)
            If IsTran = True Then SQLTran.Commit()
            SQLCon.Close()
            Return Dtab
        Catch ex As SqlException
            If IsTran = True Then SQLTran.Rollback()
            If SQLCon.State = ConnectionState.Open Then SQLCon.Close()
            Throw ex
        End Try
    End Function


   本人博客的文章大部分来自网络转载,因为时间的关系,没有写明转载出处和作者。所以在些郑重的说明:文章只限交流,版权归作者。谢谢

原文地址:https://www.cnblogs.com/wzg0319/p/1349209.html