Main

.NET Archives

September 17, 2008

ODP.NET with Visual C++ 2008


Many developers still using C++ for some reasons. One of my customers is using VC++ but with open source database and he is not that happy with the database so he wants to change to Oracle database. This is a simple sample that I build for him with 1 button and 1 listbox.

When I was doing few lines of code for this sample I realize 2 not quite happy scenario in VC++; 1. The Oracle DataAccess Class does not appear in blue for example, OracleConnection, as compare to C#. 2. The intellisense is not as strong when comparing to C# as well.

The following are the few lines of code that in the sample:

static String ^ORA_CONNECTION_STRING = "Data Source=orcldemo;Persist Security Info=True;User ID=HR;Password=hr;";

private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {

       OracleConnection ^conn = gcnew OracleConnection(ORA_CONNECTION_STRING);

       conn->Open();

 

       OracleCommand ^cmd = gcnew OracleCommand();

       cmd->Connection = conn;

 

       cmd->CommandText = "select department_id, department_name, city"

                + " from departments d, locations l" + " where d.location_id =

                   l.location_id";

 

        OracleDataReader ^dr = cmd->ExecuteReader();

 

       while (dr->Read())

       {

         listBox1->Items->Add("The " + dr->GetString(1) +

                           " department is in " + dr->GetString(2));

       }

 

       conn->Close();      

     }

 

Download the sample file from http://skydrive.live.com. The sample file name is WinAppCpp.zip. My MSN ID is chanmmn@hotmail.com

September 28, 2008

Oracle.DataAccess.dll in Windows Server 2008 x64

After I installed ODP.NET 11g ODTwithODAC1110621.zip in Windows Server 2008 x64 I got the following error message:
Could not load file or assembly 'Oracle.DataAccess, Version=2.111.6.20, Culture=neutral, PublicKeyToken=89b483f429c47342' or one of its dependencies. An attempt was made to load a program with an incorrect format. (http://forums.oracle.com/forums/thread.jspa?messageID=2732111)

So, I changed the ODP.NET 10g ODTwithODAC10202.exe. Well, there was no luck and I was still getting the same error message from Visual Studio 2008.

Thanks to Mark William answered my question in the forum http://forums.oracle.com/forums/thread.jspa?threadID=706071&tstart=0.

Finally, Oracle.DataAccess.dll is able to open the connection to connect to Oracle Database 11g without an error. At the same time can create Data Sources without error too.

Somehow, in order to do that, I need to install both Oracle Client for Windows Server 2008 x64 and Oracle Client for Windows Server 2008 x86 download from the following link:
http://www.oracle.com/technology/software/products/database/index.html

October 12, 2008

Write Table content from Database to XML file


I found the easiest way to write a table or even tables from database to XML file is in.NET languages. To do this in Java you might probably need to include not only one but few proprietary library or open source jar file.

 

To do this in C#, one of the .NET languages, there is no additional reference need to be added to the project. Only using System.Data is needed because of the Dataset.

 

The code segment that writes dataset to XML file as follow (the code segment is from MSDN library):

 

    static void WriteXmlToFile(DataSet thisDataSet)

    {

      if (thisDataSet == null) { return; }

 

      // Create a file name to write to.

      string filename = "XmlDoc.xml";

 

      // Create the FileStream to write with.

      System.IO.FileStream stream = new System.IO.FileStream

          (filename, System.IO.FileMode.Create);

 

      // Write to the file with the WriteXml method.

      thisDataSet.WriteXml(stream);

}

 

The complete code to extract the data from employee table of HR schema to XML from is in http://skydrive.live.com. The sample file name is ConsoleDatasettoXML .zip. My MSN ID is chanmmn@hotmail.com.

October 19, 2008

The myth of LINQ (simplest sample for ADO.NET Entity Data Model)


If you are a .NET fan out there then you will basically hearing from many people asking, I have ADO.NET 2.0 so why is there a LINQ? It was a myth to me too. After Microsoft launch Service Pack 1 for Visual Studio 2008 then I clear some of my cloud in my head.

I will only mention one of the unique features here, ADO.NET Entity Data Model from ADO.NET Entity Framework. I have not found a better way to bind the Entity Model to DataGridView web control.

In order to create ADO.NET Entity Data Model, right click either Windows Application Project or Web Application Project and choose Add New Item… . Select ADO.NET Entity Data Model from the Add New Item dialog. After that you will have a file with edmx extension.

I have created a complete Windows Application sample using Northwind database in MS SQL 2008 Express. The Entity named NorthwindEntities. The entire sample consisted of 2 controls, 1 DataGridView and another is Button. I only have few lines of code in Button click event as below.

 private void btnGetCustomers_Click(object sender, EventArgs e)

{

  NorthwindEntities nwe = new NorthwindEntities();

 

  var cus = from customer in nwe.Customers

            select customer;

 

  dataGridView1.DataSource = cus;

}

I guess no one will ever say this is difficult.

The complete code is in http://skydrive.live.com. The sample file name is WinAppEntities.rar. My MSN ID is chanmmn@hotmail.com.


Unfortunately, ADO.NET Entity Data Model is not working with Oracle Database yet.

October 25, 2008

Write a simple object to and from XML using C#


Write a simple object to and from XML using C#

Write to XML file is always easier than read from XML file. Not too sure what I mean, it is alright because I have a sample for you to understand and comment.

In this sample I have a class BankAccount. I will serialize the BankAccount object to XML with 2 properties and use XMLReader to get back what have been written to the XML file (in the sample the file name for XML file is XMLBank.xml).

For this sample I glue the WriteXML and ReadXML method with the BankAccount. The entire class as below: 

public class BankAccount

  {

    public String AccID { get; set; }

    public double Amount { get; set; }

 

    public void WriteXML()

    {

      XmlSerializer serializer;

      TextWriter writer = new StreamWriter("XMLBank.xml");

 

      BankAccount bc = new BankAccount();

      bc.AccID = "13-0001";

      bc.Amount = 1000000;

 

      serializer =  new XmlSerializer(typeof(BankAccount));

      serializer.Serialize(writer, bc);

      writer.Close();

    }

 

    public void ReadXML()

    {

      XmlReader reader = XmlReader.Create("XMLBank.xml");

 

      reader.Read();

      reader.ReadStartElement("BankAccount");

      reader.ReadStartElement("AccID");

      Console.WriteLine(reader.ReadString());

      reader.ReadToFollowing("Amount");

      Console.WriteLine(reader.ReadString());

    }

  }

Doing it this way then the application will not tie down to any database such as Oracle Database or MS SQL.  If you have better and easier link then do share with me.

The complete code is in http://skydrive.live.com. The sample file name is ConsoleObjtoXML.rar. My MSN ID is chanmmn@hotmail.com.


Resources:


A Beginner's Guide to the XML DOM


XML Technology Center

About .NET

This page contains an archive of all entries posted to Ming Man Chan's Blog in the .NET category. They are listed from oldest to newest.

Database is the next category.

Many more can be found on the main index page or by looking through the archives.

Top Tags

Powered by
Movable Type and Oracle