Thursday, March 20, 2008

What is an optimizer in MS SQL server ?

Original post by sam.SQL at http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=2995794&SiteID=1

The question:

What does mean by Optimizer in MS SQL ? What is it ?
How do you use it ? what are the benifits of use it ?

The accepted answer by Derekman:

The query optimizer is the component of SQL server that determines the most efficient way to execute a query based upon the distribution statistics and histograms maintained within sql server. When you pass a query to sql server the query is optimized based on the estimated "cost" of the query for the different possible ways to exeute the statement based on the table(s) and index(es) available. SQL Server query optimizer reviews the different options in which to execute the query and decides on the most efficient way based on overall cost. Most, but not all, queries are optimized automatically and you do not need to do anything to invoke query optimization. There are however "query hints" that allow you to over ride a query plan recommended by the optimizer.

Please review BOL and the below link for a more detailed explanation.

Best of Luck!!Find out cool Dot Net tutorials and also download projects source code on the dot net source

Add registry key and delete it with vb.net

Original post by Natural_Orange at http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=1828643&SiteID=1

The question:

My program has an option for running at startup.
I need a way to add a registry key to:
HKEY_Current_User\Software\Microsoft\Windows\CurrentVersion\Run\
And Remove it as well.

I've looked in to some other stuff but it seems way to complicated for what i need.

The accepted answer by Moayad Mardini:

To add a value to that key :

My.Computer.Registry.SetValue("HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run", YourProgramName, YourProgramPath, Microsoft.Win32.RegistryValueKind.String)

Then to delete it :

My.Computer.Registry.CurrentUser.OpenSubKey("Software\Microsoft\Windows\CurrentVersion\Run", True).DeleteValue(YourProgramName)

Hope this helpsFind out cool Dot Net tutorials and also download projects source code on the dot net source

How to add a new record (vb.net+SQL)

Original post by Jarge at http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=2483346&SiteID=1

The question:

Ok i have an unbound text box

i also have a command button that does nothing.

I need to be able to have whatever i type into the text box to be entered into a table.

how can i do this?

for the record i know extremely little about VBA, i can copy and paste, and edit bits if im told what, when and where to edit but apart from that i'm useless.

Any help will be greatly appreciated.

The accepted answer by Spotty:

From what you said, you are leaving out real important points

A table, what sort of database are you using, does you applicable actually already use the database in any way already ?

What version of VB are you using ?

All these points determine a solution. The technology you are probably going to be using is called ADO.NET and you are going to be using a Command Object. A web search on ADO.NET + VB.NET + INSERT QUERY will probably reveal quite a few basic examples.


Something a simple as may work.
Dim con As SqlConnection
Try
con = New SqlConnection("Server=(local)\SQLEXPRESS;Initial Catalog=MyDatabase;Integrated Security=SSPI")
con.Open()
Dim cmd As New SqlCommand()
cmd.Connection = con
cmd.CommandText = "INSERT INTO Employee (FirstName) VALUES (@FirstName)"
' 2. Map parameters
cmd.Parameters.Add("@FirstName", SqlDbType.NVarChar, 10, "FirstName")
cmd.Parameters("@FirstName").Value = Textbox1.text
cmd.ExecuteNonQuery()
Finally
con.Close()
End Try
Find out cool Dot Net tutorials and also download projects source code on the dot net source

List Currently Opened Windows with VB.net

Original post by Jupiter13 at http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=2592441&SiteID=1

The question:

I am trying to list all of the currently open windows that are in the taskbar. Does anybody know how to do that?

Thanks in advance.

The accepted answer by Tall Dude:

If you just want the applications,

(not all windows,) try:


Public Class Form1

' Derived from code at:

' http://www.thescarms.com/vbasic/alttab.aspx

Declare Function EnumWindows Lib "user32" _

(ByVal lpEnumFunc As CallBack, ByVal lParam As Integer) As Integer

Declare Function GetForegroundWindow Lib "user32" () As Integer

Declare Function GetParent Lib "user32" (ByVal hwnd As Integer) As Integer

Declare Function GetWindow Lib "user32" _

(ByVal hwnd As Integer, ByVal wCmd As Integer) As Integer

Declare Function GetWindowInteger Lib "user32" Alias "GetWindowLongA" _

(ByVal hwnd As Integer, ByVal nIndex As Integer) As Integer

Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" _

(ByVal hwnd As Integer, ByVal lpString As String, ByVal cch As Integer) As Integer

Declare Function IsIconic Lib "user32" (ByVal hwnd As Integer) As Integer

Declare Function IsWindowVisible Lib "user32" (ByVal hwnd As Integer) As Integer





'

' Constants used with APIs

'

Public Const SW_SHOW = 5

Public Const SW_RESTORE = 9

Public Const GW_OWNER = 4

Public Const GWL_HWNDPARENT = (-8)

Public Const GWL_EXSTYLE = (-20)

Public Const WS_EX_TOOLWINDOW = &H80

Public Const WS_EX_APPWINDOW = &H40000

'



Public Delegate Function CallBack(ByVal hwnd As Integer, ByVal lParam As Integer) As Boolean





Public Shared Function fEnumWindowsCallBack(ByVal hwnd As Integer, ByVal lParam As Integer) As Boolean

Dim lReturn As Integer

Dim lExStyle As Integer

Dim bNoOwner As Boolean

Dim sWindowText As String

'

' This callback function is called by Windows (from

' the EnumWindows API call) for EVERY window that exists.

' It populates the listbox with a list of windows that we

' are interested in.

'

' Windows to display are those that:

' - are not this app's

' - are visible

' - do not have a parent

' - have no owner and are not Tool windows OR

' have an owner and are App windows

'

If hwnd <> Form1.Handle Then

If IsWindowVisible(hwnd) Then

If GetParent(hwnd) = 0 Then

bNoOwner = (GetWindow(hwnd, GW_OWNER) = 0)

lExStyle = GetWindowInteger(hwnd, GWL_EXSTYLE)



If (((lExStyle And WS_EX_TOOLWINDOW) = 0) And bNoOwner) Or _

((lExStyle And WS_EX_APPWINDOW) And Not bNoOwner) Then

'

' Get the window's caption.

'

sWindowText = Space$(256)

lReturn = GetWindowText(hwnd, sWindowText, Len(sWindowText))

If lReturn Then

'

' Add it to our list.

'

sWindowText = Trim(sWindowText)

Form1.ListBox1.Items.Add(sWindowText)



End If

End If

End If

End If

End If

fEnumWindowsCallBack = True

End Function







Private Sub Button1_Click(ByVal sender As System.Object, _

ByVal e As System.EventArgs) Handles Button1.Click

ListBox1.Items.Clear()

EnumWindows(AddressOf fEnumWindowsCallBack, 0)

End Sub

End Class
Find out cool Dot Net tutorials and also download projects source code on the dot net source

convert binary to octal ,hexadecimal and decimal numbers

This is the second part of the previous question.

The response by Martin Xie - MSFT:

By means of Convert class, you can first convert Binary to Decimal Intger, and then convert Decimal to Octal and Hexadecimal string.

Imports Microsoft.VisualBasic

Imports System

Imports System.Collections.Generic

Imports System.Text



Friend Class Program

Shared Sub Main(ByVal args As String())



Dim bin As String = "1000001" 'Binary string



Dim decimalValue As Integer

Dim hexValue As String

Dim OctalValue As String



decimalValue = Convert.ToInt32(bin, 2) 'First convert Binary to Decimal Intger

Console.WriteLine("Decimal:" & decimalValue)



OctalValue = Oct(decimalValue) 'Convert Decimal to Octal string

Console.WriteLine("Octal:" & OctalValue)



hexValue = Hex(decimalValue) 'Convert Decimal to Hexadecimal string

Console.WriteLine("Hexadecimal:" & hexValue)



Console.Read()



End Sub



End Class



The output will be:

Decimal: 65

Octal: 101

Hexadecimal: 41
Find out cool Dot Net tutorials and also download projects source code on the dot net source

Convert ASCII Code/Character to Binary Number

Original post by rattlesnake316 at http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=2159243&SiteID=1

The question:

Hello! Hope someone could help me on converting ASCII characters to Binary
Also,hope you know how to convert Boolean Number to Octal or Hexadecimal or Decimal number.
Thanks in advance!

The accepted answer by Martin Xie - MSFT :

Hi rattlesnake,

ASCII can only represent character codes between 0 and 127.

About converting ASCII characters to Binary,

e.g. convert AscII value 66 of character B to Binary like this:


Imports Microsoft.VisualBasic

Imports System

Imports System.Collections.Generic

Imports System.Text



Friend Class Program

Shared Sub Main(ByVal args As String())



Dim myChar As Char = "B"c

Dim j As Integer = 0

Dim ascii As Integer = System.Convert.ToInt32(myChar)

Console.WriteLine("ASCII:" & ascii)

Dim binTempResult As String = ""

Do While ascii > 0

j = ascii Mod 2

binTempResult &= j.ToString()

ascii = ascii \ 2

Loop



'reverse output

Dim arr As Char() = binTempResult.ToCharArray()

Array.Reverse(arr)

Dim binResult As String = New String(arr)

System.Console.Write(binResult)

Console.Read()



End Sub



End Class


The output will be:

ASCII:66

1000010Find out cool Dot Net tutorials and also download projects source code on the dot net source

Problem applying a filter to binding source

The original post by Kelly R. Martin at http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=1013527&SiteID=1

The question:

Upon updateing a combobox to select the sex of a farm animal I am attempting to filter out the opposite sex in the next ID box.Can anyone see where my issue is here. I have rewritten this many time to no avail. I have read through the msdn help on this and all appears to be done correctly. Also... How would I use one of these filters to show only discrete items?

http://imkaudio.com/error/nocolumnerror.bmp

And here is the code

Public Class Form1

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

'TODO: This line of code loads data into the 'Ct10DataSet.animal' table. You can move, or remove it, as needed.

Me.AnimalTableAdapter.Fill(Me.Ct10DataSet.animal)

End Sub

Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged

Me.AnimalBindingSource1.Filter = "Sex = " & ComboBox1.SelectedValue.ToString()

End Sub

End Class


The Accepted answer by ahmedilyas :

try this:

Me.AnimalBindingSource1.Filter = "Sex = '" & ComboBox1.Text & "'"
Find out cool Dot Net tutorials and also download projects source code on the dot net source