0% found this document useful (0 votes)
62 views2 pages

Creare Baza de Date Din Cod:: Public Static Void New String New

The document contains code to create a database and tables in SQL Server, insert and delete data from the tables, and define a Stack class in C#. The code first creates a Romania database and adds Judete and Localitati tables with a foreign key relationship. It then inserts sample data into the Judete table and defines methods to delete data by ID. Finally, it defines a Stack class with methods to push and pop string items onto a fixed-size stack structure.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
62 views2 pages

Creare Baza de Date Din Cod:: Public Static Void New String New

The document contains code to create a database and tables in SQL Server, insert and delete data from the tables, and define a Stack class in C#. The code first creates a Romania database and adds Judete and Localitati tables with a foreign key relationship. It then inserts sample data into the Judete table and defines methods to delete data by ID. Finally, it defines a Stack class with methods to push and pop string items onto a fixed-size stack structure.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

CREARE BAZA DE DATE DIN COD:

public static void creareDB()


{
string ds = @"Data Source=ADMIN\SQLEXPRESS";
string db = "Initial Catalog=";
string ins = "Integrated Security=True";
string str;
SqlConnection myCon = new SqlConnection(@ds + ";" + db + ";" + ins);

str="CREATE DATABASE Romania";


SqlCommand cmd = new SqlCommand();
cmd.Connection = myCon;
cmd.CommandText = str;
try
{
myCon.Open();
cmd.ExecuteNonQuery();
myCon.Close();
}
catch (Exception ex)
{
Console.WriteLine(ex);
Console.ReadKey();
}
}
+ ADAUGARE TABELE
public static void modificareDb()
{
SqlConnection myCon = new SqlConnection(@"Data Source=ADMIN\SQLEXPRESS;Initial Catalog=Romania;Integrated Security=True");
string str,str2;
str = "CREATE TABLE [dbo].[Judete]([id] [int] PRIMARY KEY, [Denumire] [text] NOT NULL, [Suprafata] [int] NOT NULL )";
SqlCommand cmd = new SqlCommand();
cmd.Connection = myCon;
cmd.CommandText = str;
try
{
myCon.Open();
cmd.ExecuteNonQuery();
myCon.Close();
}
catch (Exception ex)
{
Console.WriteLine("Eroare la Judete" + ex);
Console.ReadKey();
}
str2="CREATE TABLE [dbo].[Localitati]([id] [int] PRIMARY KEY, [Denumire] [text] NOT NULL, [Judete_FK] [int] NOT NULL)";
try
{
cmd.CommandText = str2;
myCon.Open();
cmd.ExecuteNonQuery();
myCon.Close();
Console.WriteLine("SUCCESS!!!");
}
catch (Exception ex)
{
Console.WriteLine("Eroare la Localitati" + ex);
Console.ReadKey();
}

try
{
myCon.Open();
cmd.CommandText = "ALTER TABLE [dbo].[Localitati] ADD CONSTRAINT [FK_Judete_Localitati] FOREIGN KEY([Judete_FK]) REFERENCES [dbo].
[Judete]([id]) ON DELETE CASCADE";
cmd.ExecuteNonQuery();
Console.WriteLine("Realatie creata!");
myCon.Close();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}

INSTERT
cmd.CommandText = "INSERT INTO Judete(id,Denumire,Suprafata) VALUES('" + jud[i].id + "','" + jud[i].denumire + "','" + jud[i].suprafata + "')";
cmd.ExecuteNonQuery();

DELETE
static void DeleteJud()
{
Console.WriteLine("Dati id-ul judetului: ");
int idjud = int.Parse(Console.ReadLine());
SqlConnection con2 = new SqlConnection(@"Data Source=ADMIN\SQLEXPRESS;Initial Catalog=Romania; Integrated Security = True");
SqlCommand cmd = new SqlCommand();
cmd.Connection = con2;
con2.Open();
string judname = "";
SqlDataAdapter da = new SqlDataAdapter("SELECT id,Denumire FROM Judete ", con2);
DataSet ds = new DataSet();

da.Fill(ds, "Jud");
bool match = false;
foreach (DataRow row in ds.Tables["Jud"].Rows)

if (idjud == (int)row["id"])
{ match = true; judname = row["Denumire"].ToString(); }

if (match == true)
{
cmd.CommandText = "Delete FROM Judete WHERE id=" + idjud + "";
cmd.ExecuteNonQuery();
Console.WriteLine("Judetul " + judname + " a fost sters");
}

else
Console.WriteLine("Judetul nu a fost gasit!");
con2.Close();
}

STIVA :
public class Stackp
{
public const int MaxSize = 10;
public string[] items = new string[MaxSize];
public int currentIndex = -1;

public Stackp()
{}

public bool IsEmpty()


{
if (currentIndex < 0)
return true;
else
return false;

public void Push(string item)


{
if (currentIndex >= MaxSize - 1)
throw new InvalidOperationException("Stack is full!");

currentIndex++;
items[currentIndex] = item;
}

public string Pop()


{
if (IsEmpty())
throw new InvalidOperationException("Stack is empty!");

string item = items[currentIndex];


items[currentIndex] = null;
currentIndex--;

return item;

You might also like