[Python] Execute Python Script in VB.NET


Description
     Sometime in VB.NET we execute some shell script from third party like perl / phyton / other because it's may be have more advantage feature than .NET. this tutorial will help you to make python script can be execute from VB.NET

HowTo
1. Download Python Here (Official)
2. Write your python code , for example : print("Hello world python !!") in text file and save to .py
3. Create project at VB.NET, drag component 1 button and 1 textbox into form.
4. Double click button1, at event click button1 write this code 

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim proc As Process = New Process
        proc.StartInfo.FileName = "C:\Python34\python.exe" 'Default Python Installation
        proc.StartInfo.Arguments = pathmypythonfile.py
        proc.StartInfo.UseShellExecute = False 'required for redirect.
        proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden 'don't show commandprompt.
        proc.StartInfo.CreateNoWindow = True
        proc.StartInfo.RedirectStandardOutput = True 'captures output from commandprompt.
        proc.Start()
        AddHandler proc.OutputDataReceived, AddressOf proccess_OutputDataReceived
        proc.BeginOutputReadLine()
        proc.WaitForExit()

        TextBox1.Text = Value
    End Sub

    Public sub proccess_OutputDataReceived(ByVal sender As Object, ByVal e As DataReceivedEventArgs)
        On Error Resume Next
        If e.Data = "" Then
        Else
            Value = e.Data
        End If
    End sub 

5. Create module file, and write this variable global :
    Module module
           Public Value As String
    End Module

6. Running the application, if textbox1 have populated with some string then your code was success.

[Python] CRUD in database SQLServer


Description
     This tutorial will help you to make python can connect to database SQLServer and execute some command like CRUD (CREATE, UPDATE, READ, DELETE). the SQLServer library not include in default installation package, so you must download and install manually.     

HowTo
1. Install the latest Python.
2. Download library for SQLServer (Pymssql) from this site
3. After installation successfully, now you use the library. here's sample code for CRUD.


#--------------------------------START HERE-----------------------------------------------------------
import pymssql

conn = pymssql.connect(host='localhost', user='usrnm', password='passwd', Database='mydb')
cur = conn.cursor() 

#CREATE , INSERT , UPDATE, SELECT always use execute command
cur.execute('CREATE TABLE test(id INT, name VARCHAR(100))')
cur.execute("INSERT INTO test(id,name) VALUES('3','mahendra')")
cur.execute("UPDATE test set name='rony' where id='1'")
cur.execute("DELETE from test where id='1'")
conn.commit() # don't forget to commit after manipulating database

#To retrieve data / select you can do this
cur.execute('SELECT * from test')
row = cur.fetchone()

while row:
      #Index column fields in database always from 0
      print (row[0],row[1])
      row = cur.fetchone()

conn.close() #don't forget close connection after all process CRUD complete