1 Replies - 2321 Views - Last Post: 03 August 2011 - 04:11 AM Rate Topic: -----

#1 RKrogh   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 26-April 11

SQL: How do I write to a database?

Posted 03 August 2011 - 03:53 AM


I'm new to working with SQL and I need help how I can write a save-method. With some help I have a Load-method which look like:

private readonly List<Player.Player> internalList;

public void LoadAll()
            Player.Player player = new Player.Player();

            using (SqlConnection connection = new SqlConnection(ConnectionString))
                using (SqlCommand command = connection.CreateCommand())
                    command.CommandText = "SELECT id, name, wins, lost FROM players";
                    //command.CommandText = "INSERT INTO players VALUES()";
                    //command.CommandText = "UPDATE players WHERE id=" + player.ID;
                    IDataReader reader = command.ExecuteReader();
                    while (reader.Read())
                        Player.Player dbitem = new Player.Player();
                        dbitem.ID = reader.GetGuid(0);
                        dbitem.Name = reader.GetString(1);

I have an object Player where I like to save and load the ID(GUID), Name (string), Wins(int), Lost(int).

How do I choose the elements and save them to the database?
I've been looking for IDataWriter or something similar but I haven't got a good tutorial on this at all.

Thank you in advance!
//Beginner in C#: Robert

Is This A Good Question/Topic? 0
  • +

Replies To: SQL: How do I write to a database?

#2 eclipsed4utoo   User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1535
  • View blog
  • Posts: 5,972
  • Joined: 21-March 08

Re: SQL: How do I write to a database?

Posted 03 August 2011 - 04:11 AM

Have you checked out our Tutorial section? Specifically, this tutorial on the basics of SQL.

You don't use a DataWriter to write to the database. You do an INSERT/UPDATE statement, pass parameters, then call the ExecuteNonQuery method for the SqlCommand.

If you take a look at this tutorial, I do an INSERT statement toward the end of the tutorial. That might help you out.
Was This Post Helpful? 2
  • +
  • -

Page 1 of 1