Citar
private void frmMain_Load(object sender, EventArgs e)
{
try
{
SqlConnection Con = new SqlConnection("Data Source=.;Initial Catalog=SayHello;Integrated Security=True");
Con.Open();
string cmd = "SELECT Message FROM ES";
SqlCommand comand = new SqlCommand(cmd, Con);
SqlDataReader readComm = comand.ExecuteReader();
if (readComm.Read())
{
MessageBox.Show("DB Load");
}
Con.Close();
}
catch
{
DataLayer.CreateDBinSQLserver DB = new DataLayer.CreateDBinSQLserver();
DB.createDB();
DB.createTablePS();
DB.UtilizeFunction();
MessageBox.Show("DB creted");
}
}
{
try
{
SqlConnection Con = new SqlConnection("Data Source=.;Initial Catalog=SayHello;Integrated Security=True");
Con.Open();
string cmd = "SELECT Message FROM ES";
SqlCommand comand = new SqlCommand(cmd, Con);
SqlDataReader readComm = comand.ExecuteReader();
if (readComm.Read())
{
MessageBox.Show("DB Load");
}
Con.Close();
}
catch
{
DataLayer.CreateDBinSQLserver DB = new DataLayer.CreateDBinSQLserver();
DB.createDB();
DB.createTablePS();
DB.UtilizeFunction();
MessageBox.Show("DB creted");
}
}
Citar
namespace DataLayer
{
public class CreateDBinSQLserver
{
public void createDB()
{
SqlConnection Con = new SqlConnection("Data Source=.;Initial Catalog=master;Integrated Security=True");
Con.Open();
string cmd = "USE master IF DB_ID('SayHello') IS NOT NULL BEGIN DROP DATABASE SayHello END";
SqlCommand command = new SqlCommand(cmd, Con);
command.ExecuteNonQuery();
string cmd2 = "CREATE DATABASE SayHello";
SqlCommand command2 = new SqlCommand(cmd2, Con);
command2.ExecuteNonQuery();
Con.Close();
}
public void createTablePS()
{
SqlConnection Con2 = new SqlConnection("Data Source=.;Initial Catalog=SayHello;Integrated Security=True");
Con2.Open();
string cmdTables = "CREATE TABLE ENG(ID INT UNIQUE IDENTITY(1,1) NOT NULL, Message varchar(50) UNIQUE NOT NULL, PRIMARY KEY(ID)) CREATE TABLE ES(ID INT UNIQUE IDENTITY(1,1) NOT NULL, Message varchar(50) UNIQUE NOT NULL, PRIMARY KEY(ID), FOREIGN KEY(Message) REFERENCES ENG(Message)) CREATE TABLE JAP(ID INT UNIQUE IDENTITY(1,1) NOT NULL, Message varchar(50) UNIQUE NOT NULL, PRIMARY KEY(ID)) CREATE INDEX IN1 ON ENG (ID) CREATE INDEX IN2 ON ES (ID) CREATE INDEX IN3 ON JAP (ID)";
SqlCommand command2 = new SqlCommand(cmdTables, Con2);
command2.ExecuteNonQuery();
string cmdINSERT = "INSERT INTO ENG (Message) values ('Hello world.'), ('New hello world.') INSERT INTO ES (Message) values ('Hello world.'), ('New hello world.') INSERT INTO JAP (Message) values ('Konnichiwa sekai'), ('Sekai haro futatabi')";
SqlCommand commINS = new SqlCommand(cmdINSERT, Con2);
commINS.ExecuteNonQuery();
string cmdPSins = "CREATE procedure Ins @message varchar(50) as INSERT into JAP (Message) values (@message)";
SqlCommand command3 = new SqlCommand(cmdPSins, Con2);
command3.ExecuteNonQuery();
string cmdINN = "CREATE procedure msg @id int as SELECT E.Message, es.Message FROM ENG E inner join ES es on E.id = es.id";
SqlCommand command4 = new SqlCommand(cmdINN, Con2);
command4.ExecuteNonQuery();
string FUNCTION = "CREATE function ConvertMinAMayus(@Message varchar(50)) returns varchar(50) as begin return UPPER(@Message) END";
SqlCommand command5 = new SqlCommand(FUNCTION, Con2);
command5.ExecuteNonQuery();
Con2.Close();
}
public void UtilizeFunction()
{
SqlConnection Con2 = new SqlConnection("Data Source=.;Initial Catalog=SayHello;Integrated Security=True");
Con2.Open();
string PsFunc = "CREATE procedure UPandAllMayus @Message varchar(50), @id int as UPDATE JAP set Message=dbo.ConvertMinAMayus(@Message) where id=@id";
SqlCommand command6 = new SqlCommand(PsFunc, Con2);
command6.ExecuteNonQuery();
Con2.Close();
}
}
}
{
public class CreateDBinSQLserver
{
public void createDB()
{
SqlConnection Con = new SqlConnection("Data Source=.;Initial Catalog=master;Integrated Security=True");
Con.Open();
string cmd = "USE master IF DB_ID('SayHello') IS NOT NULL BEGIN DROP DATABASE SayHello END";
SqlCommand command = new SqlCommand(cmd, Con);
command.ExecuteNonQuery();
string cmd2 = "CREATE DATABASE SayHello";
SqlCommand command2 = new SqlCommand(cmd2, Con);
command2.ExecuteNonQuery();
Con.Close();
}
public void createTablePS()
{
SqlConnection Con2 = new SqlConnection("Data Source=.;Initial Catalog=SayHello;Integrated Security=True");
Con2.Open();
string cmdTables = "CREATE TABLE ENG(ID INT UNIQUE IDENTITY(1,1) NOT NULL, Message varchar(50) UNIQUE NOT NULL, PRIMARY KEY(ID)) CREATE TABLE ES(ID INT UNIQUE IDENTITY(1,1) NOT NULL, Message varchar(50) UNIQUE NOT NULL, PRIMARY KEY(ID), FOREIGN KEY(Message) REFERENCES ENG(Message)) CREATE TABLE JAP(ID INT UNIQUE IDENTITY(1,1) NOT NULL, Message varchar(50) UNIQUE NOT NULL, PRIMARY KEY(ID)) CREATE INDEX IN1 ON ENG (ID) CREATE INDEX IN2 ON ES (ID) CREATE INDEX IN3 ON JAP (ID)";
SqlCommand command2 = new SqlCommand(cmdTables, Con2);
command2.ExecuteNonQuery();
string cmdINSERT = "INSERT INTO ENG (Message) values ('Hello world.'), ('New hello world.') INSERT INTO ES (Message) values ('Hello world.'), ('New hello world.') INSERT INTO JAP (Message) values ('Konnichiwa sekai'), ('Sekai haro futatabi')";
SqlCommand commINS = new SqlCommand(cmdINSERT, Con2);
commINS.ExecuteNonQuery();
string cmdPSins = "CREATE procedure Ins @message varchar(50) as INSERT into JAP (Message) values (@message)";
SqlCommand command3 = new SqlCommand(cmdPSins, Con2);
command3.ExecuteNonQuery();
string cmdINN = "CREATE procedure msg @id int as SELECT E.Message, es.Message FROM ENG E inner join ES es on E.id = es.id";
SqlCommand command4 = new SqlCommand(cmdINN, Con2);
command4.ExecuteNonQuery();
string FUNCTION = "CREATE function ConvertMinAMayus(@Message varchar(50)) returns varchar(50) as begin return UPPER(@Message) END";
SqlCommand command5 = new SqlCommand(FUNCTION, Con2);
command5.ExecuteNonQuery();
Con2.Close();
}
public void UtilizeFunction()
{
SqlConnection Con2 = new SqlConnection("Data Source=.;Initial Catalog=SayHello;Integrated Security=True");
Con2.Open();
string PsFunc = "CREATE procedure UPandAllMayus @Message varchar(50), @id int as UPDATE JAP set Message=dbo.ConvertMinAMayus(@Message) where id=@id";
SqlCommand command6 = new SqlCommand(PsFunc, Con2);
command6.ExecuteNonQuery();
Con2.Close();
}
}
}
Les agradezco cualquier tipo de ayuda, no se si debe estar mal hacer esto a travez de pocos metodos o si esta mal hacerlo de esta forma, desde ya gracias.