Недавно я начал работать с более сложным кодом, чтобы улучшить свои навыки программирования. По мере роста моих проектов мне становится все труднее сохранять мой код хорошо отформатированным и читабельным, и зачастую он выглядит довольно беспорядочным. Есть ли способ автоматически форматировать код Visual Studio? Я знаю, что Visual Studio Code может сделать это для HTML, поэтому мне интересно, существует ли здесь что-то подобное. Кроме того, есть ли у вас какие-либо личные советы или рекомендации по поддержанию чистоты и хорошей структуры кода? Какова наилучшая практика в этом отношении? Например, форматирование приведенного ниже кода заняло у меня неоправданно много времени, и я до сих пор не полностью удовлетворен тем, как он выглядит.
Заранее благодарен за любые советы.
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 FindBook(int BookId, string Title, int AuthorId, int GenreId, int MemberId)
{
BindingList list = new BindingList();
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 ListCustomer()
{
Connect();
BindingList list = new BindingList();
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 ListAuthor()
{
Connect();
BindingList list = new BindingList();
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 ListGenre()
{
Connect();
BindingList list = new BindingList();
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;
}
}
}