ARTICLE AD BOX
I’ve recently started working with more complex code to improve my programming skills. As my projects grow, I’m finding it increasingly difficult to keep my code well formatted and readable and it often ends up looking quite messy. Is there a way to have Visual Studio automatically format code? I know that Visual Studio Code can do this for HTML, so I’m wondering if something similar exists here. Also, do you have any personal tips or best practices for keeping code clean and well-structured? What is the best practice ragrding this? For example, the code below took me an unreasonable amount of time to format, and I’m still not fully satisfied about how it looks.
Thanks in advance for any advice.
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Data.SQLite; using System.Windows.Forms; using System.IO; using System.ComponentModel; using System.Runtime.Remoting.Messaging; namespace library { internal class SQLClass { public static string Cs { get; private set; } public static SQLiteConnection Connection { get; private set; } private SQLiteCommand command; public SQLClass() { Cs = @"URI=file:../../../database.db"; Connection = new SQLiteConnection(Cs); } public static void Connect() { try { Connection.Open(); } catch (Exception ex) { MessageBox.Show(ex.Message); } } public static void Disconnect() { try { Connection.Close(); } catch (Exception ex) { MessageBox.Show(ex.Message); } } public static void BookCustomerEdit(int BookId, int MemberId) { try { Connect(); SQLiteCommand command = new SQLiteCommand(Connection); command.CommandText = "UPDATE books SET MemberId = " + MemberId + " WHERE BookId = " + BookId + ";"; command.ExecuteNonQuery(); Disconnect(); } catch (Exception ex) { MessageBox.Show(ex.Message); } } public static void NewBook(string Title, int GenreId, int AuthorId) { try { Connect(); SQLiteCommand command = new SQLiteCommand(Connection); command.CommandText = "INSERT INTO books(Title, AuthorId, GenreId, MemberId) VALUES(@Title, @AuthorId, @GenreId, -1);"; command.Parameters.AddWithValue("@AuthorId", AuthorId); command.Parameters.AddWithValue("@Title", Title); command.Parameters.AddWithValue("@GenreId", GenreId); command.ExecuteNonQuery(); Disconnect(); } catch (Exception ex) { MessageBox.Show(ex.Message); } } public static void NewAuthor(string FirstName, string LastName) { try { Connect(); SQLiteCommand command = new SQLiteCommand(Connection); command.CommandText = "INSERT INTO authors(FirstName, LastName) VALUES(@FirstName, @LastName);"; command.Parameters.AddWithValue("@FirstName", FirstName); command.Parameters.AddWithValue("@LastName", LastName); command.ExecuteNonQuery(); Disconnect(); } catch (Exception ex) { MessageBox.Show(ex.Message); } } public static void NewCustomer(string FirstName, string LastName) { try { Connect(); SQLiteCommand command = new SQLiteCommand(Connection); command.CommandText = "INSERT INTO members(FirstName, LastName) VALUES(@FirstName, @LastName);"; command.Parameters.AddWithValue("@FirstName", FirstName); command.Parameters.AddWithValue("@LastName", LastName); command.ExecuteNonQuery(); Disconnect(); } catch (Exception ex) { MessageBox.Show(ex.Message); } } public static string[] FindCustomer(string FirstName, string LastName, string MemberId) { try { SQLiteCommand command = new SQLiteCommand(Connection); string cmd = ""; if (FirstName != "") cmd += " FirstName='" + FirstName + "'"; if (LastName != "") { if (cmd.Length > 3) cmd += " AND "; cmd += " LastName='" + LastName + "'"; } if (MemberId != "") { if (cmd.Length > 3) cmd += " AND "; cmd += " MemberId='" + MemberId + "'"; } Connect(); string[] result = new string[3]; command.CommandText = "SELECT * FROM members WHERE" + cmd + ";"; using (var reader = command.ExecuteReader()) while (reader.Read()) { result[0] = (string)reader["FirstName"]; result[1] = (string)reader["LastName"]; result[2] = Convert.ToInt64(reader["MemberId"]).ToString(); } Disconnect(); return result; } catch (Exception ex) { MessageBox.Show(ex.Message); return null; } } public static int FindAuthorByName(string Name) { try { Connect(); SQLiteCommand command = new SQLiteCommand(Connection); int a = -1; if (Name != "") { command.CommandText = "SELECT * FROM authors WHERE FirstName = '" + Name + "' OR LastName = '" + Name + "';"; using (var reader = command.ExecuteReader()) while (reader.Read()) a = Convert.ToInt32(reader["AuthorId"]); } Disconnect(); return a; } catch (Exception ex) { return -1; } } public static int FindGenreByName(string Name) { try { Connect(); SQLiteCommand command = new SQLiteCommand(Connection); int a = -1; command.CommandText = "SELECT * FROM genres WHERE Name = '" + Name.ToLower() + "';"; using (var reader = command.ExecuteReader()) while (reader.Read()) a = Convert.ToInt32(reader["GenreId"]); Disconnect(); return a; } catch (Exception ex) { return -1; } } public static BindingList<Book> FindBook(int BookId, string Title, int AuthorId, int GenreId, int MemberId) { BindingList<Book> list = new BindingList<Book>(); SQLiteCommand command = new SQLiteCommand(Connection); Connect(); try { command.CommandText = "SELECT * FROM books WHERE BookId= @BookId OR Title= @Title OR AuthorId= @AuthorId OR GenreId= @GenreId OR MemberId= @MemberId;"; command.Parameters.AddWithValue("@BookId", BookId); command.Parameters.AddWithValue("@Title", Title); command.Parameters.AddWithValue("@AuthorId", AuthorId); command.Parameters.AddWithValue("@GenreId", GenreId); command.Parameters.AddWithValue("@MemberId", MemberId); if (BookId == -1 && Title == "" && GenreId == -1 && AuthorId == -1 && MemberId == -1) command.CommandText = "SELECT * FROM books;"; using (var reader = command.ExecuteReader()) while (reader.Read()) list.Add(new Book( Convert.ToInt32(reader["BookId"]), (string)reader["Title"], Convert.ToInt32(reader["AuthorId"]), Convert.ToInt32(reader["GenreId"]), Convert.ToInt32(reader["MemberId"]) )); Disconnect(); } catch (Exception ex) { MessageBox.Show(ex.Message); } return list; } public static BindingList<Customer> ListCustomer() { Connect(); BindingList<Customer> list = new BindingList<Customer>(); SQLiteCommand command = new SQLiteCommand(Connection); command.CommandText = "SELECT * FROM members"; using (var reader = command.ExecuteReader()) while (reader.Read()) list.Add(new Customer( Convert.ToInt32(reader["MemberId"]), (string)reader["FirstName"], (string)reader["LastName"] )); Disconnect(); return list; } public static BindingList<Author> ListAuthor() { Connect(); BindingList<Author> list = new BindingList<Author>(); SQLiteCommand command = new SQLiteCommand(Connection); command.CommandText = "SELECT * FROM authors"; using (var reader = command.ExecuteReader()) while (reader.Read()) list.Add(new Author( Convert.ToInt32(reader["AuthorId"]), (string)reader["FirstName"], (string)reader["LastName"] )); Disconnect(); return list; } public static BindingList<Genre> ListGenre() { Connect(); BindingList<Genre> list = new BindingList<Genre>(); SQLiteCommand command = new SQLiteCommand(Connection); command.CommandText = "SELECT * FROM genres"; using (var reader = command.ExecuteReader()) while (reader.Read()) list.Add(new Genre( Convert.ToInt32(reader["GenreId"]), (string)reader["Name"] )); Disconnect(); return list; } } }