2023年2月14日 星期二

RestartService

 Public Shared Function RestartService(ByVal ServiceName As String) As Boolean

    Dim mySC As ServiceProcess.ServiceController

    Try

        mySC = New ServiceProcess.ServiceController(ServiceName)

        mySC.Stop()

        mySC.Start()

        Return mySC.Status = ServiceProcess.ServiceControllerStatus.Running

    Catch

        Return False

    End Try

End Function



' restart without bothering about success or failure

RestartService("MSSQLSERVER")


'or like this if you are too concerned ;)

If RestartService("MSSQLSERVER") Then

    MsgBox("Service restarted")

Else

    MsgBox("Unable to restart the service.")

End If


----------------------------------------

@ECHO OFF


net stop ServiceName

net start ServiceName


2023年2月13日 星期一

delay 的方法

  Sub delay(ByVal delay_ms As Integer)

        Dim tspan As New TimeSpan

        Dim tstart = Now

        While tspan.TotalMilliseconds < delay_ms

            tspan = Now - tstart

            Application.DoEvents()

        End While

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Label1.Text = "Hello"

        delay(100) 'leaks 572 ns every 10 ms

        Label1.Text = "Salut"

    End Sub


2023年1月9日 星期一

ProcessCmdKey

 Public Class Form1


    Protected Overrides Function ProcessCmdKey(ByRef msg As Message, keyData As Keys) As Boolean

        Select Case keyData

            Case (Keys.F10 Or Keys.Shift Or Keys.Control)

                MessageBox.Show("You pressed Shft+Ctrl+F10.")

                Return True

            Case (Keys.F10 Or Keys.Control)

                MessageBox.Show("You pressed Ctrl+F10.")

                Return True

            Case Keys.F10

                MessageBox.Show("You pressed F10.")

                Return True

        End Select


        Return MyBase.ProcessCmdKey(msg, keyData)

    End Function


End Class




Option Strict On


Public Class Form1


    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        Me.Location = New Point(CInt((Screen.PrimaryScreen.WorkingArea.Width / 2) - (Me.Width / 2)), CInt((Screen.PrimaryScreen.WorkingArea.Height / 2) - (Me.Height / 2)))

        Me.KeyPreview = True

    End Sub


    Private Sub Form1_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown

        If e.KeyCode = Keys.F10 Then

            e.Handled = True

            Button2_Click(Me, Nothing)

        End If

        If e.Control AndAlso e.KeyCode = Keys.B Then

            e.Handled = True

            Button2_Click(Me, Nothing)

        End If

    End Sub


    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

        Button2.Text = ChrW(38) & "Follow"

    End Sub


    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click

        MessageBox.Show("Button2 selected.")

    End Sub


End Class

MS-SQL刪除帳號

 SELECT * FROM INFORMATION_SCHEMA.SCHEMATA


將帳號佔用的權限轉移到dbo

alter authorization on schema::[db_securityadmin] To [dbo]