FrankBerrocal / BigSnowMan

DevOps project to develop PM tools
1 stars 1 forks source link

Test DML statements in C# #23

Closed FrankBerrocal closed 1 year ago

FrankBerrocal commented 1 year ago

With LA3, connections to a Database were set.
DML was non-responsive.
Based on this example:

  1. Create a database.
  2. Create a test table.
  3. Insert dummy data.
  4. Provide diagnosis.
FrankBerrocal commented 1 year ago

I will create my first code in a ConsoleAPP. I will not waste time trying to go to Docker, I have ASP options and I cannot wast time trying to implement something not developed yet.

FrankBerrocal commented 1 year ago

Idea, test with Google Datastore. Now, test locally.

FrankBerrocal commented 1 year ago

Connection created.

// Build connection string SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder(); builder.DataSource = "localhost,1440"; // Port should be indicated. In a container, the same. builder.UserID = "sa";
builder.Password = "myPassw0rd";
builder.InitialCatalog = "master";

FrankBerrocal commented 1 year ago

// Create a sample database Console.Write("Dropping and creating database 'SampleDB' ... "); String sql = "DROP DATABASE IF EXISTS [SampleDB]; CREATE DATABASE [SampleDB]"; using (SqlCommand command = new SqlCommand(sql, connection)) { command.ExecuteNonQuery(); Console.WriteLine("Done."); }

FrankBerrocal commented 1 year ago

StringBuilder sb = new StringBuilder(); sb.Append("USE SampleDB; "); sb.Append("CREATE TABLE Employees ( "); sb.Append(" Id INT , "); sb.Append(" Name NVARCHAR(50), "); sb.Append(" Location NVARCHAR(50) "); sb.Append("); ");

NOT WORKING I am not able to create the table. WHY? this is an example from the internet. This is the same element I tried to create during last session.

FrankBerrocal commented 1 year ago

Changed the name of the database, however keeps creating SampleDB. The file running is not the one I am working with

FrankBerrocal commented 1 year ago

That last step made me check the code. I analyzed the elements required by the program:

  1. SQL string
  2. SQL command
  3. String Builder (SQL string.toString(), SQL command { executeNonQuery() or executeQuery() }

Table has been created.

FrankBerrocal commented 1 year ago

I have checked the code, and created an abstraction. This can lead to an object during implementation.

                String sql = "";
                StringBuilder sb = new StringBuilder();
                sb.Append("DROP DATABASE IF EXISTS [Ulysses];");
                sb.Append("CREATE DATABASE [Ulysses];");
                sql = sb.ToString();
                using (SqlCommand command = new SqlCommand(sql, connection))
                {
                    command.ExecuteNonQuery();
                    Console.WriteLine("Done.");
                }
                sb.clear();   //to work with the next command
FrankBerrocal commented 1 year ago

Image

FrankBerrocal commented 1 year ago

Image

FrankBerrocal commented 1 year ago

I have been able to implement and standardize all the examples on the reference page https://www.microsoft.com/en-us/sql-server/developer-get-started/csharp/macos/step/2.html

This means all the functionality is available, reflected in SQL, and also, generalization has been conceptualized.

One important element is that the UML conceptualization is developed from the point of view of the user, but since I am working with multiple layers, each one involves a different diagram.

For example, I have the Class diagram for the entities, which is backed up by an ER diagram. And also in C#, another diagram should represent the objects handling all SQL objects, displays, and queries.