Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Text;
- using System.Windows.Forms;
- using System.Xml;
- using System.IO;
- using System.Resources;
- using Npgsql;
- namespace Rekod
- {
- public partial class Login : Form
- {
- XmlDocument xml = new XmlDocument();
- XmlNode node;
- public NpgsqlConnection conn;
- private NpgsqlCommand qwCmd;
- private NpgsqlDataReader zxReader;
- string base_text = "";
- string host_text = "";
- string port_text = "";
- public int id;
- public string nameuser, connStr;
- public bool close = false, admin = false, visible_noactive = false;
- public Login()
- {
- InitializeComponent();
- qwCmd = new NpgsqlCommand();
- }
- public Login(string user, string pass, string host, string port, string baza)
- {
- InitializeComponent();
- comboBox2.Text = user;
- textBox1.Text = pass;
- base_text = baza;
- host_text = host;
- port_text = port;
- comboBox1.Text = baza + "@" + host + ":" + port;
- Program.ipString = host;
- Program.dbString = baza;
- Program.portString = port;
- qwCmd = new NpgsqlCommand();
- conn = getConn();
- if (conn != null)
- {
- qwCmd.Connection = conn;
- getUser();
- }
- else
- {
- ShowDialog();
- }
- }
- private void eror()
- {
- panel1.Visible = true;
- panel2.Visible = false;
- panel2.Enabled = false;
- panel1.Enabled = true;
- }
- private void komponets()
- {
- string line = comboBox1.Text;//.ToString();
- char[] sep;
- sep = new char[1];
- sep[0] = '@';
- string[] array_line = line.Split(sep);
- base_text = array_line[0];
- try
- {
- sep[0] = ':';
- array_line = array_line[1].Split(sep);
- port_text = array_line[1];
- }
- catch
- { }
- finally
- {
- host_text = array_line[0];
- if (port_text == "")
- {
- port_text = "5432";
- }
- }
- }
- private string[] partitionedIntoSubstrings(char SeparatesSymbol, string SharedLine)
- { char[] sep;
- sep = new char[1];
- sep[0] = SeparatesSymbol;
- string[] array_line = SharedLine.Split(sep);
- return array_line;
- }
- private int[] SelectCountBasePortUser(XmlNode nod, string UserName)
- {
- int schetchik_base = 0;
- int schetchik_port = 0;
- int schetchik_user = 0;
- foreach (XmlNode nod2 in nod.ChildNodes)
- {
- if (nod2.Name == "dataBase")
- {
- if (nod2.FirstChild.Value == base_text)
- {
- schetchik_base++;
- }
- }
- if (nod2.Name == "users")
- {
- if (nod2.FirstChild.Value == UserName && UserName != "")
- {
- schetchik_user++;
- }
- }
- if (nod2.Name == "port")
- {
- if (nod2.FirstChild != null)
- {
- if (nod2.FirstChild.Value == port_text)
- {
- schetchik_port++;
- }
- }
- }
- }
- int[] Mas = {schetchik_base, schetchik_user,schetchik_port};
- return Mas;
- }
- private void insert_xml(string StringConnect, string UserName)
- {
- if (StringConnect != "")
- {
- string base_text = partitionedIntoSubstrings('@', StringConnect)[0];
- string host_text = partitionedIntoSubstrings(':', partitionedIntoSubstrings('@', StringConnect)[1])[0];
- try
- {
- string port_text = partitionedIntoSubstrings(':', partitionedIntoSubstrings('@', StringConnect)[1])[1];
- }
- catch
- {
- port_text = "";
- }
- string user_text = "";
- int schetchik_host = 0;
- foreach (XmlNode nod in node.ChildNodes)
- {
- if (host_text == nod.FirstChild.Value)
- {
- schetchik_host++;
- int[] countSchetchik= SelectCountBasePortUser(nod, UserName);
- AddXml(countSchetchik, nod, port_text);
- }
- }
- if (schetchik_host == 0)
- {
- XmlElement user = xml.CreateElement("users");
- user.InnerText = user_text;
- XmlElement port = xml.CreateElement("port");
- port.InnerText = port_text;
- XmlElement nameBase = xml.CreateElement("dataBase");
- nameBase.InnerText = base_text;
- XmlElement host = xml.CreateElement("host");
- host.InnerText = host_text;
- host.AppendChild(nameBase);
- host.AppendChild(user);
- if (port_text != "")
- {
- host.AppendChild(port);
- }
- node.InsertAfter(host, node.FirstChild);
- try
- {
- xml.Save(Program.path_string + "\\" + Program.setting_file);
- }
- catch
- {
- MessageBox.Show("Не найдена конфигурация!", "Ошибка!", MessageBoxButtons.OK, MessageBoxIcon.Error);
- }
- }
- }
- }
- private void AddXml(int[] countSchetchik,XmlNode nod, string port_text )
- {
- string user_text = "";
- bool change = false;
- int schetchik_base = countSchetchik[0];
- int schetchik_port = countSchetchik[1];
- int schetchik_user = countSchetchik[2];
- if (schetchik_user + schetchik_port + schetchik_base != 0)
- {
- if (schetchik_base == 0)
- {
- XmlElement nameBase = xml.CreateElement("dataBase");
- nameBase.InnerText = base_text;
- nod.InsertAfter(nameBase, nod["users"]);
- change = true;
- }
- if (schetchik_port == 0)
- {
- if (port_text != "" && port_text != " " && port_text != null)
- {
- XmlElement port = xml.CreateElement("port");
- port.InnerText = port_text;
- nod.InsertAfter(port, nod["users"]);
- change = true;
- }
- }
- if (schetchik_user == 0)
- {
- XmlElement user = xml.CreateElement("users");
- user.InnerText = user_text;
- nod.InsertAfter(user, nod["users"]);
- change = true;
- }
- try
- {
- if (change)
- {
- xml.Save(Program.path_string + "\\" + Program.setting_file);
- }
- }
- catch
- {
- MessageBox.Show("Ошибка сохранения файла настроек! " +
- "Возможно у вас недостаточно прав доступа к рабочей папке!",
- "Ошибка!", MessageBoxButtons.OK, MessageBoxIcon.Error);
- }
- }
- }
- private void fill_combobox()
- {
- string line = "";
- foreach (XmlNode nod1 in node.ChildNodes)
- {
- line = "@" + nod1.FirstChild.Value;
- foreach (XmlNode nod2 in nod1.ChildNodes)
- {
- if (nod2.Name == "dataBase")
- {
- string line_name = line;
- line = nod2.InnerText + line;
- int i = 0;
- foreach (XmlNode port in nod1.ChildNodes)
- {
- if (port.Name == "port")
- {
- if (port.InnerText != "")
- {
- string line_port = line + ":" + port.InnerText;
- comboBox1.Items.Add(line_port);
- i++;
- }
- else
- {
- if (i == 0)
- {
- comboBox1.Items.Add(line);
- i++;
- }
- }
- }
- else
- {
- if (i == 0)
- {
- comboBox1.Items.Add(line);
- i++;
- }
- }
- }
- line = line_name;
- }
- }
- }
- }
- private void Login_Load(object sender, EventArgs e)
- {
- System.Resources.ResourceManager resources = new System.Resources.ResourceManager("Images", this.GetType().Assembly);
- #if gs
- pictureBox1.Location = new System.Drawing.Point(26, 23);
- this.pictureBox1.Image = global::Rekod.Properties.Resources.a_c48d37de1;
- this.Text = "ООО \"Градосервис\" MapEditor";
- #endif
- #if rls
- this.pictureBox1.Image = global::Rekod.Properties.Resources.upload_logotip_cku22;
- this.Text = "ОАО \"НПК РЕКОД\" РЕКОД Инфраструктура";
- #endif
- try
- {
- xml.Load(Program.path_string + "\\" + Program.setting_file);
- node = xml.DocumentElement;
- fill_combobox();
- }
- catch
- {
- MessageBox.Show("Не найдена конфигурация!", "Ошибка!", MessageBoxButtons.OK, MessageBoxIcon.Error);
- OpenFileDialog openFile = new OpenFileDialog();
- openFile.Title = "Укажите файл конфигурации!";
- openFile.FileName = "settings.aews";
- openFile.Filter = "Файл конфигурации *.aews|*.aews";
- if (openFile.ShowDialog() == DialogResult.OK)
- {
- //Program.path_string = Environment.CurrentDirectory;
- try
- {
- Program.path_string = System.IO.Path.GetDirectoryName(openFile.FileName);
- xml.Load(openFile.FileName);
- node = xml.DocumentElement;
- Program.setting_file = openFile.SafeFileName;
- fill_combobox();
- }
- catch
- {
- MessageBox.Show("Данный файл не является файлом конфигурации!", "Ошибка!", MessageBoxButtons.OK, MessageBoxIcon.Error);
- Application.ExitThread();
- }
- }
- else
- {
- Application.ExitThread();
- }
- }
- }
- private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
- {
- comboBox2.Items.Clear();
- string name_dirty = comboBox1.SelectedItem.ToString();
- char[] sep;
- sep = new char[1];
- sep[0] = '@';
- string[] name_frash = name_dirty.Split(sep);
- foreach (XmlNode nod1 in node.ChildNodes)
- {
- sep[0] = ':';
- try
- {
- name_frash = name_frash[1].Split(sep);
- }
- catch
- { }
- if (nod1.FirstChild.Value == name_frash[0])
- {
- foreach (XmlNode nod2 in nod1.ChildNodes)
- {
- if (nod2.Name == "users")
- {
- comboBox2.Items.Add(nod2.InnerText);
- }
- }
- }
- }
- }
- private NpgsqlConnection getConn()
- {
- conn = new NpgsqlConnection();
- if (comboBox1 != null)
- {
- komponets();
- Program.ipString = host_text;
- Program.dbString = base_text;
- Program.portString = port_text;
- string constr = "Server=" + host_text + ";Port=" + port_text + ";database=" + base_text +
- ";User Id=" + comboBox2.Text + "; Password=" + textBox1.Text + ";";
- conn = new NpgsqlConnection();
- conn.ConnectionString = constr;
- connStr = conn.ConnectionString;
- try
- {
- conn.Open();
- }
- catch (Exception ex)
- {
- if (textBox2.Text == "")
- textBox2.Text = ex.Message;
- return null;
- }
- qwCmd = new NpgsqlCommand();
- qwCmd.Connection = conn;
- Program.conn = conn;
- Program.sqlCmd = new SQLCommand();
- return conn;
- }
- Program.conn = conn;
- Program.sqlCmd = new SQLCommand();
- return conn;
- }
- private void button1_Click(object sender, EventArgs e)
- {
- bool err = false;
- textBox2.Text = "";
- try
- {
- qwCmd = new NpgsqlCommand();
- conn = getConn();
- if (conn != null)
- {
- qwCmd.Connection = conn;
- insert_xml(comboBox1.Text, comboBox2.Text);
- getUser();
- }
- else
- {
- err = true;
- if (textBox2.Text == "")
- textBox2.Text = "Отсутствует подключение к базе данных.";
- }
- }
- catch (Exception exe)
- {
- err = true;
- if (textBox2.Text == "")
- textBox2.Text = exe.ToString();
- }
- finally
- {
- if (textBox2.Text != "")
- eror();
- int i = 0;
- switch (textBox2.Text)
- {
- case "Connection string argument missing!\r\nИмя параметра: HOST":
- textBox2.Text = "Отсутствует адрес сервера в строке соединения.";
- i++;
- break;
- case "Connection string argument missing!\r\nИмя параметра: USER ID":
- textBox2.Text = "Отсутствует имя пользователя.";
- i++;
- break;
- }
- if (i == 0)
- {
- try
- {
- string[] erorr = textBox2.Text.Split(' ');
- switch (erorr[1])
- {
- case "28000:":
- textBox2.Text = "Неверный логин, или пароль для пользователя '" + comboBox2.Text + "'";
- break;
- case "3D000:":
- textBox2.Text = "Базы данных '" + base_text + "' не существует.";
- break;
- }
- }
- catch
- { }
- }
- }
- if (!err)
- {
- Close();
- close = true;
- }
- }
- private void button3_Click(object sender, EventArgs e)
- {
- close = false;
- if (qwCmd != null)
- {
- qwCmd.Dispose();
- }
- Close();
- }
- private void label3_Click(object sender, EventArgs e)
- {
- }
- private void button2_Click(object sender, EventArgs e)
- {
- close = false;
- if (qwCmd != null)
- {
- qwCmd.Dispose();
- }
- Close();
- }
- private void button4_Click(object sender, EventArgs e)
- {
- this.Size = new System.Drawing.Size(514, 233);
- textBox2.Size = new System.Drawing.Size(502, 65);
- textBox2.Visible = true;
- }
- public void getUser()
- {
- if (conn == null)
- {
- getConn();
- }
- qwCmd.CommandText = "SELECT id, name_full, admin FROM " + Program.scheme + ".user_db WHERE login = @file_blob AND pass = @file_blob2";
- NpgsqlParameter param = new NpgsqlParameter("@file_blob", DbType.String);
- param.Value = comboBox2.Text;
- qwCmd.Parameters.Add(param);
- param = new NpgsqlParameter("@file_blob2", DbType.String);
- param.Value = textBox1.Text;
- qwCmd.Parameters.Add(param);
- zxReader = qwCmd.ExecuteReader();
- if (zxReader.Read())
- {
- id = Convert.ToInt32(zxReader.GetValue(0).ToString());
- Program.id_user = id;
- nameuser = zxReader.GetValue(1).ToString();
- admin = zxReader.GetBoolean(2);
- zxReader.Close();
- getInfoSystem();
- }
- else
- {
- textBox2.Text = "Пользователь " + comboBox2.Text + " не найден!";
- zxReader.Close();
- return;
- }
- qwCmd.Dispose();
- close = true;
- Close();
- }
- private void getInfoSystem()
- {
- classesOfMetods cls = new classesOfMetods(conn);
- cls.reloadInfo();
- }
- private void textBox2_KeyUp(object sender, KeyEventArgs e)
- {
- if (e.KeyCode == Keys.Enter)
- {
- button2_Click(button2, null);
- }
- }
- private void comboBox1_TextChanged(object sender, EventArgs e)
- {
- comboBox2.Items.Clear();
- }
- private void button5_Click(object sender, EventArgs e)
- {
- panel1.Visible = false;
- panel2.Visible = true;
- this.Size = new System.Drawing.Size(496, 161);
- textBox2.Size = new System.Drawing.Size(502, 65);
- textBox2.Visible = false;
- panel2.Enabled = true;
- panel1.Enabled = false;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement