Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //bound
- <?xml version="1.0" encoding="utf-8" ?>
- <configuration>
- <connectionStrings>
- <add name ="SQLClient"
- providerName="System.Data.SqlClient"
- connectionString = "Server=localhost; Database=Lab12; User ID=sa; Password=Nikusha1299"/>
- connectionString="Server=localhost;Database=Lab12;Trusted_Connection=true"/>
- // connectionString="Server=192.168.0.92\sqls2005; Database=lab12; User ID=sa;
- Password=Nikusha1299"/>
- </connectionStrings>
- </configuration>
- using System;
- using System.Configuration;
- using System.Data;
- using System.Data.SqlClient;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace Lab12
- {
- internal class Program
- {
- //private static SqlConnection cn;
- // Create and open a connection.
- /*private static void Open()
- {
- string connection_string = "Server=localhost; Database=Lab12; User ID=sa; Password=Nikusha1299";
- Console.WriteLine(connection_string);
- try
- {
- cn = new SqlConnection(connection_string);
- cn.Open();
- }
- catch (SqlException ex)
- {
- Console.WriteLine("Error: " + ex.Message);
- return;
- }
- }*/
- /*private static void Close()
- {
- if (cn != null && cn.State != ConnectionState.Closed)
- {
- cn.Close();
- }
- }*/
- //операция просмотра содержимого таблиц
- private static void SelectAll(SqlConnection cn)
- {
- try
- {
- cn.Open();
- // Create a SQL command object.
- String strSQL = "Select * From FilmLibrary";
- SqlCommand myCommand = new SqlCommand(strSQL, cn);
- // Obtain a data reader
- SqlDataReader dr;
- dr = myCommand.ExecuteReader();
- // Loop over the results.
- while (dr.Read())
- {
- Console.Write("|");
- //for (int i = 0; i < dr.FieldCount; i++)
- //{
- Console.WriteLine("-> id: {0}, film: {1}.",
- dr["id"].ToString().Trim(), dr["film"].ToString().Trim());
- //}
- Console.WriteLine();
- }
- Console.WriteLine("\n");
- dr.Close();
- if (cn != null && cn.State != ConnectionState.Closed)
- {
- cn.Close();
- }
- }
- catch (SqlException ex)
- {
- Console.WriteLine("Error: " + ex.Message);
- cn.Close();
- }
- }
- //операция вставки данных в таблицах
- private static void Insert(SqlConnection cn, int id, string film)
- {
- try
- {
- cn.Open();
- SqlCommand cmd = cn.CreateCommand();
- cmd.Connection = cn;
- cmd.CommandText = "INSERT FilmLibrary (id, film) VALUES (@id, @film);";
- SqlParameter[] parameters = new SqlParameter[2];
- parameters[0] = new SqlParameter
- {
- ParameterName = "@id",
- Value = id,
- SqlDbType = SqlDbType.Int
- };
- parameters[1] = new SqlParameter
- {
- ParameterName = "@film",
- Value = film,
- SqlDbType = SqlDbType.VarChar
- };
- cmd.Parameters.AddRange(parameters);
- cmd.ExecuteNonQuery();
- if (cn != null && cn.State != ConnectionState.Closed)
- {
- cn.Close();
- }
- }
- catch (SqlException ex)
- {
- Console.WriteLine("Error: " + ex.Message);
- cn.Close();
- }
- }
- //операция удаления данных в таблицах
- private static void Delete(SqlConnection cn, int id)
- {
- try
- {
- cn.Open();
- // Create a SQL command object.
- SqlCommand cmd = cn.CreateCommand();
- cmd.Connection = cn;
- cmd.CommandText = "DELETE FilmLibrary WHERE (id = @id);";
- SqlParameter[] parameters = new SqlParameter[1];
- parameters[0] = new SqlParameter
- {
- ParameterName = "@id",
- Value = id,
- SqlDbType = SqlDbType.Int
- };
- cmd.Parameters.AddRange(parameters);
- cmd.ExecuteNonQuery();
- if (cn != null && cn.State != ConnectionState.Closed)
- {
- cn.Close();
- }
- }
- catch (SqlException ex)
- {
- Console.WriteLine("Error: " + ex.Message);
- cn.Close();
- }
- }
- //операция изменения данных в таблицах
- private static void Update(SqlConnection cn, int id, string film)
- {
- try
- {
- cn.Open();
- SqlCommand cmd = cn.CreateCommand();
- cmd.Connection = cn;
- cmd.CommandText = "UPDATE FilmLibrary SET film = @film WHERE (id =@id);";
- SqlParameter[] parameters = new SqlParameter[2];
- parameters[0] = new SqlParameter
- {
- ParameterName = "@id",
- Value = id,
- SqlDbType = SqlDbType.Int
- };
- parameters[1] = new SqlParameter
- {
- ParameterName = "@film",
- Value = film,
- SqlDbType = SqlDbType.VarChar
- };
- cmd.Parameters.AddRange(parameters);
- cmd.ExecuteNonQuery();
- if (cn != null && cn.State != ConnectionState.Closed)
- {
- cn.Close();
- }
- }
- catch (SqlException ex)
- {
- Console.WriteLine("Error: " + ex.Message);
- cn.Close();
- }
- }
- public static void Main(string[] args)
- {
- //string connectionString = "Server=localhost; Database=Lab12; User ID=sa; Password=Nikusha1299";
- //Console.WriteLine(connectionString);
- //SqlConnection cn = new SqlConnection();
- using (SqlConnection cn = new SqlConnection())
- {
- cn.ConnectionString = ConfigurationManager.ConnectionStrings["SQLClient"].ConnectionString;
- //cn = new SqlConnection(connectionString);
- Console.OutputEncoding = Encoding.UTF8;
- SelectAll(cn);
- Insert(cn, 7, "Покорители волн");
- SelectAll(cn);
- Update(cn, 7, "Can not be readable");
- SelectAll(cn);
- Delete(cn,7);
- SelectAll(cn);
- }
- Console.ReadLine();
- //Console.ReadKey(); //Получает следующий нажатый пользователем символ или функциональную клавишу. Нажатая клавиша отображается в окне консоли.
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement