Имена столбцов в каждой таблице должны быть уникальными.Имя столбца «Доставка» в таблице «Продажи» указывается более одного раза.
Как я могу решить эту проблему?Я получаю System.Data.SqlClient.SqlException
.
class DataAccess
{
string ConnectionString;
public DataAccess()
{
ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["CafeteriaDBConnectionString"].ConnectionString;
}
public bool RecordASale(ArrayList ProductsList, DateTime SaleTime, int SalesmanID, decimal CashGiven, decimal TotalBill, decimal CashReturn, string Delivery)
{
int SaleID = ReturnSaleID();
using (SqlConnection connection = new SqlConnection(ConnectionString))
{
connection.Open();
using (SqlCommand command = new SqlCommand("alter table [Sales] add [Delivery] varchar default 5 NOT NULL"))
{
command.Connection = connection;
command.ExecuteNonQuery();
}
}
using (SqlConnection connection = new SqlConnection(ConnectionString))
{
connection.Open();
/*Start a local transaction*/
SqlTransaction sqlTran = connection.BeginTransaction();
/*Enlist a command in the current transaction*/
SqlCommand command = connection.CreateCommand();
command.Transaction = sqlTran;
try
{
// Execute separate commands.
command.Parameters.AddWithValue("@SaleTime", SaleTime);
command.Parameters.AddWithValue("@SalesmanID", SalesmanID);
command.Parameters.AddWithValue("@CashGiven", CashGiven);
command.Parameters.AddWithValue("@TotalBill", TotalBill);
command.Parameters.AddWithValue("@CashReturn", CashReturn);
/*command.Parameters.AddWithValue("@Delivery", Delivery);*/
command.CommandText =
"Insert into Sales (SaleTime, SalesmanID, CashGiven, TotalBill, CashReturn, Delivery) values (@SaleTime, @SalesmanID, @CashGiven, @TotalBill, @CashReturn, @Delivery)";
command.ExecuteNonQuery();
foreach (Details ProductDetail in ProductsList)
{
//// Execute separate commands.
//command.Parameters.AddWithValue("@ProductName", ProductDetail.Name);
//command.Parameters.AddWithValue("@ProductPrice", ProductDetail.Price);
//command.Parameters.AddWithValue("@ProductQuantity", ProductDetail.Quantity);
//command.Parameters.AddWithValue("@ProductTotal", ProductDetail.Total);
//command.Parameters.AddWithValue("@SaleID", SaleID);
command.CommandText =
"Insert into SaleItems (ProductName, ProductPrice, ProductQuantity, ProductTotal, SaleID) values ('" + ProductDetail.Name + "', '" + ProductDetail.Price + "', '" + ProductDetail.Quantity + "', '" + ProductDetail.Total + "', '" + SaleID + "')";
command.ExecuteNonQuery();
}
// Commit the transaction.
sqlTran.Commit();
//connection.Close();
return true;
}
catch (Exception ee)
{
throw ee;
}
}
}