using System Data Sql Client string connection String

  • Slides: 23
Download presentation
using System. Data. Sql. Client;

using System. Data. Sql. Client;

string connection. String = @"Data Source=. SQLEXPRESS; Initial Catalog=usersdb; Integrated Security=True"; // Создание подключения

string connection. String = @"Data Source=. SQLEXPRESS; Initial Catalog=usersdb; Integrated Security=True"; // Создание подключения Sql. Connection connection =new Sql. Connection(connection. String); try { // Открываем подключение connection. Open(); Console. Write. Line("Подключение открыто"); } catch (Sql. Exception ex) { Console. Write. Line(ex. Message); } finally { // закрываем подключение connection. Close(); Console. Write. Line("Подключение закрыто. . . "); }

static void Main(string[] args) { string connection. String = @"Data Source=. SQLEXPRESS; Initial Catalog=usersdb;

static void Main(string[] args) { string connection. String = @"Data Source=. SQLEXPRESS; Initial Catalog=usersdb; Integrated Security=True"; using (Sql. Connection connection = new Sql. Connection(connection. String)) { connection. Open(); Console. Write. Line("Подключение открыто"); } Console. Write. Line("Подключение закрыто. . . "); }

using (Sql. Connection connection = new Sql. Connection(connection. String)) { connection. Open(); Sql. Command

using (Sql. Connection connection = new Sql. Connection(connection. String)) { connection. Open(); Sql. Command command = new Sql. Command(); command. Command. Text = "SELECT * FROM Users"; command. Connection = connection; }

using (Sql. Connection connection = new Sql. Connection(connection. String)) { connection. Open(); Sql. Command

using (Sql. Connection connection = new Sql. Connection(connection. String)) { connection. Open(); Sql. Command command = new Sql. Command(); command. Command. Text = "SELECT * FROM Users"; command. Connection = connection; Sql. Data. Reader reader = command. Execute. Reader(); if(reader. Has. Rows) // если есть данные {// выводим названия столбцов Console. Write. Line("{0}t{1}t{2}", reader. Get. Name(0), reader. Get. Name(1), reader. Get. Name(2)); while (reader. Read()) // построчно считываем данные {object id = reader. Get. Value(0); object name = reader. Get. Value(1); object age = reader. Get. Value(2); Console. Write. Line("{0} t{1} t{2}", id, name, age); }} reader. Close(); }

using (Sql. Connection connection = new Sql. Connection(connection. String)) {connection. Open(); // Создаем объект

using (Sql. Connection connection = new Sql. Connection(connection. String)) {connection. Open(); // Создаем объект Data. Adapter Sql. Data. Adapter adapter = new Sql. Data. Adapter(sql, connection); // Создаем объект Dataset Data. Set ds = new Data. Set(); // Заполняем Dataset adapter. Fill(ds); // Отображаем данные data. Grid. View 1. Data. Source = ds. Tables[0]; }

try { this. Validate(); this. b. RICKBinding. Source. End. Edit(); this. table. Adapter. Manager

try { this. Validate(); this. b. RICKBinding. Source. End. Edit(); this. table. Adapter. Manager 1. Update. All(thi s. laba. Data. Set 1); Message. Box. Show("Данные внесены!"); } catch (Exception ex) {Message. Box. Show(ex. Message); }