Go Back  Xtreme .NET Talk > .NET > Syntax Specific > Visual C# .NET > Problem connecting to the SQL database


Reply
 
Thread Tools Display Modes
  #1  
Old 05-29-2012, 09:06 AM
Ales Zigon Ales Zigon is offline
Newcomer
 
Join Date: Jul 2003
Posts: 4
Ales Zigon is on a distinguished road
Angry Problem connecting to the SQL database



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?
Reply With Quote
  #2  
Old 05-30-2012, 09:07 AM
dotnetguy37 dotnetguy37 is offline
Freshman
 
Join Date: Nov 2009
Posts: 29
dotnetguy37 is on a distinguished road
Default Slovenian commentary

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

Google translate recognizes,
Quote:
Originally Posted by Ales Zigon
Class za shranjevanje in pisanje podatkov v bazo
..as Slovenian, and it translates to:
Quote:
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 by dotnetguy37; 05-30-2012 at 09:42 AM.
Reply With Quote
  #3  
Old 05-30-2012, 09:41 AM
Ales Zigon Ales Zigon is offline
Newcomer
 
Join Date: Jul 2003
Posts: 4
Ales Zigon is on a distinguished road
Default 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...
Reply With Quote
  #4  
Old 05-30-2012, 09:44 AM
dotnetguy37 dotnetguy37 is offline
Freshman
 
Join Date: Nov 2009
Posts: 29
dotnetguy37 is on a distinguished road
Default

Quote:
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.
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Connecting to visual Web Dev Database aderes Database / XML / Reporting 1 12-28-2005 06:11 AM
Connecting to the database and best practices. mike55 Database / XML / Reporting 7 11-17-2005 05:55 PM
Problem connecting to database samsmithnz Database / XML / Reporting 4 01-13-2004 06:21 PM
Connecting to an Access Database BlueJay924 Database / XML / Reporting 3 07-18-2003 04:36 AM
Asp.net and connecting my database andycharger Database / XML / Reporting 4 07-02-2003 02:27 AM

Advertisement:







Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe