Archive

Archive for the ‘C#’ Category

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: , , ,

Singleton App

May 23rd, 2009 sadegh No comments

Have you ever wanted to create a singleton app, that user just can run one instance of it.
Today i wanna talk about how to making our application capable to recognize whether another instance of app is running or not
if its running then we dont let user to run our app again.
for achieving that approach well use Name Mutex.
Mutex is a  WaitHandle-derived class , it has two state : owned or unowned
in program.cs instead of Application.Run well use SingletonApp.Run

program.cs :

static class Program
{
static void Main(  )
{
SingletonApp.Run(new MyForm(  ));
}
}

SingletonApp.cs :

public static class SingletonApp
{
static Mutex m_Mutex;

public static void Run(Form mainForm)
{
bool first = IsFirstInstance(  );
if(first)
{
Application.ApplicationExit += OnExit;
Application.Run(mainForm);
}
}
static bool IsFirstInstance(  )
{
Assembly assembly = Assembly.GetEntryAssembly(  );
string name = assembly.FullName;

m_Mutex = new Mutex(false,name);
bool owned = false;
owned = m_Mutex.WaitOne(TimeSpan.Zero,false);
return owned ;
}
static void OnExit(object sender,EventArgs args)
{
m_Mutex.ReleaseMutex(  );
m_Mutex.Close(  );
}
//Other overloaded versions of Run(  )
}

If no other instance of the application is running, the form MyForm is displayed. If there is another instance running, the application exits. The SingletonApp class provides the same Run( ) methods as the Application class.

Categories: C# Tags: