tags:

views:

93

answers:

4

Is there a way i can create a struct or class, fill it with data and dump/read it to and from my DBs?

+3  A: 

Yes, C# offers many ways to connect to databases.

If you have C# 3.0, look into Linq To SQL, that should give you what you're looking for.

Edit: Come to think of it, is there any (semi-modern) programming language that doesn't have that functionality in it?

Matt Grande
I don't know of a language with DB support built-in (except maybe PHP). There are however many frameworks with that capability.
Bryan Watts
+1  A: 

There are many ways depending on your needs.

Darin Dimitrov
+4  A: 

This sounds like you more or less need a basic introduction to connecting and manipulating a database from C#. The above poster said to look into LINQ to SQL, but you can also look into the more basic underlying framework of ADO.NET which will get you to understand the basics of how it works.

Also, you can use this site right here for a number of different database tutorials for C#.

Edit: More information from C# Station, CodeProject, and Codersource

Edit 2: If you are interested in things like Linq to SQL as others have mentioned above, here are some tutorials from C# Corner, and C-Sharp Online

Edit 3: Others would also suggest things such as the ADO.NET Entity Framework. I would not necessarily suggest this for beginners who still need to grasp the fundamentals of working with a database. Here is some information from the MSDN Overview

Simple Example (This is pulled directly from the C# Station link given above)

Listing 1. Using a SqlConnection

using System;
using System.Data;
using System.Data.SqlClient;

/// <summary>
/// Demonstrates how to work with SqlConnection objects
/// </summary>
class SqlConnectionDemo
{
    static void Main()
    {
        // 1. Instantiate the connection
        SqlConnection conn = new SqlConnection(
            "Data Source=(local);Initial Catalog=Northwind;
             Integrated Security=SSPI");

        SqlDataReader rdr = null;

        try
        {
            // 2. Open the connection
            conn.Open();

            // 3. Pass the connection to a command object
            SqlCommand cmd = 
                new SqlCommand("select * from Customers", conn);

            //
            // 4. Use the connection
            //

            // get query results
            rdr = cmd.ExecuteReader();

            // print the CustomerID of each record
            while (rdr.Read())
            {
                Console.WriteLine(rdr[0]);
            }
        }
        finally
        {
            // close the reader
            if (rdr != null)
            {
                rdr.Close();
            }

            // 5. Close the connection
            if (conn != null)
            {
                conn.Close();
            }
        }
    }
}
TheTXI
+1 learning this before hopping on the ORM train is a good idea.
SnOrfus
@SnOrfus: exactly. There are plenty of things out there which can help make things easier, but only if you have a basic understanding of what is happening beneath the surface. Otherwise you're just going to tie yourself up in knots.
TheTXI
A: 

If you jut want a simple ORM, look at Activerecord which is based on NHibernate There is also XPO from www.devexpress.com And another approach is DB4O (DB for Objects) www.db40.com

feihtthief