Пример #1
0
 public void UpdateNetwork(Network n)
 {
     try
     {
         //Create Progress Form
         FormProgress fp = new FormProgress();
         //Create Query
         connection = new SQLiteConnection("Data Source=" + dbpath + ";Version=3; foreign keys=true;");
         connection.Open();
         command = new SQLiteCommand(connection);
         command.CommandText = "UPDATE network SET name=@name, inputtype=@inputtype WHERE networkNumber=@nnr ;";
         command.Parameters.AddWithValue("@nnr", n.NetworkNumber);
         command.Parameters.AddWithValue("@name", n.Name);
         command.Parameters.AddWithValue("@inputtype", n.InputType);
         command.ExecuteNonQuery();
         //drop old ip address connection to this network
         command.CommandText = "DELETE FROM networkipadress WHERE networkNumber=@nnr ;";
         command.Parameters.AddWithValue("@nnr", n.NetworkNumber);
         //initialize Progress Form
         fp.SetMax(n.IpAddresses.Count);
         fp.Show();
         //Add new ip-Addresses
         SQLiteTransaction transaction = connection.BeginTransaction();
         foreach (IPAddress ip in n.IpAddresses)
         {
             try
             {//Write IP to table, if it is already there, throw exception
                 command.CommandText = "INSERT INTO ipaddresses (ipaddress) VALUES(@ipaddress);";
                 command.Parameters.AddWithValue("@ipaddress", ip.ToString());
                 command.ExecuteNonQuery();
             }
             catch (Exception e)
             {
                 Log.WriteLog("Error writing IP to DB: " + e.Message);
             }
             try
             {//Connect ip to netwok in database
                 command.CommandText = "INSERT INTO networkipadress (networkNumber, ipaddress) VALUES(@networkNumber, @ipaddress);";
                 command.Parameters.AddWithValue("@networkNumber", n.NetworkNumber);
                 command.Parameters.AddWithValue("@ipaddress", ip.ToString());
                 command.ExecuteNonQuery();
             }
             catch (Exception e)
             {
                 Log.WriteLog("Error writing IP/NW to DB: " + e.Message);
             }
             fp.Progress();
         }
         transaction.Commit();
         Log.WriteLog(string.Format("Network {0} changed in database.", n.Name));
         fp.Close();
     }
     catch (Exception e)
     {
         MessageBox.Show("Error SQL Write Network", "", MessageBoxButtons.OK, MessageBoxIcon.Error);
         Log.WriteLog(e.Message);
     }
     connection.Close();
 }
Пример #2
0
 public void SaveNetwork(Network n, NetworkInventory ni)
 {
     try
     {
         //Create Progress Form
         FormProgress fp = new FormProgress();
         //Create Query
         connection = new SQLiteConnection("Data Source=" + dbpath + ";Version=3; foreign keys=true;");
         connection.Open();
         command = new SQLiteCommand(connection);
         command.CommandText = "INSERT INTO network (networkNumber, name, inputtype, networkInventoryNumber) VALUES(@networkNumber, @name, @inputtype, @networkInventoryNumber);";
         command.Parameters.AddWithValue("@networkNumber", n.NetworkNumber);
         command.Parameters.AddWithValue("@name", n.Name);
         command.Parameters.AddWithValue("@inputtype", n.InputType);
         command.Parameters.AddWithValue("@networkInventoryNumber", ni.NetworkInventoryNumber);
         command.ExecuteNonQuery();
         SQLiteTransaction transaction = connection.BeginTransaction();
         //Initialize Progress Form
         fp.SetMax(n.IpAddresses.Count);
         fp.Show();
         foreach (IPAddress ip in n.IpAddresses)
         {
             try
             {//Write IP to table, if it is already there, throw exception
                 command.CommandText = "INSERT INTO ipaddresses (ipaddress) VALUES(@ipaddress);";
                 command.Parameters.AddWithValue("@ipaddress", ip.ToString());
                 command.ExecuteNonQuery();
                 //Log.WriteLog("IPAddress {0} saved to database", ip.ToString());
             }
             catch (Exception e)
             {
                 Log.WriteLog("Error writing IP to DB: " + e.Message);
             }
             try
             {//Connect ip to netwok in database
                 command.CommandText = "INSERT INTO networkipadress (networkNumber, ipaddress) VALUES(@networkNumber, @ipaddress);";
                 command.Parameters.AddWithValue("@networkNumber", n.NetworkNumber);
                 command.Parameters.AddWithValue("@ipaddress", ip.ToString());
                 command.ExecuteNonQuery();
                 //Log.WriteLog("IPaddress {0} connected to network {1} in database.", ip.ToString(), n.Name);
             }
             catch (Exception e)
             {
                 Log.WriteLog("Error writing IP/NW to DB: " + e.Message);
             }
             fp.Progress();
         }
         transaction.Commit();
         Log.WriteLog(string.Format("Network {0} saved to database.", n.Name));
         fp.Close();
     }
     catch (Exception e)
     {
         MessageBox.Show("Error SQL Write Network", "", MessageBoxButtons.OK, MessageBoxIcon.Error);
         Log.WriteLog(e.Message);
     }
     connection.Close();
 }