BigLitho/Large.Lito.Database/Make3_Treko3D/Data/DataAccess/LayerSQL.cs

507 lines
18 KiB
C#

using System;
using System.Windows.Forms;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Data.DataAccess
{
using System.Data;
using System.Data.SqlClient;
using Data.Sql;
using Data.DataModel;
class LayerSQL : ConnectionSQL, ILayerSQL
{
/// <summary>
/// Method to get club member by Id
/// </summary>
/// <param name="id">member id</param>
/// <returns>Data row</returns>
public DataRow GetLayerById(int id)
{
DataTable dataTable = new DataTable();
DataRow dataRow;
string connectionString = this.ConnectionString;
using (SqlConnection sqlCon = new SqlConnection(connectionString))
{
using (SqlCommand comm = new SqlCommand())
{
string cmdString = Scripts.sqlGetLayerById;
comm.Connection = sqlCon;
comm.CommandText = cmdString;
comm.Parameters.AddWithValue("@Id", id);
try
{
SqlDataAdapter sd = new SqlDataAdapter();
sd.SelectCommand = comm;
sd.Fill(dataTable);
//sqlCon.Open();
//comm.ExecuteNonQuery();
}
catch (SqlException e)
{
// do something with the exception
// don't hide it
MessageBox.Show(
e.Message,
"Exception!",
//Resources.System_Error_Message_Title,
MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
// Get the datarow from the table
dataRow = dataTable.Rows.Count > 0 ? dataTable.Rows[0] : null;
}
}
return dataRow;
}
/// <summary>
/// Method to get project by name
/// </summary>
/// <param name="name">project name</param>
/// <returns>Data row</returns>
public DataRow GetLayerByNameTechnology(string name, string technology)
{
DataTable dataTable = new DataTable();
DataRow dataRow;
string connectionString = this.ConnectionString;
using (SqlConnection sqlCon = new SqlConnection(connectionString))
{
using (SqlCommand comm = new SqlCommand())
{
string cmdString = Scripts.sqlGetLayerByNameTechnology;
comm.Connection = sqlCon;
comm.CommandText = cmdString;
comm.Parameters.AddWithValue("@LayerName", name);
comm.Parameters.AddWithValue("@Technology", technology);
try
{
SqlDataAdapter sd = new SqlDataAdapter();
sd.SelectCommand = comm;
sd.Fill(dataTable);
//sqlCon.Open();
//comm.ExecuteNonQuery();
}
catch (SqlException e)
{
// do something with the exception
// don't hide it
MessageBox.Show(
e.Message,
"Exception!",
//Resources.System_Error_Message_Title,
MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
// Get the datarow from the table
dataRow = dataTable.Rows.Count > 0 ? dataTable.Rows[0] : null;
}
}
return dataRow;
}
/// <summary>
/// Method to get project by name
/// </summary>
/// <param name="name">project name</param>
/// <returns>Data row</returns>
public DataRow GetLayerByName(string name)
{
DataTable dataTable = new DataTable();
DataRow dataRow;
string connectionString = this.ConnectionString;
using (SqlConnection sqlCon = new SqlConnection(connectionString))
{
using (SqlCommand comm = new SqlCommand())
{
string cmdString = Scripts.sqlGetLayerByName;
comm.Connection = sqlCon;
comm.CommandText = cmdString;
comm.Parameters.AddWithValue("@LayerName", name);
try
{
SqlDataAdapter sd = new SqlDataAdapter();
sd.SelectCommand = comm;
sd.Fill(dataTable);
//sqlCon.Open();
//comm.ExecuteNonQuery();
}
catch (SqlException e)
{
// do something with the exception
// don't hide it
MessageBox.Show(
e.Message,
"Exception!",
//Resources.System_Error_Message_Title,
MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
// Get the datarow from the table
dataRow = dataTable.Rows.Count > 0 ? dataTable.Rows[0] : null;
}
}
return dataRow;
}
/// <summary>
/// Method to get all club members
/// </summary>
/// <returns>Data table</returns>
public DataTable GetProjectLayers(int projectId)
{
DataTable dataTable = new DataTable();
string connectionString = this.ConnectionString;
SqlConnection sqlCon = new SqlConnection(connectionString);
sqlCon.Open();
//string commandString = "SELECT * FROM ProjectSettings";
string commandString = Scripts.SqlGetProjectLayers;
SqlCommand sqlCmd = new SqlCommand(commandString, sqlCon);
sqlCmd.Parameters.AddWithValue("@ProjectId", projectId);
//SqlDataReader dr = sqlCmd.ExecuteReader();
SqlDataAdapter sd = new SqlDataAdapter();
sd.SelectCommand = sqlCmd;
sd.Fill(dataTable);
return dataTable;
}
/// <summary>
/// Method to get all club members
/// </summary>
/// <returns>Data table</returns>
public int GetCountOfLayers(int projectId)
{
string connectionString = this.ConnectionString;
SqlConnection sqlCon = new SqlConnection(connectionString);
sqlCon.Open();
//string commandString = "SELECT * FROM ProjectSettings";
string commandString = Scripts.SqlGetCountOfLayers;
SqlCommand sqlCmd = new SqlCommand(commandString, sqlCon);
sqlCmd.Parameters.AddWithValue("@ProjectId", projectId);
int cnt = (int)sqlCmd.ExecuteScalar();
return cnt;
}
/// <summary>
/// Method to add new member
/// </summary>
/// <param name="layer">club member model</param>
/// <returns>true or false</returns>
public bool AddLayer(LayerModel layer)
{
string connectionString = this.ConnectionString;
using (SqlConnection sqlCon = new SqlConnection(connectionString))
{
using (SqlCommand comm = new SqlCommand())
{
string cmdString = Scripts.SqlInsertLayer;
comm.Connection = sqlCon;
comm.CommandText = cmdString;
comm.Parameters.AddWithValue("@LayerName", layer.LayerName);
comm.Parameters.AddWithValue("@Order", layer.Order);
comm.Parameters.AddWithValue("@TechnologyName", layer.TechnologyName);
comm.Parameters.AddWithValue("@OpticalSchema", layer.OpticalSchema);
comm.Parameters.AddWithValue("@ProjectId", layer.ProjectId);
comm.Parameters.AddWithValue("@LayerLeft", layer.LayerLeft);
comm.Parameters.AddWithValue("@LayerTop", layer.LayerTop);
comm.Parameters.AddWithValue("@LayerWidth", layer.LayerWidth);
comm.Parameters.AddWithValue("@LayerHeight", layer.LayerHeight);
comm.Parameters.AddWithValue("@ArcWidth", layer.ArcWidth);
comm.Parameters.AddWithValue("@RadiusMax", layer.RadiusMax);
comm.Parameters.AddWithValue("@Step", layer.Step);
comm.Parameters.AddWithValue("@SourceFilePath", layer.SourceFilePath);
comm.Parameters.AddWithValue("@AnglesFilePath", layer.AnglesFilePath);
try
{
sqlCon.Open();
comm.ExecuteNonQuery();
}
catch (SqlException e)
{
// do something with the exception
// don't hide it
MessageBox.Show(
e.Message,
"Exception!",
//Resources.System_Error_Message_Title,
MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
}
}
return true;
}
/// <summary>
/// Method returns last created project Id
/// </summary>
/// <returns>Project Id</returns>
public DataRow GetLastLayer()
{
DataTable dataTable = new DataTable();
DataRow dataRow;
string connectionString = this.ConnectionString;
using (SqlConnection sqlCon = new SqlConnection(connectionString))
{
using (SqlCommand comm = new SqlCommand())
{
string cmdString = Scripts.SqlGetLastLayer;
comm.Connection = sqlCon;
comm.CommandText = cmdString;
try
{
SqlDataAdapter sd = new SqlDataAdapter();
sd.SelectCommand = comm;
sd.Fill(dataTable);
//sqlCon.Open();
//comm.ExecuteNonQuery();
}
catch (SqlException e)
{
// do something with the exception
// don't hide it
MessageBox.Show(
e.Message,
"Exception!",
//Resources.System_Error_Message_Title,
MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
// Get the datarow from the table
dataRow = dataTable.Rows.Count > 0 ? dataTable.Rows[0] : null;
}
}
return dataRow;
}
/// <summary>
/// Method to update layer
/// </summary>
/// <param name="layer">layer</param>
/// <returns>true / false</returns>
public bool UpdateLayer(LayerModel layer)
{
string connectionString = this.ConnectionString;
using (SqlConnection sqlCon = new SqlConnection(connectionString))
{
using (SqlCommand comm = new SqlCommand())
{
string cmdString = Scripts.SqlUpdateLayer;
comm.Connection = sqlCon;
comm.CommandText = cmdString;
comm.Parameters.AddWithValue("@Id", layer.Id);
comm.Parameters.AddWithValue("@Order", layer.Order);
comm.Parameters.AddWithValue("@LayerName", layer.LayerName);
comm.Parameters.AddWithValue("@TechnologyName", layer.TechnologyName);
comm.Parameters.AddWithValue("@OpticalSchema", layer.OpticalSchema);
comm.Parameters.AddWithValue("@ProjectId", layer.ProjectId);
comm.Parameters.AddWithValue("@ArcWidth", layer.ArcWidth);
comm.Parameters.AddWithValue("@LayerLeft", layer.LayerLeft);
comm.Parameters.AddWithValue("@LayerTop", layer.LayerTop);
comm.Parameters.AddWithValue("@LayerWidth", layer.LayerWidth);
comm.Parameters.AddWithValue("@LayerHeight", layer.LayerHeight);
comm.Parameters.AddWithValue("@RadiusMax", layer.RadiusMax);
comm.Parameters.AddWithValue("@Step", layer.Step);
comm.Parameters.AddWithValue("@SourceFilePath", layer.SourceFilePath);
comm.Parameters.AddWithValue("@AnglesFilePath", layer.AnglesFilePath);
try
{
sqlCon.Open();
comm.ExecuteNonQuery();
}
catch (SqlException e)
{
MessageBox.Show(
e.Message,
"Exception!",
//Resources.System_Error_Message_Title,
MessageBoxButtons.OK,
MessageBoxIcon.Error);
// do something with the exception
// don't hide it
}
}
}
return true;
}
/// <summary>
/// Method to update layer
/// </summary>
/// <param name="layer">layer</param>
/// <returns>true / false</returns>
public bool DeleteLayer(LayerModel layer)
{
string connectionString = this.ConnectionString;
using (SqlConnection sqlCon = new SqlConnection(connectionString))
{
using (SqlCommand comm = new SqlCommand())
{
string cmdString = Scripts.SqlDeleteLayer;
comm.Connection = sqlCon;
comm.CommandText = cmdString;
comm.Parameters.AddWithValue("@Id", layer.Id);
try
{
sqlCon.Open();
comm.ExecuteNonQuery();
}
catch (SqlException e)
{
MessageBox.Show(
e.Message,
"Exception!",
//Resources.System_Error_Message_Title,
MessageBoxButtons.OK,
MessageBoxIcon.Error);
// do something with the exception
// don't hide it
}
}
}
return true;
}
/// <summary>
/// Service method to get project by Id
/// </summary>
/// <param name="id">project id</param>
/// <returns>Data row</returns>
public DataRow GetColorProfileByLayerId(int layerId)
{
DataTable dataTable = new DataTable();
DataRow dataRow;
string connectionString = this.ConnectionString;
using (SqlConnection sqlCon = new SqlConnection(connectionString))
{
using (SqlCommand comm = new SqlCommand())
{
string cmdString = Scripts.SqlGetLayerColorProfiles;
comm.Connection = sqlCon;
comm.CommandText = cmdString;
comm.Parameters.AddWithValue("@LayerId", layerId);
try
{
SqlDataAdapter sd = new SqlDataAdapter();
sd.SelectCommand = comm;
sd.Fill(dataTable);
//sqlCon.Open();
//comm.ExecuteNonQuery();
}
catch (SqlException e)
{
// do something with the exception
// don't hide it
MessageBox.Show(
e.Message,
"Exception!",
//Resources.System_Error_Message_Title,
MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
// Get the datarow from the table
dataRow = dataTable.Rows.Count > 0 ? dataTable.Rows[0] : null;
}
}
return dataRow;
}
/// <summary>
/// Method to update layer
/// </summary>
/// <param name="layer">layer</param>
/// <returns>true / false</returns>
public bool DeleteColorProfile(LayerModel layer)
{
string connectionString = this.ConnectionString;
using (SqlConnection sqlCon = new SqlConnection(connectionString))
{
using (SqlCommand comm = new SqlCommand())
{
string cmdString = Scripts.SqlDeleteColorProfileByLayerId;
comm.Connection = sqlCon;
comm.CommandText = cmdString;
comm.Parameters.AddWithValue("@LayerId", layer.Id);
try
{
sqlCon.Open();
comm.ExecuteNonQuery();
}
catch (SqlException e)
{
// do something with the exception
// don't hide it
MessageBox.Show(
e.Message,
"Exception!",
//Resources.System_Error_Message_Title,
MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
}
}
return true;
}
}
}