//Выполняет запрос на изменение public static void Execute(SqlProps props, string stSql) { var con = Connect(props); var com = new SqlCommand(stSql, con) { CommandTimeout = 300 }; bool b = false; int i = 0; Exception exc = null; while (!b && i++ < 3) { try { com.ExecuteNonQuery(); b = true; } catch (Exception ex) { exc = ex; Thread.Sleep(500); } } try { con.Close(); } catch {} if (!b) { throw exc; } }
private void butCheck_Click(object sender, EventArgs e) { var props = new SqlProps(ServerName.Text, DatabaseName.Text, Identification != "Windows", Login.Text, Password.Text); try { SqlDb.Connect(ServerName.Text, DatabaseName.Text, Identification != "Windows", Login.Text, Password.Text).Close(); } catch (Exception ex) { MessageBox.Show("Соединение не установлено\n" + ex.Message, "", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); return; } try { if (AdditionalCheck != null && !AdditionalCheck(props)) { MessageBox.Show("Соединение не установлено", "", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } else { MessageBox.Show("Успешное соединение", "", MessageBoxButtons.OK, MessageBoxIcon.Information); } } catch (Exception ex) { MessageBox.Show("База данных не является архивом результатов или к ней нет доступа" + "\n" + ex.Message, "", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } }
//Ридер для SQL Server, timeout - ограничение времени на команду, сек public ReaderAdo(SqlProps props, string stSql, int timeout = 300) { _useDb = false; _props = props; DatabaseType = DatabaseType.SqlServer; _connection = SqlDb.Connect(props); OpenReader(stSql, _connection, timeout); }
//stSql - строка запроса, props - свойства соединения public DataSetSql(SqlProps props, string stSql) { _con = SqlDb.Connect(props); _adapter = new SqlDataAdapter(stSql, _con) { SelectCommand = { CommandTimeout = 100000 } }; new SqlCommandBuilder(_adapter); Reload(); }
//Ридер для SQL Server public AdoReader(SqlProps props, //Настройки соединения с SQL string stSql, //запрос int timeout = 300) //ограничение времени на команду, сек { _useDb = false; _props = props; DatabaseType = DatabaseType.SqlServer; _connection = SqlDb.Connect(props); OpenReader(stSql, _connection, timeout); }
//Список всех таблиц базы данных public static List <string> SqlTablesList(SqlProps props) { var list = new List <string>(); using (var rec = new ReaderAdo(props, "SELECT * FROM sys.tables WHERE type_desc = 'USER_TABLE'")) while (rec.Read()) { list.Add(rec.GetString("Name")); } return(list); }
//Создать таблицу в базе данных на основе шаблона, если такой еще нет public static void CreateTable(SqlProps props, //Свойства базы данных string templateTable, //Имя таблицы - шаблона string createdTable) //Имя создаваемой таблицы { var con = Connect(props); if (DBNull.Value.Equals(new SqlCommand("SELECT object_id('" + createdTable + "')", con).ExecuteScalar())) { new SqlCommand("SELECT * INTO " + createdTable + " FROM " + templateTable, con).ExecuteNonQuery(); } try { con.Close(); } catch { } }
public SqlBulk(SqlProps props, string tabl) { _tabl = tabl; _props = props; //Получение списка колонок таблицы using (var rec = new AdoReader(props, "SELECT fld.name, fld.length FROM sysobjects tab, syscolumns fld WHERE tab.id=fld.id And tab.name='" + tabl + "'")) { int i = 0; while (rec.Read()) { _cols.Add(rec.GetString("name"), i++); _colsSize.Add(rec.GetString("name"), rec.GetInt("length")); } _starter = new BulkStarter(_cols.Count, _table); } }
//sqlIdent=true - идентификация SqlServer иначе Windows public static SqlConnection Connect(SqlProps props) { var st = "Data Source=" + (props.ServerName ?? "") + ";Initial Catalog=" + (props.DatabaseName ?? "") + ";Integrated Security="; if (!props.SqlIdent) { st += "True"; } else { st += "False;User="******"") + "; Password="******""); } var con = new SqlConnection(st); con.Open(); return(con); }
public SqlInserter(SqlProps props, string table) { _sqlProps = props; _connection = SqlDb.Connect(props); _table = table; }