protected void buttonProvjeriServerDatabaseString_Click(object sender, EventArgs e) { SimplerAES saes = new SimplerAES(); string connstring; // tu sad testiramo da li se možemo spojiti na server ili ćemo dobiti grešku //connString = "Server = (LocalDB)\\MSSQLLocalDB; AttachDbFilename=|DataDirectory|\\BMS_Data.mdf; Integrated Security = False; User ID=tonza;Password=tonza"; connstring = "Server = " + textBoxServerServerString.Text + ";"; // ovaj dio dopisujemo samo ako postoji u bazi, a koristi se samo kad se radi o localDB-u, kada treba attachati bazu. Kad se spajamo na "Veliki" SQL to nam ne treba, pa kod unosa upozori korisnika na to if (textBoxServerDatabaseAttachString.Text.Length > 0) connstring += "AttachDbFileName= " + textBoxServerDatabaseAttachString.Text + ";"; connstring += "Database = " + textBoxServerDatabaseName.Text + ";"; connstring += "Integrated Security = " + (checkBoxServerDatabaseIntegratedAuth.Checked == true ? "True" : "False") + ";"; // ako se ne koristi integrated, onda trebamo user name i password if (!checkBoxServerDatabaseIntegratedAuth.Checked) { connstring += "User ID=" + textBoxServerDatabaseKorisnik.Text + ";"; connstring += "Password="******";"; } // mi bi i manji timeout isto connstring += "Connection Timeout=3"; bool uspjesno = true; string greska =""; SqlConnection myConn = new SqlConnection(connstring); try { myConn.Open(); } catch (SqlException se ) { uspjesno = false; greska = se.Message; } if (uspjesno) { labelServerProvjeraStringa.Text = "Uspješno spajanje na server. Ne zaboravite snimiti testirane postavke."; textBoxServerDatabaseLozinka.Attributes["h"] = saes.Encrypt(textBoxServerDatabaseLozinka.Text); // dozvoli snimanje podataka buttonServerSnimiPostavke.Enabled = true; } else { labelServerProvjeraStringa.Text = "Neuspješno spajanje na server !!! Greška:"+greska; } }
protected void buttonServerSnimiPostavke_Click(object sender, EventArgs e) { SemaforiDataContext db = new SemaforiDataContext(); SimplerAES saes = new SimplerAES(); if (Convert.ToInt32(labelServerID.Text) > 0) { // ako imamo ID, znači da mijenjamo postojeći tblServer server = (from tblServer in db.tblServers where tblServer.id == Convert.ToInt32(labelServerID.Text) select tblServer).Single(); server.naziv = textBoxServerNaziv.Text; server.serverString = textBoxServerServerString.Text; db.SubmitChanges(); tblDB dbase = (from tblDB in db.tblDBs where tblDB.serverid == server.id select tblDB).Single(); dbase.naziv = textBoxServerDatabaseName.Text; dbase.serverid = server.id; dbase.integratedAuth = checkBoxServerDatabaseIntegratedAuth.Checked; dbase.korisnik = textBoxServerDatabaseKorisnik.Text; dbase.dbAttachString = textBoxServerDatabaseAttachString.Text; // kriptiramo lozinku da je baš ne spremimo u clear text u bazu // ako nešto piše u TXt, to je nova lozinka. Spremi u H atribut if (textBoxServerDatabaseLozinka.Text.Length>0) { textBoxServerDatabaseLozinka.Attributes["h"] = saes.Encrypt(textBoxServerDatabaseLozinka.Text); } dbase.lozinka = textBoxServerDatabaseLozinka.Attributes["h"]; db.SubmitChanges(); (sender as Button).Enabled = false; labelServerProvjeraStringa.Text = ""; } else { //nemamo ID, upisujemo novi tblServer server = new tblServer(); tblDB dbase = new tblDB(); server.naziv = textBoxServerNaziv.Text; server.serverString = textBoxServerServerString.Text; db.tblServers.InsertOnSubmit(server); db.SubmitChanges(); dbase.naziv = textBoxServerDatabaseName.Text; dbase.serverid = server.id; dbase.integratedAuth = checkBoxServerDatabaseIntegratedAuth.Checked; dbase.korisnik = textBoxServerDatabaseKorisnik.Text; dbase.dbAttachString = textBoxServerDatabaseAttachString.Text; // kriptiramo password prije nego ga upišemo u bazu textBoxServerDatabaseLozinka.Attributes["h"] = saes.Encrypt(textBoxServerDatabaseLozinka.Text); dbase.lozinka = saes.Encrypt(textBoxServerDatabaseLozinka.Text); db.tblDBs.InsertOnSubmit(dbase); db.SubmitChanges(); (sender as Button).Enabled = false; labelServerProvjeraStringa.Text = ""; } ListViewServeri.DataBind(); menuUpdatePanel.Update(); }