using System.Data;
using System.Data.OleDb;

namespace MyShop.MyShopDataSetTableAdapters
{
    public partial class CustomersTableAdapter
    {
        public virtual int InsertCustomerQuery(string firstName, string lastName, string mobile)
        {
            using (OleDbCommand command = new OleDbCommand())
            {
                command.Connection = this.Connection;
                command.CommandType = CommandType.Text;
                command.CommandText = "INSERT INTO [Customers] ([CustomerFirstName], [CustomerLastName], [CustomerMobile]) VALUES (?, ?, ?)";
                command.Parameters.Add("CustomerFirstName", OleDbType.VarWChar).Value = firstName;
                command.Parameters.Add("CustomerLastName", OleDbType.VarWChar).Value = lastName;
                command.Parameters.Add("CustomerMobile", OleDbType.VarWChar).Value = mobile;

                ConnectionState previousConnectionState = command.Connection.State;

                if ((command.Connection.State & ConnectionState.Open) != ConnectionState.Open)
                {
                    command.Connection.Open();
                }

                try
                {
                    return command.ExecuteNonQuery();
                }
                finally
                {
                    if (previousConnectionState == ConnectionState.Closed)
                    {
                        command.Connection.Close();
                    }
                }
            }
        }

        public virtual int UpdateCustomerQuery(string firstName, string lastName, string mobile, int customerCode)
        {
            using (OleDbCommand command = new OleDbCommand())
            {
                command.Connection = this.Connection;
                command.CommandType = CommandType.Text;
                command.CommandText = "UPDATE [Customers] SET [CustomerFirstName] = ?, [CustomerLastName] = ?, [CustomerMobile] = ? WHERE ([CustomerCode] = ?)";
                command.Parameters.Add("CustomerFirstName", OleDbType.VarWChar).Value = firstName;
                command.Parameters.Add("CustomerLastName", OleDbType.VarWChar).Value = lastName;
                command.Parameters.Add("CustomerMobile", OleDbType.VarWChar).Value = mobile;
                command.Parameters.Add("CustomerCode", OleDbType.Integer).Value = customerCode;

                ConnectionState previousConnectionState = command.Connection.State;

                if ((command.Connection.State & ConnectionState.Open) != ConnectionState.Open)
                {
                    command.Connection.Open();
                }

                try
                {
                    return command.ExecuteNonQuery();
                }
                finally
                {
                    if (previousConnectionState == ConnectionState.Closed)
                    {
                        command.Connection.Close();
                    }
                }
            }
        }

        public virtual int DeleteCustomerQuery(int customerCode)
        {
            using (OleDbCommand command = new OleDbCommand())
            {
                command.Connection = this.Connection;
                command.CommandType = CommandType.Text;
                command.CommandText = "DELETE FROM [Customers] WHERE ([CustomerCode] = ?)";
                command.Parameters.Add("CustomerCode", OleDbType.Integer).Value = customerCode;

                ConnectionState previousConnectionState = command.Connection.State;

                if ((command.Connection.State & ConnectionState.Open) != ConnectionState.Open)
                {
                    command.Connection.Open();
                }

                try
                {
                    return command.ExecuteNonQuery();
                }
                finally
                {
                    if (previousConnectionState == ConnectionState.Closed)
                    {
                        command.Connection.Close();
                    }
                }
            }
        }

        public virtual int FillByCustomerCode(MyShopDataSet.CustomersDataTable dataTable, int customerCode)
        {
            using (OleDbCommand command = new OleDbCommand())
            {
                command.Connection = this.Connection;
                command.CommandType = CommandType.Text;
                command.CommandText = "SELECT [CustomerCode], [CustomerFirstName], [CustomerLastName], [CustomerMobile] FROM [Customers] WHERE ([CustomerCode] = ?)";
                command.Parameters.Add("CustomerCode", OleDbType.Integer).Value = customerCode;

                this.Adapter.SelectCommand = command;

                if (this.ClearBeforeFill)
                {
                    dataTable.Clear();
                }

                return this.Adapter.Fill(dataTable);
            }
        }

        public virtual int FillByCustomerName(MyShopDataSet.CustomersDataTable dataTable, string customerName)
        {
            using (OleDbCommand command = new OleDbCommand())
            {
                command.Connection = this.Connection;
                command.CommandType = CommandType.Text;
                command.CommandText = "SELECT [CustomerCode], [CustomerFirstName], [CustomerLastName], [CustomerMobile] FROM [Customers] WHERE ([CustomerFirstName] LIKE ?) OR ([CustomerLastName] LIKE ?)";
                command.Parameters.Add("CustomerFirstName", OleDbType.VarWChar).Value = "%" + customerName + "%";
                command.Parameters.Add("CustomerLastName", OleDbType.VarWChar).Value = "%" + customerName + "%";

                this.Adapter.SelectCommand = command;

                if (this.ClearBeforeFill)
                {
                    dataTable.Clear();
                }

                return this.Adapter.Fill(dataTable);
            }
        }
    }

    public partial class FactorsTableAdapter
    {
        public virtual int InsertQuery(int customerCode, System.DateTime factorDate)
        {
            using (OleDbCommand command = new OleDbCommand())
            {
                command.Connection = this.Connection;
                command.CommandType = CommandType.Text;
                command.CommandText = "INSERT INTO [Factors] ([CustomerCode], [FactorDate]) VALUES (?, ?)";
                command.Parameters.Add("CustomerCode", OleDbType.Integer).Value = customerCode;
                command.Parameters.Add("FactorDate", OleDbType.Date).Value = factorDate;

                ConnectionState previousConnectionState = command.Connection.State;

                if ((command.Connection.State & ConnectionState.Open) != ConnectionState.Open)
                {
                    command.Connection.Open();
                }

                try
                {
                    command.ExecuteNonQuery();
                    command.CommandText = "SELECT @@IDENTITY";
                    command.Parameters.Clear();
                    return System.Convert.ToInt32(command.ExecuteScalar());
                }
                finally
                {
                    if (previousConnectionState == ConnectionState.Closed)
                    {
                        command.Connection.Close();
                    }
                }
            }
        }
    }

    public partial class FactorItemsTableAdapter
    {
        public virtual int InsertQuery(int factorCode, int goodsCode, int goodsAmount)
        {
            using (OleDbCommand command = new OleDbCommand())
            {
                command.Connection = this.Connection;
                command.CommandType = CommandType.Text;
                command.CommandText = "INSERT INTO [FactorItems] ([FactorCode], [GoodsCode], [GoodsAmount]) VALUES (?, ?, ?)";
                command.Parameters.Add("FactorCode", OleDbType.Integer).Value = factorCode;
                command.Parameters.Add("GoodsCode", OleDbType.Integer).Value = goodsCode;
                command.Parameters.Add("GoodsAmount", OleDbType.Integer).Value = goodsAmount;

                ConnectionState previousConnectionState = command.Connection.State;

                if ((command.Connection.State & ConnectionState.Open) != ConnectionState.Open)
                {
                    command.Connection.Open();
                }

                try
                {
                    return command.ExecuteNonQuery();
                }
                finally
                {
                    if (previousConnectionState == ConnectionState.Closed)
                    {
                        command.Connection.Close();
                    }
                }
            }
        }
    }

    public partial class GoodsTableAdapter
    {
        public virtual int InsertQuery(int goodsCode, string goodsName, int goodsUnitPrice, int goodsStock)
        {
            using (OleDbCommand command = new OleDbCommand())
            {
                command.Connection = this.Connection;
                command.CommandType = CommandType.Text;
                command.CommandText = "INSERT INTO [Goods] ([GoodsCode], [GoodsName], [GoodsUnitPrice], [GoodsStock]) VALUES (?, ?, ?, ?)";
                command.Parameters.Add("GoodsCode", OleDbType.Integer).Value = goodsCode;
                command.Parameters.Add("GoodsName", OleDbType.VarWChar).Value = goodsName;
                command.Parameters.Add("GoodsUnitPrice", OleDbType.Integer).Value = goodsUnitPrice;
                command.Parameters.Add("GoodsStock", OleDbType.Integer).Value = goodsStock;

                ConnectionState previousConnectionState = command.Connection.State;

                if ((command.Connection.State & ConnectionState.Open) != ConnectionState.Open)
                {
                    command.Connection.Open();
                }

                try
                {
                    return command.ExecuteNonQuery();
                }
                finally
                {
                    if (previousConnectionState == ConnectionState.Closed)
                    {
                        command.Connection.Close();
                    }
                }
            }
        }

        public virtual int DeleteQuery(int goodsCode)
        {
            using (OleDbCommand command = new OleDbCommand())
            {
                command.Connection = this.Connection;
                command.CommandType = CommandType.Text;
                command.CommandText = "DELETE FROM [Goods] WHERE ([GoodsCode] = ?)";
                command.Parameters.Add("GoodsCode", OleDbType.Integer).Value = goodsCode;

                ConnectionState previousConnectionState = command.Connection.State;

                if ((command.Connection.State & ConnectionState.Open) != ConnectionState.Open)
                {
                    command.Connection.Open();
                }

                try
                {
                    return command.ExecuteNonQuery();
                }
                finally
                {
                    if (previousConnectionState == ConnectionState.Closed)
                    {
                        command.Connection.Close();
                    }
                }
            }
        }

        public virtual int UpdateQuery(string goodsName, int goodsUnitPrice, int goodsStock, int goodsCode)
        {
            using (OleDbCommand command = new OleDbCommand())
            {
                command.Connection = this.Connection;
                command.CommandType = CommandType.Text;
                command.CommandText = "UPDATE [Goods] SET [GoodsName] = ?, [GoodsUnitPrice] = ?, [GoodsStock] = ? WHERE ([GoodsCode] = ?)";
                command.Parameters.Add("GoodsName", OleDbType.VarWChar).Value = goodsName;
                command.Parameters.Add("GoodsUnitPrice", OleDbType.Integer).Value = goodsUnitPrice;
                command.Parameters.Add("GoodsStock", OleDbType.Integer).Value = goodsStock;
                command.Parameters.Add("GoodsCode", OleDbType.Integer).Value = goodsCode;

                ConnectionState previousConnectionState = command.Connection.State;

                if ((command.Connection.State & ConnectionState.Open) != ConnectionState.Open)
                {
                    command.Connection.Open();
                }

                try
                {
                    return command.ExecuteNonQuery();
                }
                finally
                {
                    if (previousConnectionState == ConnectionState.Closed)
                    {
                        command.Connection.Close();
                    }
                }
            }
        }

        public virtual int FillByGoodsCode(MyShopDataSet.GoodsDataTable dataTable, int goodsCode)
        {
            using (OleDbCommand command = new OleDbCommand())
            {
                command.Connection = this.Connection;
                command.CommandType = CommandType.Text;
                command.CommandText = "SELECT [GoodsCode], [GoodsName], [GoodsUnitPrice], [GoodsStock] FROM [Goods] WHERE ([GoodsCode] = ?)";
                command.Parameters.Add("GoodsCode", OleDbType.Integer).Value = goodsCode;

                this.Adapter.SelectCommand = command;

                if (this.ClearBeforeFill)
                {
                    dataTable.Clear();
                }

                return this.Adapter.Fill(dataTable);
            }
        }

        public virtual int FillByGoodsName(MyShopDataSet.GoodsDataTable dataTable, string goodsName)
        {
            using (OleDbCommand command = new OleDbCommand())
            {
                command.Connection = this.Connection;
                command.CommandType = CommandType.Text;
                command.CommandText = "SELECT [GoodsCode], [GoodsName], [GoodsUnitPrice], [GoodsStock] FROM [Goods] WHERE ([GoodsName] LIKE ?)";
                command.Parameters.Add("GoodsName", OleDbType.VarWChar).Value = "%" + goodsName + "%";

                this.Adapter.SelectCommand = command;

                if (this.ClearBeforeFill)
                {
                    dataTable.Clear();
                }

                return this.Adapter.Fill(dataTable);
            }
        }
    }

    public partial class UsersTableAdapter
    {
        public virtual int InsertUserQuery(string userName, string password)
        {
            using (OleDbCommand command = new OleDbCommand())
            {
                command.Connection = this.Connection;
                command.CommandType = CommandType.Text;
                command.CommandText = "INSERT INTO [Users] ([Username], [Password]) VALUES (?, ?)";
                command.Parameters.Add("Username", OleDbType.VarWChar).Value = userName;
                command.Parameters.Add("Password", OleDbType.LongVarWChar).Value = password;

                ConnectionState previousConnectionState = command.Connection.State;

                if ((command.Connection.State & ConnectionState.Open) != ConnectionState.Open)
                {
                    command.Connection.Open();
                }

                try
                {
                    return command.ExecuteNonQuery();
                }
                finally
                {
                    if (previousConnectionState == ConnectionState.Closed)
                    {
                        command.Connection.Close();
                    }
                }
            }
        }

        public virtual int UpdateUserQuery(string userName, string password, int userCode)
        {
            using (OleDbCommand command = new OleDbCommand())
            {
                command.Connection = this.Connection;
                command.CommandType = CommandType.Text;
                command.CommandText = "UPDATE [Users] SET [Username] = ?, [Password] = ? WHERE ([UserCode] = ?)";
                command.Parameters.Add("Username", OleDbType.VarWChar).Value = userName;
                command.Parameters.Add("Password", OleDbType.LongVarWChar).Value = password;
                command.Parameters.Add("UserCode", OleDbType.Integer).Value = userCode;

                ConnectionState previousConnectionState = command.Connection.State;

                if ((command.Connection.State & ConnectionState.Open) != ConnectionState.Open)
                {
                    command.Connection.Open();
                }

                try
                {
                    return command.ExecuteNonQuery();
                }
                finally
                {
                    if (previousConnectionState == ConnectionState.Closed)
                    {
                        command.Connection.Close();
                    }
                }
            }
        }

        public virtual int DeleteUserQuery(int userCode)
        {
            using (OleDbCommand command = new OleDbCommand())
            {
                command.Connection = this.Connection;
                command.CommandType = CommandType.Text;
                command.CommandText = "DELETE FROM [Users] WHERE ([UserCode] = ?)";
                command.Parameters.Add("UserCode", OleDbType.Integer).Value = userCode;

                ConnectionState previousConnectionState = command.Connection.State;

                if ((command.Connection.State & ConnectionState.Open) != ConnectionState.Open)
                {
                    command.Connection.Open();
                }

                try
                {
                    return command.ExecuteNonQuery();
                }
                finally
                {
                    if (previousConnectionState == ConnectionState.Closed)
                    {
                        command.Connection.Close();
                    }
                }
            }
        }

        public virtual int FillByUserNameAndPass(MyShopDataSet.UsersDataTable dataTable, string userName, string password)
        {
            using (OleDbCommand command = new OleDbCommand())
            {
                command.Connection = this.Connection;
                command.CommandType = CommandType.Text;
                command.CommandText = "SELECT [UserCode], [Username], [Password] FROM [Users] WHERE ([Username] = ?) AND ([Password] = ?)";
                command.Parameters.Add("Username", OleDbType.VarWChar).Value = userName;
                command.Parameters.Add("Password", OleDbType.LongVarWChar).Value = password;

                this.Adapter.SelectCommand = command;

                if (this.ClearBeforeFill)
                {
                    dataTable.Clear();
                }

                return this.Adapter.Fill(dataTable);
            }
        }
    }
}
