private void button1_Click(object sender, EventArgs e) { try { if (textBox1.Text != null && textBox2.Text != null && textBox3.Text != null && comboBox1.SelectedIndex != -1) { int role = comboBox1.SelectedIndex; string str = ""; switch (role) { case 0: str = "db_owner"; break; case 1: str = "DecanatRole"; break; case 2: str = "TeacherRole"; break; case 3: str = "StarostaRole"; break; case 4: str = "StudentRole"; break; } role++; String quertString = @"insert into _User (login, password, id_role) values ('" + textBox2.Text + "','" + textBox3.Text + "','" + role.ToString() + "');"; SqlCommand insert = new SqlCommand(quertString, con); con.Open(); insert.ExecuteNonQuery(); con.Close(); //СОЗДАНИЕ String quertString2 = @"EXEC sp_addlogin " + textBox2.Text + "," + textBox3.Text + ",UniverConnect"; SqlCommand create2 = new SqlCommand(quertString2, con); con.Open(); create2.ExecuteNonQuery(); con.Close(); String quertString3 = @"EXEC sp_adduser " + textBox2.Text + "," + textBox1.Text + ""; SqlCommand create3 = new SqlCommand(quertString3, con); con.Open(); create3.ExecuteNonQuery(); con.Close(); String quertString4 = @"EXEC sp_addrolemember " + str + "," + textBox1.Text + " "; SqlCommand create4 = new SqlCommand(quertString4, con); con.Open(); create4.ExecuteNonQuery(); con.Close(); MDIParent1 my = Application.OpenForms.OfType <MDIParent1>().Single(); my.UserRegistered(); } else { MessageBox.Show("Заполнены не все поля", "Ошибка"); } this.Close(); } catch (Exception ex) { MessageBox.Show(ex.Message, "У вас недостаточно прав!"); } }
private void button1_Click(object sender, EventArgs e) { String ConnectString = "Integrated Security=false; User Id = " + textBox1.Text + "; Password = "******"; server= " + @"DESKTOP-FNK5TNL\MY" + "; Initial Catalog = " + "UniverConnect"; //DESKTOP-FNK5TNL\MY, UniverConnect } else { ConnectString += "; server= " + textBox3.Text + "; Initial Catalog = " + textBox4.Text; } // Объявление новой переменной типа SqlConection SqlConnection con = new SqlConnection(ConnectString); Exception error = null; // Переменная, представляющая ошибки, появляющиеся во время выполнения приложения try { con.Open(); // Открытие подключения } catch (Exception ex) // При возникновении неполадок при подключении появится сообщение с информацией об ошибке { error = ex; // Переменная error запоминает конкретную ошибку MessageBox.Show(string.Format("Error: {0}", ex.Message), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } finally { if (error == null) // Если переменная не изменилась, то ошибок при подключении не было и соединение с БД установлено { SqlCommand command = new SqlCommand($" sp_helpuser'{textBox1.Text}'", con); SqlDataReader reader = command.ExecuteReader(); reader.Read(); string a = reader["Rolename"].ToString(); reader.Close(); SqlCommand command1 = new SqlCommand($" SELECT id_user FROM dbo._User WHERE(login = SUSER_SNAME())", con); SqlDataReader reader1 = command1.ExecuteReader(); reader1.Read(); int id_user = Convert.ToInt32(reader1[0]); reader1.Close(); SqlCommand command2 = new SqlCommand($"SELECT Changed.id_user FROM Changed where Changed.id_user = '******' ", con); SqlDataReader reader2 = command2.ExecuteReader(); reader2.Read(); bool est = reader2.HasRows; reader2.Close(); con.Close(); switch (a) { case "db_owner": MDIParent1 mdi = new MDIParent1(con); mdi.Show(); break; case "DecanatRole": Decanat decanat = new Decanat(); decanat.Show(); break; case "TeacherRole": Teacher teach = new Teacher(id_user, est, con); teach.Show(); break; case "StarostaRole": Starosta starosta = new Starosta(con, id_user, est); starosta.Show(); break; case "StudentRole": Student stud = new Student(id_user, est, con); stud.Show(); break; } con.Close(); } } }
private static void Program_OnCloudAppInit() { MDIParent1 thisForm = new MDIParent1(); thisForm.Show(); }