I’ve found a need to use Perforce with C# recently. The first step was to get the right DLLs for the job. I got the P4 API DLL from SourceForge here
Extracting it, I think you actually only need the p4API dll in the bin/CLR_2.0 directory, I took the whole directory just in case.
In your project right click on the References directory and ‘Add Reference’ and navigate to the p4api.dll.
// Load up the API
using P4API;
...
// Create the connection
P4Connection p4 = null;
try
{
p4 = new P4Connection(); // Uses the default connection to Perforce
p4.Connect();
// Don't throw an exception on a Perforce error. We handle these manually.
p4.ExceptionLevel = P4ExceptionLevels.NoExceptionOnErrors;
}
catch (System.Exception ex)
{
Console.WriteLine(ex.Message);
}
I want to delete a file from the repository so to do this I needed to just put through the right call. From what I’ve gathered you can run any P4 command that can be run via the command line, all you need to do is
try
{
if (p4 != null)
{
p4.RunUnParsed("delete", selectedItemPath);
}
}
catch (System.Runtime.InteropServices.COMException ex)
{
MessageBox.Show(ex.Message);
}
Done!
Hi,
I’ve just started using C# for the first time and decided to chuck up a little intro on how to connect with a Postgres database. This is a very stripped down tutorial, does the basics and doesn’t really handle exceptions, error states and the like. Firstly you need to get a few DLLs, Npgsql.dll, policy.2.0.Npgsql.dll and MonoSecurity.dll. You can get them from PgFoundry here.
I’ve put these in the same folder as the project. Go to Solution Explorer and locate the ‘References’ section under your project. Right click that and Add Reference. Click the ‘Browse’ tab and select the Npqsql dll only.
Firstly you need to load up the package/namespace (I’m not sure of the correct terminology here). Then you’re ready to connect to the database using the correct name, port, username and so on. This is simple.
using System;
...
using Npgsql;
....
NpgsqlConnection dbConnection = new NpgsqlConnection("Server=myServer;Port=xxxx;User Id=xyz;Password=xyz;Database=myDB;");
try
{
dbConnection.Open();
}
catch
{
//Handle error states here
}
That’s it! You’re now ready to execute queries and updates on the database.
I’ve included a very simple select statement below, it selects all from a hypothetical animal table and extracts the name and type column values from the results. The GetOrdinal is a useful helpful function to return you the integer that represents the index of the named column. It is more robust to changes in the database as indices may change but you need to check for NULLs as it won’t work (I’ve found out!)
NpgsqlCommand Command = new NpgsqlCommand("select name,type from animals", dbConnection);
NpgsqlDataReader result = Command.ExecuteReader();
while (result.Read())
{
String animalName = result.GetString(result.GetOrdinal("name"));
String animalType = result.GetString(result.GetOrdinal("type"));
... (Do something here)
}
Finally when you’re done with your database you need to close down the connection
dbConnection.Close();
dbConnection = null;
Thats it’s some very simple DB manipulations in very few lines of code, impressed with C# so far I must say!
Next step do things in transactions and handle exceptions!