Problem connecting to the SQL database

Ales Zigon

Newcomer
Joined
Jul 6, 2003
Messages
4
Hi!

I'm playing with SQL database (or at least trying to) and C#, but I'm getting this problem:
I've created a database and a single table in it with VS. The problem is, that for the life of me, I cannot open it (or connect to it). The database is shown in my solution explorer (on the right-hand side if the IDE), the connection string is the one from the properties window, but every time, I run the code, there come an error saying that the server is not accesible...

Well, here's my code:
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Diagnostics;

namespace Tarok_turnir
	{
	// Class za shranjevanje in pisanje podatkov v bazo
	class cPodatki
		{

        #region fields

        private string myConnectionString;
        private SqlConnection myConnection;

        #endregion fields

		#region constructor
		public cPodatki()
			{
			
			myConnectionString= @"Data Source=|DataDirectory|\Tarok.sdf";
			myConnection = new SqlConnection(myConnectionString);
			}
		#endregion constructor

        #region Branje igralcev

        public List<string> PreberiIgralce()
            {
            //  prebere seznam igralcev
            //  in vrne list<> igralcev
            try
                {
                SqlDataReader myReader = null;
                SqlCommand myComand = new SqlCommand("SELECT * FROM seznam_igralcev", myConnection);
                myConnection.Open();
                myReader = myComand.ExecuteReader();
                List<string> igralci = new List<string>();
                while (myReader.Read())
                    {
                    igralci.Add(myReader["ImeIgralca"].ToString() + " " + myReader["PriimekIgralca"].ToString());
                    }
                return igralci;
                }
            catch (System.Exception ex)
                {
                throw ex;
                }
            finally
                {
                myConnection.Close();
                }

            }

        #endregion Branje igralcev


        #region Vpis igralcev

        public void VpisiIgralca(string ime, string priimek, string naslov, int id_kluba)
            {
            //  vpiše novega igralca v bazo
            string SQLstring = "INSERT INTO Seznam_igralcev (ImeIgralca, PriimekIgralca, NaslovIgralca, IDkluba) VALUES (@ime, @priimek, @naslov, @id_kluba)";
            SqlCommand myCommand = new SqlCommand(SQLstring, myConnection);
            myCommand.Parameters.AddWithValue("@ImeIgralca", ime);
            myCommand.Parameters.AddWithValue("@PriimekIgralca", priimek);
            myCommand.Parameters.AddWithValue("@NaslovIgralca", naslov);
            myCommand.Parameters.AddWithValue("@IDKluba", id_kluba);
            myConnection.Open();

            try
                {
                myCommand.ExecuteNonQuery();
                }
            catch (System.Exception ex)
                {
                throw ex;
                }
            finally
                {
                myConnection.Close();
                }
            }

        #endregion Vpis igralcev
		}
	}

And here's the testing part...
Code:
        private void button1_Click(object sender, EventArgs e)
            {
            cPodatki podatki=new cPodatki();
            foreach (string igralec in podatki.PreberiIgralce())
            {
            listBox1.Items.Add(podatki.PreberiIgralce());
            }
            }

Does anybody have an idea what am I doing wrong?
 
Slovenian commentary

No idea on the SQL question, but for other forum members info..

Google translate recognizes,
Ales Zigon said:
Class za shranjevanje in pisanje podatkov v bazo
..as Slovenian, and it translates to:
Class for storing and writing data to the database
..however the namespace name "Tarok_turnir" doesn't translate from Slovenian
or any other language that Google translate recognizes,
but I know there is a Slovenian card game called "Tarok",
and so maybe "turnir" means "turn".

From the translation of the Slovenian comment above,
"podatki" would roughly translate as "data" or information,
so "class cPodatki" would be "class cData"

"PreberiIgralce" doesn't translate via Google translate, but there is a film called
"Preberi in zažgi", whose American title is "Burn after reading",
so I'm guessing "Preberi" probably means "read"
(google translates "read" from Slovenian-to-English as "preberite" - its close..)

"Izgubljamo igralce" means "losing player" so I'm guessing "Igralce" means player,
which makes "PreberiIgralce" something like "readPlayer" (possibly).
 
Last edited:
Re: Slovenian commentary

On the first part, you're right on.
And for the secon part, it would translate to: "class cData". Tarok_turnir would translate to: "Tarok_tournament". FYI: "Tarok" is a card game...
 
And for the secon part, it would translate to: "class cData". Tarok_turnir would translate to: "Tarok_tournament". FYI: "Tarok" is a card game...
Hey..I think I can almost understand Slovenian now (:)).
Thanks for the the translation validation.
 
Back
Top