很抱歉啊大家,之前上傳的代碼不是很詳細(xì),現(xiàn)在我把這一段的代碼全部放上來
private void OrderFoodAddButton_Click(object sender, EventArgs e)
{
if (IndentOderID != null)
{
using (SqlConnection conn = new SqlConnection(connectiongString))
{
conn.Open();
//↓
string sqlstring, FoodName;
int FoodId = Convert.ToInt32(comboBox1.SelectedValue);
decimal FoodPrice;
Int64 OrderId = Convert.ToInt64(IndentOderID.Text.ToString());
sqlstring = "select FoodName,FoodPrice from FoodTab where FoodId=" + FoodId + "";//根據(jù)選擇的食物搜索出食物名字,食物價(jià)格
SqlCommand cmd = new SqlCommand(sqlstring, conn);
SqlDataReader readfood = cmd.ExecuteReader();
//
readfood.Read();
FoodName = readfood[0].ToString();
FoodPrice = Convert.ToDecimal(readfood[1].ToString());
readfood.Close();
//
sqlstring="select OrderPrice from OrderTab where OrderId='"+OrderId+"'";//找出這個(gè)訂單的價(jià)格 SqlCommand cmd1 = new SqlCommand(sqlstring, conn); SqlDataReader readPrice = cmd1.ExecuteReader(); // readPrice.Read(); decimal OrderPrice=Convert.ToDecimal(readPrice[0].ToString()); readPrice.Close(); // OrderPrice = OrderPrice + FoodPrice;//將訂單價(jià)格更新 sqlstring = "select max(RelationId) as RelationIdMax from RelationTab";//找出關(guān)系表中關(guān)系ID最大項(xiàng) SqlCommand cmd2 = new SqlCommand(sqlstring, conn); SqlDataReader readrelationIdMax = cmd2.ExecuteReader(); // readrelationIdMax.Read(); int RelationIdMax = Convert.ToInt32(readrelationIdMax[0].ToString()); RelationIdMax = ++RelationIdMax;//自加確保關(guān)系ID唯一 readrelationIdMax.Close(); //; string sqlUpdataString = "insert into table_RelationTab(RelationId,FoodId,OrderId,FoodName,FoodPrice) values(" + RelationIdMax + "," + FoodId + "," + OrderId + ",'" + FoodName + "'," + FoodPrice + ")";//更新關(guān)系表 SqlCommand cmd3 = new SqlCommand(sqlUpdataString, conn); cmd3.CommandText = sqlUpdataString; sqlUpdataString = "update OrderTab set OrderPrice="+OrderPrice+" where OrderId="+OrderId+"";//更新訂單總價(jià) SqlCommand cmd4 = new SqlCommand(sqlUpdataString, conn); cmd4.CommandText = sqlUpdataString; sqlstring = "select RelationId,FoodName,FoodPrice from RelationTab where OrderId=" + OrderId + "";//找出關(guān)系表中更新后的菜譜,用于刷新GridView控件 SqlCommand cmd5 = new SqlCommand(sqlstring, conn); cmd5.CommandText = sqlstring; SqlDataAdapter adpfood = new SqlDataAdapter(sqlstring, conn); DataSet dsfood = new DataSet(); adpfood.Fill(dsfood); IndentBindingSource.DataSource = dsfood.Tables[0].DefaultView; IndentGridView.DataSource = IndentBindingSource; // IndentOrderPrice.Text = OrderPrice.ToString();//更新價(jià)格顯示 //↑ conn.Close(); } }
聯(lián)系客服