I am relatively new to C #, but with a lot of help from Google search and Stack Overflow. I have already made a number of applications that include working with Office, system services, processes, WMI, SQL, Linq, and Active Directory ...
Although, despite the successes in using these applications, I'm still not sure about many things in C #, such as good code practice and the use of many keywords, etc.
C # classes; I know what I can do with them, I know about constructors and destructors, creation and properties, but I'm not sure when I will use them. So far, I have written all my code in the Form1.cs file inside different methods. These Methods do a number of different things with completely different APIs. This obviously means that trying to save this code can be quite a challenge, and it is becoming increasingly difficult for me to find anything inside my Form1.cs.
My question is for you guys, should I break my code into different classes? I tried to separate the material related to SqlConnection and SqlCommands into a separate class, but without instantiating the same class several times in my Form1.cs. I donโt see it being easier or more useful.
I'm trying to put a new application together, but this time keeping the functionality in my class, I was hoping that someone could tell me that I'm stupid, doing it wrong, or at least giving me some tips.
This application will eventually download my connection string from App.Config, connect to the SQL database, and populate the DataSet with several tables from the database. This is by no means functional, as I cannot solve this problem.
Many thanks:)
partial class Form1 : Form { public Form1() { InitializeComponent(); } string myConnectionString; private void Form1_Load(object sender, System.EventArgs e) { AppConfig cfg = new AppConfig(); if (cfg.loadConfig()) { myConnectionString = cfg.myConnectionString(); } if (!String.IsNullOrEmpty(myConnectionString)) { SQLConn SQL = new SQLConn(); if (SQL.createConnection(myConnectionString)) { MessageBox.Show("Connected!"); } } } } class myDataSet { DataSet DataSet() { DataSet ds = new DataSet(); SQLConn sql = new SQLConn(); return ds; } public void fillData() { try { SqlCommand sqlCmd = new SqlCommand("SELECT * FROM hardware"); } catch (Exception ex) { MessageBox.Show(ex.Message); } } } class SQLConn : IDisposable { SqlConnection sqlConn; public bool createConnection(string myConnectionString) { sqlConn = new SqlConnection(); sqlConn.ConnectionString = myConnectionString; try { sqlConn.Open(); return true; } catch (Exception ex) { MessageBox.Show(ex.Message); } return false; } public void Dispose() { if (sqlConn.State == ConnectionState.Open) { sqlConn.Close(); sqlConn.Dispose(); } } } class AppConfig { Configuration cfg; public bool loadConfig() { try { cfg = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); if (!File.Exists(cfg.FilePath)) { MessageBox.Show("No configuration file"); } return true; } catch(Exception ex) { MessageBox.Show(ex.Message); } return false; } public string myConnectionString() { string connectionString = ConfigurationManager.ConnectionStrings["IT_ProjectConnectionString"].ConnectionString; return connectionString; } }