我有一个网络服务,可以根据是否可用来添加或更新房间库存。(下面的代码)。我在使用此代码时遇到的问题是,它要么对整个范围进行添加或更新,要么引发错误,例如,如果我将 2013 年 18 月 8 日至 2013 年 8 月 25 日期间的库存更改为18/8-23/8 没有库存,但 24/8-25/8 有,24/8 抛出错误,说明库存可用并且无法插入以停止整个过程。我希望我的代码能够添加或更新整个日期范围,而不仅仅是添加或修改所有内容。那可能吗?我需要为此更改我的代码吗?我怎么做?将不胜感激任何关于我哪里出错或如何更改我的代码以便能够做到这一点的见解。
[WebMethod(Description = "Add or Amend Availability & Rates")]
public bool Avail(string Username, string Password, DateTime Dte, DateTime Dtm, int ID, string RoomType, int Qty, double CurPrice)
{
GetCredentials(Username, Password);
int ID= Convert.ToInt16(GetCredentials(Username, Password).Tables[0].Rows[0]["strTypeID"]);
bool retVal = false;
GetCredentials(Username, Password);
using (SqlConnection mySQLconnection = new SqlConnection(connStr))
{
using (SqlCommand dbCommand = new SqlCommand("select * from Available where intID=@ID and dtm=@Dtm and strRoomType=@RoomType", mySQLconnection))
{
SqlParameter dt = new SqlParameter("@Dtm", Dtm);
SqlParameter RoomT = new SqlParameter("@RoomType", RoomType);
SqlParameter typeI = new SqlParameter("@ID", ID);
dbCommand.Parameters.Add(dt);
dbCommand.Parameters.Add(RoomT);
dbCommand.Parameters.Add(typeI);
mySQLconnection.Open();
using (SqlDataReader reader = dbCommand.ExecuteReader())
{
if (!reader.HasRows)
{
AddAvail(Username, Password, Dte, Dtm, RoomType, Qty, CurPrice);
retVal = false;
}
else
{
AmdAvail(Username, Password, Dte, Dtm, RoomType, Qty, CurPrice);
retVal = true;
}
mySQLconnection.Close();
dbCommand.Dispose();
mySQLconnection.Dispose();
return retVal;
}
}
}
}
/*----------------------------------------------------
* Webmethod AddAvail Adds multiple availability for speicifed date range
* ---------------------------------------------------*/
[WebMethod(Description = "Multiple Add of Availability", BufferResponse = true)]
public void AddAvail(string Username, string Password, DateTime Dte,DateTime Dtm, string RoomType, int Qty, double CurPrice)
{
GetAuthCredentials(Username, Password);
DateTime dat = Dtm;
int strTypeID = Convert.ToInt16(GetAuthCredentials(Username, Password).Tables[0].Rows[0]["strTypeID"]);
using (SqlConnection mySQLconnection = new SqlConnection(connStr))
{
mySQLconnection.Open();
for (DateTime date = Dte; date <= dat; date = date.AddDays(1.0))
{
string sqlInsertString = "INSERT INTO Available (dtm,intResortID,strRoomType,intQty,curPrice) VALUES (@dat,@strTypeID,@strRoomType,@intQty,@CurPrice)";
using (SqlCommand command = new SqlCommand())
{
command.Connection = mySQLconnection;
command.CommandText = sqlInsertString;
SqlParameter dt = new SqlParameter("@dat", date);
SqlParameter intRID = new SqlParameter("@strTypeID", strTypeID);
SqlParameter strRType = new SqlParameter("@strRoomType", RoomType);
SqlParameter intQuty = new SqlParameter("@intQty", Qty);
SqlParameter curpPrice = new SqlParameter("@curPrice", CurPrice);
command.Parameters.AddRange(new SqlParameter[] { dt, intRID, strRType, intQuty, curpPrice });
command.ExecuteNonQuery();
}
}
}
}
/*-----------------------------------------------------
* Webmethod AmdAvail Amends multiple Availability for specified date range
* -----------------------------------------------------*/
[WebMethod(Description = "Multilple Updates", BufferResponse = true)]
public DataSet AmdAvail(string Username, string Password, DateTime Dte, DateTime Dtm, string RoomType, int Qty, double CurPrice)
{
GetAuthCredentials(Username, Password);
int strTypeID = Convert.ToInt16(GetAuthCredentials(Username, Password).Tables[0].Rows[0]["strTypeID"]);
using (SqlConnection mySQLconnection = new SqlConnection(connStr))
{
mySQLconnection.Open();
using (SqlCommand dbCommand = new SqlCommand())
{
dbCommand.CommandText = "Update Available set intQty=@Qty,curprice=@CurPrice where dtm between @Dte and @Dtm and strRoomType=@Roomtype and intResortID=@strTypeID ";
dbCommand.Connection = mySQLconnection;
//Create new DataAdapter
using (SqlDataAdapter da = new SqlDataAdapter())
{
da.SelectCommand = dbCommand;
SqlParameter typeI = new SqlParameter("@strTypeID", strTypeID);
dbCommand.Parameters.Add(typeI);
dbCommand.Parameters.AddWithValue("@Dtm", Dtm);
dbCommand.Parameters.AddWithValue("@Dte", Dte);
dbCommand.Parameters.AddWithValue("@Qty", Qty);
dbCommand.Parameters.AddWithValue("@RoomType", RoomType);
dbCommand.Parameters.AddWithValue("@curprice", CurPrice);
dbCommand.Parameters.AddWithValue("@username", Username);
dbCommand.Parameters.AddWithValue("@password", Password);
DataSet ds = new DataSet();
da.Fill(ds);
return ds;
}
}
}
}