Andre Broers’ personal blog

May 22, 2008

Linq to Oracle Autogenerated Key

Filed under: .net, linq, oracle, windows — Tags: , , , — broersa @ 1:38 pm

We build on previous sample. In this example we add an autogenerated primary key. This is done by adding a sequence and an insert trigger in oracle:

SQL> create sequence degrees_seq
  2  start with 1
  3  increment by 1
  4  nomaxvalue;

Sequence created.

SQL>
SQL> create trigger degrees_trigger
  2  before insert on degrees
  3  for each row
  4  begin
  5  select degrees_seq.nextval into :new.degkey from dual;
  6  end;
  7  /

Trigger created.

SQL>

As we can see in linq.cs generated file the column DEGKey is attached with the IsDbGenerated = true attribute. This says that the .Net environment knows the column is updated from the database, which is what we want.

Now we get a new sample oracletest2.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using DbLinq.Linq.Mapping;
using System.Data.OracleClient;

class OracleTest
{
  static void Main(string[] args)
  {
     string connStr = “User Id=linq;Password=linq;Data Source=XE”;

     Linq db = new Linq(new OracleConnection(connStr)); // Linq is the mapperclass created with DbMetal

     Console.WriteLine(“Insert temperature 1:”);
     Degrees a = new Degrees();
     a.DEGKey = 1; // Must be set but doesn’t matter because is database generated
     a.DEGDate = System.DateTime.Now;
     a.DEGCeLcIUs = 1;
     db.Degrees.InsertOnSubmit(a);
     System.Threading.Thread.Sleep(5000);
     Console.WriteLine(“Insert temperature 2:”);
     Degrees b = new Degrees();
     b.DEGKey = 1; // Must be set but doesn’t matter because is database generated
     b.DEGDate = System.DateTime.Now;
     b.DEGCeLcIUs = 2;
     db.Degrees.InsertOnSubmit(b);
     System.Threading.Thread.Sleep(5000);
     Console.WriteLine(“Insert temperature 3:”);
     Degrees c = new Degrees();
     c.DEGKey = 1; // Must be set but doesn’t matter because is database generated
     c.DEGDate = System.DateTime.Now;
     c.DEGCeLcIUs = 3;
     db.Degrees.InsertOnSubmit(c);
     Console.WriteLine(“Key values before submit:”);
     Console.WriteLine(“a.DEGKey:”+a.DEGKey);
     Console.WriteLine(“b.DEGKey:”+b.DEGKey);
     Console.WriteLine(“c.DEGKey:”+c.DEGKey);
     db.SubmitChanges();
     Console.WriteLine(“Key values after submit (Should be synchronized but are not)”);
     Console.WriteLine(“a.DEGKey:”+a.DEGKey);
     Console.WriteLine(“b.DEGKey:”+b.DEGKey);
     Console.WriteLine(“c.DEGKey:”+c.DEGKey);

     Console.WriteLine(“——”);

     Console.WriteLine(“Select all temperatures (now the keys are synced): “);
     var q3 = from p in db.Degrees select p;
     Console.WriteLine(“Fired sql:”);
     Console.WriteLine(db.GetQueryText(q3));
     Console.WriteLine(“Result:”);
     Console.WriteLine(“DEGKEY – DEGDATE – DEGCELCIUS”);
     foreach (var v in q3)
     {
       Console.WriteLine(v.DEGKey + ” – ” + v.DEGDate + ” – ” + v.DEGCeLcIUs);  // For some reason DbMetal makes a very cryptic property name
     }
     Console.WriteLine(“——”);
   }
 }

Mind the comments in the code. A flaw in dblinq is that on the SubmitChanged the primarykey field should be updated which aren’t. For the rest it works nice.

build:

csc /r:dblinq.dll,dblinq.oracle.dll linq.cs oracletest2.cs

and run:

oracletest2.exe

have fun..

Linq for Oracle sample

Filed under: .net, linq, oracle, windows — Tags: , , , — broersa @ 11:21 am

Start with a database in my situation XE on localhost.

C:\>sqlplus / as sysdba
SQL*Plus: Release 10.2.0.1.0
Copyright (c) 1982, 2005, Oracle.  All rights reserved.
Connected to:
Oracle Database 10g Express Edition Release 10.2.0.1.0 - Production
SQL> create user linq identified by linq;
User created.
SQL> grant connect,resource to linq;
Grant succeeded.
SQL> connect linq/linq
Connected.
SQL> create table degrees
  2  (
  3    degkey number primary key
  4   ,degdate date
  5   ,degcelcius number
  6  )
  7  /
Table created.
SQL>

Download the dblinq library from : http://code2code.net/DB_Linq/

or get the latest via svn which has VisualMetal:
svn checkout http://dblinq2007.googlecode.com/svn/trunk/

Goto the directory and do the following to build the oracle libraries:

cd DbLinq.Oracle
msbuild

This will build the DbLinq.dll and DbLinq.Oracle.dll in bin\Debug

cd DbMetal
msbuild

This will build the DbMetal tool which creates the database to objects mapper classes. Ignore the copy errors at the end, it works without the copying because the oracle assemblies are in the GAC. DbMetal is created in the bin directory.

Run the command to create the mapper classes.

dbmetal /provider:oracle /conn:”Data Source=XE;User Id=linq;Password=linq;” /database=linq /code:linq.cs

create a new working directory (c:\dotnet\oracle) and copy the linq.cs, DbLinq.dll and DbLinq.Oracle.dll to this directory.

Create the test program oracletest.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using DbLinq.Linq.Mapping;
using System.Data.OracleClient;

class OracleTest
{
static void Main(string[] args)
{
string connStr = “User Id=linq;Password=linq;Data Source=XE”;

Linq db = new Linq(new OracleConnection(connStr)); // Linq is the mapperclass created with DbMetal

Console.WriteLine(“Select and delete all temperatures:”);
var q1 = from p in db.Degrees select p;
//db.Degrees.RemoveAll(q1); // Not implemented :-(
Console.WriteLine(“Fired sql:”);
Console.WriteLine(db.GetQueryText(q1));
Console.WriteLine(“Result:”);
foreach (var v in q1)
{
Console.WriteLine(v.DEGKey + ” – ” + v.DEGDate + ” – ” + v.DEGCeLcIUs); // For some reason DbMetal makes a very cryptic property name
db.Degrees.DeleteOnSubmit(v);
}
db.SubmitChanges();
Console.WriteLine(“——”);

Console.WriteLine(“Select temperatures (none):”);
var q2 = from p in db.Degrees select p;
Console.WriteLine(“Fired sql:”);
Console.WriteLine(db.GetQueryText(q2));
Console.WriteLine(“Result:”);
Console.WriteLine(“DEGKEY – DEGDATE – DEGCELCIUS”);
foreach (var v in q2)
{
Console.WriteLine(v.DEGKey + ” – ” + v.DEGDate + ” – ” + v.DEGCeLcIUs); // For some reason DbMetal makes a very cryptic property name
}
Console.WriteLine(“——”);

Console.WriteLine(“Insert temperature 1:”);
Degrees a = new Degrees();
a.DEGKey = 1;
a.DEGDate = System.DateTime.Now;
a.DEGCeLcIUs = 1;
db.Degrees.InsertOnSubmit(a);
System.Threading.Thread.Sleep(5000);
Console.WriteLine(“Insert temperature 2:”);
Degrees b = new Degrees();
b.DEGKey = 2;
b.DEGDate = System.DateTime.Now;
b.DEGCeLcIUs = 2;
db.Degrees.InsertOnSubmit(b);
System.Threading.Thread.Sleep(5000);
Console.WriteLine(“Insert temperature 3:”);
Degrees c = new Degrees();
c.DEGKey = 3;
c.DEGDate = System.DateTime.Now;
c.DEGCeLcIUs = 3;
db.Degrees.InsertOnSubmit(c);

db.SubmitChanges();
Console.WriteLine(“——”);

Console.WriteLine(“Select all temperatures:”);
var q3 = from p in db.Degrees select p;
Console.WriteLine(“Fired sql:”);
Console.WriteLine(db.GetQueryText(q3));
Console.WriteLine(“Result:”);
Console.WriteLine(“DEGKEY – DEGDATE – DEGCELCIUS”);
foreach (var v in q3)
{
Console.WriteLine(v.DEGKey + ” – ” + v.DEGDate + ” – ” + v.DEGCeLcIUs); // For some reason DbMetal makes a very cryptic property name
}
Console.WriteLine(“——”);

Console.WriteLine(“Select all temperatures higher than 1 and add 5 to these:”);
var q4 = from p in db.Degrees where p.DEGCeLcIUs>1 select p;
Console.WriteLine(“Fired sql:”);
Console.WriteLine(db.GetQueryText(q4));
Console.WriteLine(“Result:”);
Console.WriteLine(“DEGKEY – DEGDATE – DEGCELCIUS”);
foreach (var v in q4)
{
Console.WriteLine(v.DEGKey + ” – ” + v.DEGDate + ” – ” + v.DEGCeLcIUs); // For some reason DbMetal makes a very cryptic property name
}
foreach (var v in q4)
{
v.DEGCeLcIUs+=5;
}
db.SubmitChanges();
Console.WriteLine(“——”);

Console.WriteLine(“Select all temperatures:”);
var q5 = from p in db.Degrees select p;
Console.WriteLine(“Fired sql:”);
Console.WriteLine(db.GetQueryText(q5));
Console.WriteLine(“Result:”);
Console.WriteLine(“DEGKEY – DEGDATE – DEGCELCIUS”);
foreach (var v in q5)
{
Console.WriteLine(v.DEGKey + ” – ” + v.DEGDate + ” – ” + v.DEGCeLcIUs); // For some reason DbMetal makes a very cryptic property name
}
Console.WriteLine(“——”);

}
}

Compile and run:

csc /r:dblinq.dll,dblinq.oracle.dll *.cs

oracletest:

Select and delete all temperatures:
Fired sql:
SELECT p$.DEGCELCIUS, p$.DEGDATE, p$.DEGKEY
 FROM LINQ.DEGREES p$
Result:
1 - 5/22/2008 7:07:48 PM - 1
2 - 5/22/2008 7:07:53 PM - 2
3 - 5/22/2008 7:07:58 PM - 3
------
Select temperatures (none):
Fired sql:
SELECT p$.DEGCELCIUS, p$.DEGDATE, p$.DEGKEY
 FROM LINQ.DEGREES p$
Result:
DEGKEY - DEGDATE - DEGCELCIUS
------
Insert temperature 1:
Insert temperature 2:
Insert temperature 3:
------
Select all temperatures:
Fired sql:
SELECT p$.DEGCELCIUS, p$.DEGDATE, p$.DEGKEY
 FROM LINQ.DEGREES p$
Result:
DEGKEY - DEGDATE - DEGCELCIUS
1 - 5/22/2008 7:10:14 PM - 1
2 - 5/22/2008 7:10:19 PM - 2
3 - 5/22/2008 7:10:24 PM - 3
------
Select all temperatures higher than 1 and add 5 to these:
Fired sql:
SELECT p$.DEGCELCIUS, p$.DEGDATE, p$.DEGKEY
 FROM LINQ.DEGREES p$ WHERE p$.DEGCELCIUS > 1
Result:
DEGKEY - DEGDATE - DEGCELCIUS
2 - 5/22/2008 7:10:19 PM - 2
3 - 5/22/2008 7:10:24 PM - 3
------
Select all temperatures:
Fired sql:
SELECT p$.DEGCELCIUS, p$.DEGDATE, p$.DEGKEY
 FROM LINQ.DEGREES p$
Result:
DEGKEY - DEGDATE - DEGCELCIUS
1 - 5/22/2008 7:10:14 PM - 1
2 - 5/22/2008 7:10:19 PM - 7
3 - 5/22/2008 7:10:24 PM - 8
------

Have fun…

April 18, 2008

Delay Signing an Assembly

Filed under: .net, windows — Tags: , — broersa @ 1:45 pm

In this sample I will demonstrate the use of a delay signed assembly.

When an assembly is delay signed, the public key is added to the assembly. Delay signed assemblies can be referenced to even though they are not signed with the private key. This is especially handy while developping because in most circumstances not everyone has access to the final private key. In this blog I will show how this process works.

First the code

helloassembly.cs:


using System;

namespace helloassembly {
  public class Hello
  {
        public string SayHello(string name)
        {
            return "Hello " + name;
        }
  }
}

hello.cs:


using System;
using helloassembly;

public class HelloExe
{
        [STAThread]
        static void Main(string[] args)
        {
            Hello x = new Hello();
            Console.WriteLine(x.SayHello("Andre"));
        }
}

create the keypair:
sn -k my.sln

create the public key:
sn -p my.sln mypublic.sln

compile the assembly without signing:
csc /target:library helloassembly.cs

compile the exe:
csc /target:exe /r:helloassembly.dll Hello.cs

run the exe – Works..

sign the helloassembly with key (won’t work because it’s not a strongly named)
sn -R helloassembly.dll my(public).sln

recompile the helloassembly signed:
csc /target:library /keyfile:my.sln helloassembly.cs

run the hello.exe – Won’t work because hello is compiled against a nonsigned assembly

recompile the hello.cs:
csc /target:exe /r:helloassembly.dll Hello.cs

run the hello.exe – Works

resign helloassembly:
sn -R helloassembly.dll my.sln

run the hello.exe – Works

recompile the helloassembly with delay signing:

csc /target:library /keyfile:mypublic.sln /delaysign+ helloassembly.cs

run hello.exe – Fails because the helloassembly is not strong signed.

recompile hello.exe:
csc /target:exe /r:helloassembly.dll Hello.cs
this works because the assembly is delay signed.

run hello.exe – Fails because the helloassembly is not strong signed.

add the helloassembly to the verifications on the local machine:
sn -Vr helloassembly.dll
sn -Vl

run hello.exe – Works because hello.exe is allowed to reference helloassembly

remove the helloassembly
sn -Vu helloassembly.dll
sn -Vl

run hello.exe – Fails as expected

sign the delay signed helloassembly:
sn -R helloassembly.dll my.sln

run hello.exe – Works without recompiling.

I have explained pretty much of the possibilities.

Have fun..

Sign a .Net assembly

Filed under: .net, windows — Tags: , — broersa @ 9:03 am

A .Net assembly can be easily tampered with. Using the ildasm one can get and alter the sourcecode of an assembly (as explained in a previous blog). The sollution is to sign the assembly to be sure it is not tampered with.

Let’s start with the Hello.cs assembly:


using System;

public class Hello
{
    [STAThread]
    static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
        }
}

To sign we need a keyset (private and public).
sn -k myKeySet.sln

compile the Hello.cs with this keyfile:
csc /keyfile:myKeySet.sln /target:exe /out:Hello.exe Hello.cs

Now we have a signed assembly. When we try to alter this with the steps in previous blog we get the following error:

Unhandled Exception: System.IO.FileLoadException: Could not load file or assembly ‘Hello, Version=0.0.0.0, Culture=neutral, PublicKeyToken=707e1a34ff51325c’ or one of its dependencies. Strong name validation failed. (Exception from HRESULT: 0×8013141A)
File name: ‘Hello, Version=0.0.0.0, Culture=neutral, PublicKeyToken=707e1a34ff51325c’ —> System.Security.SecurityException: Strong name validation failed. (Exception from HRESULT: 0×8013141A)
The Zone of the assembly that failed was:
MyComputer

When we assemble with the keyfile again we resign the new (altered) assembly. Of course this can’t be done if you don’t own the private key part of the original signer ;-) . Also the hash of the assembly will be different so all assemblies referencing this assembly have to be recompiled. In short words: You can’t alter a signed assembly.

ilasm Hello.il /out:Hello2.exe /res:Hello.res /key:myKeySet.sln

or

sn -R hello2.exe myKeySet.sln

This results in a new (and definitly other) assembly.

De-assemble and Re-assemble a .Net assembly

Filed under: .net, windows — Tags: , — broersa @ 8:26 am

A .Net assembly can be de-assembled pretty easy. In this blog I create an assembly and will de-assemble, alter and re-assemble the assembly.

Let’s start with the famous Hello.cs:


using System;

public class Hello
{
    [STAThread]
    static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
        }
}

build it:

csc /target:exe Hello.cs

de-assemble:

ildasm /out:Hello.il Hello.exe

edit the string “Hello World!” in the Hello.il file to something like “Hello Andre Broers!”

assemble:

ilasm /out:Hello2.exe /res:Hello.res Hello.il

Now we have the altered assembly.

By signing the assembly we can get sure the assembly isn’t tampered with. I will explain this in a next blog.

April 15, 2008

Install subversion on Windows

Filed under: subversion, svn, windows — Tags: , , — broersa @ 7:08 am

First thing is to download and install the svn binaries from : tigris

after this open a cmd shell and issue the command:

svnadmin create “c:\svnrep\”

use the editor and edit the file c:\svnrep\conf\svnserve.conf and uncomment anon-access, auth-access and password-db

### This file controls the configuration of the svnserve daemon, if you
### use it to allow access to this repository.  (If you only allow
### access through http: and/or file: URLs, then this file is
### irrelevant.)

### Visit http://subversion.tigris.org/ for more information.

[general]
### These options control access to the repository for unauthenticated
### and authenticated users.  Valid values are "write", "read",
### and "none".  The sample settings below are the defaults.
anon-access = read
auth-access = write
### The password-db option controls the location of the password
### database file.  Unless you specify a path starting with a /,
### the file's location is relative to the conf directory.
### Uncomment the line below to use the default password file.
password-db = passwd
### The authz-db option controls the location of the authorization
### rules for path-based access control.  Unless you specify a path
### starting with a /, the file's location is relative to the conf
### directory.  If you don't specify an authz-db, no path-based access
### control is done.
### Uncomment the line below to use the default authorization file.
# authz-db = authz
### This option specifies the authentication realm of the repository.
### If two repositories have the same authentication realm, they should
### have the same password database, and vice versa.  The default realm
### is repository's uuid.
# realm = My First Repository

Edit the c:\svnrep\conf\passwd file and uncomment the default users:

### This file is an example password file for svnserve.
### Its format is similar to that of svnserve.conf. As shown in the
### example below it contains one section labelled [users].
### The name and password for each user follow, one account per line.

[users]
harry = harryssecret
sally = sallyssecret

Now the server can be started:

svnserve –daemon –root “c:\svnrep\”

Open another cmd shell:

set SVN_EDITOR=c:\winnt\notepad.exe

C:\>svn mkdir svn://localhost/myproject

The favourite editor should come up and we can edit a description for the new project. after saving and exiting the process goes on.

Authentication realm: <svn://localhost:3690> bb044873-667c-5c43-ab97-faafa55ddce9
Password for ‘abr’: *********
Authentication realm: <svn://localhost:3690> bb044873-667c-5c43-ab97-faafa55ddce9
Username: sally
Password for ’sally’: ************

Committed revision 1.

C:\>svn ls svn://localhost/
myproject/

C:\>

The first project is created.

February 20, 2008

Install sun jdk 6 on ubuntu gutsy

Filed under: java, linux, ubuntu — Tags: , , — broersa @ 1:33 pm

Use the following to install the sun java developers kit on ubuntu gutsy:

sudo apt-get install sun-java6-jdk sun-java6-plugin sun-java6-fonts
sudo update-java-alternatives –set java-6-sun

February 18, 2008

Install Wine in Ubuntu Gutsy

Filed under: linux, ubuntu — Tags: , , — broersa @ 8:11 pm

[update]

I think this this blog item is outdated and there is another way to install wine. Please “google” to another installation instruction.

[/update] 

 

First add the repository to ubuntu:

sudo wget http://wine.budgetdedicated.com/apt/sources.list.d/gutsy.list -O /etc/apt/sources.list.d/gutsy-winehq.list

Than add the key:

wget -q http://wine.budgetdedicated.com/apt/387EE263.gpg -O- | sudo apt-key add -

Than update the repository:

sudo apt-get update

Than install wine:

sudo apt-get install wine

Install the medibuntu repository

Filed under: linux, ubuntu — broersa @ 7:47 pm

 I copied a snipped from the medibuntu website to explain what it is and how to add the repository to the sources.list. See this link to get the whole page:

https://help.ubuntu.com/community/Medibuntu

Introduction

[WWW] Medibuntu (Multimedia, Entertainment & Distractions In Ubuntu) is a repository of packages that cannot be included into the Ubuntu distribution for legal reasons (copyright, license, patent, etc).

Some of these packages include the [WWW] libdvdcss package from [WWW] VideoLAN and the external binary codecs package (commonly known as w32codecs) used by [WWW] MPlayer and [WWW] xine.

Install in Gutsy

  • Ubuntu 7.10 “Gutsy Gibbon”:
    sudo wget http://www.medibuntu.org/sources.list.d/gutsy.list -O /etc/apt/sources.list.d/medibuntu.list
  • Then, add the GPG Key:
    wget -q http://packages.medibuntu.org/medibuntu-key.gpg -O- | sudo apt-key add - && sudo apt-get update

Ubuntu Gutsy Radeon Dual Head

Filed under: linux, ubuntu — broersa @ 7:31 pm

I have a radeon 7000 ve card with two lcd monitors with a resolution of 1024×768. To get them working in Gutsy I added the following Virtual line in the Screen section of my /etc/X11/xorg.conf:

Section "Screen"
        Identifier      "Default Screen"
        Device          "ATI Technologies Inc Radeon RV100 QY [Radeon 7000/VE]"
        Monitor         "C384FA-M"
        DefaultDepth    24
        SubSection "Display"
                Virtual         2048 768
                Modes           "1024x768" "800x600" "640x640"
        EndSubSection
EndSection

After a logout / login I give the command:

xrandr --output DVI-0 --right-of VGA-0

Now the display is in dual head mode. Because the total display size of the radeon7000/ve card is smaller than the 2048×768 virtual desktop the right side of the desktop isn’t refreshed. I had a work around by opening a permanent firefox browser on my right monitor.

When you put the xrandr statement in the .profile the dual head is always enabled when you log on.

« Newer PostsOlder Posts »

Blog at WordPress.com.