Archive

Archive for the ‘programming’ Category

Automating your tasks , AutoIT

August 19th, 2009 sadegh 1 comment
MsgBox(0, "nothingelz.com", "Hello World!")

The above line is a simple code which shows a Message Box. You just need to open notepad write it down and save it with “.au3” extension, oh I was almost forgetting before that you need to go to http://www.autoitscript.com/autoit3/downloads.shtml and download AutoIt . Unfortunately Windows is only supported operation system.

AutoIt

AutoIt

Well thats it, You created your first AutoIt script.  It’s cool, isn’t it  ? AutoIT is actually really good for small daily work, you can write small script in AutoIt to do your daily chores

e.g. I use following script everyday morning when I arrive at work :

$answer = MsgBox(4, "nothingelz.com", "Good morning , do you want me to start Visual Studio and SQL Server Management Studio and Firefox ?")

If $answer = 7 Then

MsgBox(0, "nothingelz.com", "OK.  c ya!")

Exit

EndIf

Run("C:\Program Files\Mozilla Firefox\firefox.exe") ; firefox huh ?

Run("C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\devenv.exe") ;visual studio

Run("C:\Program Files\Microsoft SQL Server\90\Tools\Binn\VSShell\Common7\IDE\SqlWb.exe") ;sql …

It will ask me whether or not I want it to run Firefox , Visual Studio and Sql Server Management Studio ( these are application that I use everyday at work ) so I do not need to put all of these apps short cut on my desktop and also with one click all of them will be lunched ;)

So lets analyze Above code , in first line I called MsgBox function and put its returns value to variable called $answer. MsgBox accepts three parameters , the first one is called flag which indicate which kind of Message box we want with which kinda buttons combination

0 OK button
1 OK and Cancel
2 Abort, Retry, and Ignore
3 Yes, No, and Cancel
4 Yes and No
5 Retry and Cancel
6 ** Cancel, Try Again, Continue

This table shows  possible value for flag parameter, as you see we used 4 which means that we want Yes/No MsgBox.

The second parameter is Message Box Title and the third one is its message. This function return a value , possible returned values includes :

Button Pressed Return Value
OK 1
CANCEL 2
ABORT 3
RETRY 4
IGNORE 5
YES 6
NO 7
TRY AGAIN ** 10
CONTINUE ** 11

In  conditional statement ( If statement ) we check whether user clicked on “No” or not ( in the above table you see that if our return value is 7 , it means user clicked on “No”) . if it was correct we move on next line and show a message to user and then we call “Exit” which exit our script otherwise we skip it and go to the whatever comes after “Endif” .

Then we call Run function, it accepts a path to a file and run it. We just call it three times with path to Firefox , Visual Studio and Sql Server Management Studio.

That’s it, You can change parameter of “Run” and point it to the application which you use. Almost everything else in AutoIt is the same as above code.  A Complete Help comes along with AutoIt which contains list of all function and their parameters and return value and purpose of them which is really helpful.

AutoIt is really cool scripting language you can learn it very soon and it has a rich library, full of useful functions.

Categories: programming, windows Tags: ,

Using foxpro database file (dbf) with c#

July 15th, 2009 sadegh 1 comment

Have your ever needed to connect to foxpro databases ?

Hopefully not , but sometimes you need , and the last day I needed.

Well you can easily OleDb classes defined in System.Data.OleDb namespace

to connecting and working with your database

But beforehand you need to installVisual FoxPro Ole DB Provider , you can get it from here

Well now its easy to connect to you dbf file with OleDbCommand and OleDbConnection and … dont forget to specify that you are using foxpro in your connection string , a simple connection string would be

Provider=vfpoledb.1;Data Source=C:\db\


c:\db\ is where your database file resides, but be careful that if you have a database file called mydb.dbf and its in c:\db\ then if you wanna get all rows of it you should just put database file location in connection string and database file name itself is a table name so in OleDbCommand we write

select * from mydb

now you can do whatever you want with your database

but just memo field in foxpro needs a trick

you can easily set any value to your memo field , but that value must be lower than 255 character

and we all know that Memo fields are limited only by the amount of available disk space so its a lot bigger than 255 character

the workaround is dividing your value into 255 charactered values.

I mean that “update mydb set memofield = ‘ string with more than 255 character ‘” wouldnt work but if you divide string into 255 charactered values like this “update mydb set memofield = ‘ string with 255 character ‘ + ‘ string with 255 character ‘ + ‘ string with 255 character ‘ ”

it would work

here is a sample code in c# , it gets all rows of mydb database

var oConn = new OleDbConnection(@"Provider=vfpoledb.1;Data Source=C:\db\");

oConn.Open();

using (var oCom = new OleDbCommand("select * from mydb", oConn))

{

var oReader = oCom.ExecuteReader();

while(oReader.Read())

{

// do what you wanna do !

}

}

oConn.Close();

well , till next time ;)

p.s. DBFView is a cool tool for opening dbf file , check it out here
Categories: C#, programming Tags: , , ,