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:
And here's the testing part...
Does anybody have an idea what am I doing wrong?
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)
{
// vpie 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?