分享

C#中SQLserver语句insert语句无法更新数据

 牛人的尾巴 2016-03-09

很抱歉啊大家,之前上传的代码不是很详细,现在我把这一段的代码全部放上来
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 + "";//根据选择的食物搜索出食物名字,食物价格
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+"'";//找出这个订单的价格
                SqlCommand cmd1 = new SqlCommand(sqlstring, conn);
                SqlDataReader readPrice = cmd1.ExecuteReader();
                //
                readPrice.Read();
                decimal OrderPrice=Convert.ToDecimal(readPrice[0].ToString());
                readPrice.Close();
                //
                OrderPrice = OrderPrice + FoodPrice;//将订单价格更新
                sqlstring = "select max(RelationId) as RelationIdMax from RelationTab";//找出关系表中关系ID最大项
                SqlCommand cmd2 = new SqlCommand(sqlstring, conn);
                SqlDataReader readrelationIdMax = cmd2.ExecuteReader();
                //
                readrelationIdMax.Read();
                int RelationIdMax = Convert.ToInt32(readrelationIdMax[0].ToString());
                RelationIdMax = ++RelationIdMax;//自加确保关系ID唯一
                readrelationIdMax.Close();
                //;
                string sqlUpdataString = "insert into table_RelationTab(RelationId,FoodId,OrderId,FoodName,FoodPrice) values(" + RelationIdMax + "," + FoodId + "," + OrderId + ",'" + FoodName + "'," + FoodPrice + ")";//更新关系表
                SqlCommand cmd3 = new SqlCommand(sqlUpdataString, conn);
                cmd3.CommandText = sqlUpdataString;
                sqlUpdataString = "update OrderTab set OrderPrice="+OrderPrice+" where OrderId="+OrderId+"";//更新订单总价
                SqlCommand cmd4 = new SqlCommand(sqlUpdataString, conn);
                cmd4.CommandText = sqlUpdataString;
                sqlstring = "select RelationId,FoodName,FoodPrice from RelationTab where OrderId=" + OrderId + "";//找出关系表中更新后的菜谱,用于刷新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();//更新价格显示
                //↑
                conn.Close();
            }
        }

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约