0

EDIT: Thank you everyone, I figured out how to get it to work now! Details below...

I'm kind of a newbie to C#, and I'm trying to teach myself the language by programming a really simple RPG game.

Right now, I'm at the point where I want to start adding different enemies to fight (up until now I just used a single one hardcoded in for testing).

I've started setting up a database with enemy info (one column for name, one for HP, one for common stats and attacks, etc.). I have it so that when you start combat with an enemy, the player is able to select a creature from a dropdown, and whichever creature he has will set a variable called "EnemyID".

What I want to do is use that EnemyID variable to correspond to a row in my database, then pull the value of each column into variables that I can then reference during combat.

Is this something that's possible to do? If so, could someone explain the method to me in relatively simple terms? Even just a small example of how to import row data from any kind of database will do, I'm good at understanding code once I see it in use a couple of times.

(Oh yeah, if it matters, I'm using Visual Studio Express 2013, and my database is a SQL Server Express 2014 database.)

Thanks in advance!

EDIT:

After finding a simple tutorial for ADO.NET, and following a suggestion from one of the posters, I've come up with the following code.

 public void DataPoll()
    {
        SqlConnection MonDat = new SqlConnection("Data Source=(local);
              Initial Catalog=TestDatabase;Integrated Security=SSPI");
        SqlDataReader rdr = null;
        try
        {
            MonDat.Open();
            SqlCommand cmd = new SqlCommand(
                "select * from Monsters where Id = EnemyID", MonDat);
            rdr = cmd.ExecuteReader();
            while (rdr.Read())
            {
                EnemyIDBuffer = (int)rdr["Id"];
                EnemyName = (string)rdr["Name"];
                EnemyHPBase = (int)rdr["HP"];
                EnemyAtkBase = (int)rdr["Atk"];
                EnemyDefBase = (int)rdr["Def"];
                EnemyMagBase = (int)rdr["Mag"];
                PrimAtk = (string)rdr["PrimAtk"];
                SecoAtk = (string)rdr["SecoAtk"];
                TertAtk = (string)rdr["TertAtk"];
                RareAtk = (string)rdr["RareAtk"];
            }
        }
        finally
        {
            if (rdr != null)
            {
                rdr.Close();
            }
            if (MonDat != null)
            {
                MonDat.Close();
            }
        }
    }

However, when I try to run it, my program stalls and crashes. I'm guessing I have something configured wrong (I just took script from the tutorial and tweaked it slightly). Can anyone give me a hand figuring out where I went wrong?

EnemyID is a variable I used to assign what enemy is fought, based on a menu selection. I'm trying to use that variable to generate the row ID to pull the rest of the row data from, but I think that might be causing an issue.

EDIT2: It took me longer than it really should have, but I figured it out. I had to change my code a little tiny bit.

                SqlCommand cmd = new SqlCommand(
                "select * from Monsters where Id = " + EnemyID, MonDat);

I have a habit of forgetting that you're able to join statements like this. I made a new project that only polled data and threw it into my variables, and from there put it into text boxes, and with this method I was able to poll two different sets of enemy stats by assigning different EnemyID values to two different buttons. Proof of concept, right there.

Thanks to both people who replied, both suggestions were equally useful to getting this working. :)

2
  • 4
    why not show us what you have done so far?? and tbh a simple RPG game is a horrible starter project Commented Oct 16, 2014 at 20:43
  • There's not really any reason for me to show what I have -- I won't get any benefit, and there's nothing anyone else would take interest in. As it for being a "horrible starter project", why? Building a competent RPG system will use so many different functions, I think it's the perfect way to learn a programming language. That is, as long as you don't half-ass it, which I'm not. Commented Oct 16, 2014 at 23:41

2 Answers 2

4

There's numerous tutorials out there on how to use a database, the first two use straight ADO.NET which is pure data access, making you responsible for its interaction in your code:

The next two, one is for Entity, and the other for nHibernate, they connect to SQL databases and convert the objects there to usable code in your program through a process called object relational mapping.

These are all relevant links to stuff in the most current years, with VS 2013; hopefully that provides you a good starting point.

Sign up to request clarification or add additional context in comments.

Comments

0

You can do something like this:

Your SQL should pass in the procedure name and EnemyId.

The stored procedure would do a select * from Enemies where EnemyId = @EnemyId

DataSet dataSet = HSPDataAccessProxy.Instance.ExecuteDataSet(sql);

The dataSet has the table that is returned by the store procedure and you can retrieve the columns you need from that table.

3 Comments

That might work just fine, but as I said, I'm pretty much a complete newbie to C#, so what you're saying there is still a bit above my head.
Ah, ok, I read a really good basic tutorial on ADO.NET after seeing Brian's reply above. I see that the select statement you mentioned needs to be part of a SqlCommand. My only question, is which part is which? Let's say my variable is "EnemyID", and my column in my database is just "ID". Would the statement read "where EnemyID = ID", or would it be "where ID = EnemyID". I think it's the latter, but I'm not super sure. Thank you for the suggestion!
Its does not matter what side of the where clause your variable is. EnemyId = Id is same as Id = EnemyId

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.