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

string sqlUpdataString = "insert into RelationTab(RelationId,FoodId,OrderId,FoodName,FoodPrice) values(" + RelationIdMax + "," + FoodId + "," + OrderId + ",'" + FoodName + "'," + FoodPrice + ")";

如上,这行代码并没有把数据插入到相应的表中,RelationTab是表名,相应列名也都核对过,传入数据也都进行的数据类型的转换。

在上述代码之后重新声明了新的SqlCommand对象:

SqlCommand cmd3 = new SqlCommand(sqlUpdataString, conn);
cmd3.CommandText = sqlUpdataString;

找了很久也没发现什么问题,是不是我sql语句的语法出现了错误?因为我随便删了里面几个字母,都没什么反应和区别。C#连数据库操作真的很不好用,数据库语句出了什么问题也没法比较直观的反应出来。

把你最终的sql打印出来,看看,或者放到sqlserver中执行一下,看看有没有错误

执行命令的方法
1: ExecuteNonQuery:不返回结果(非查询)
2: ExecuteReader:(0行或多行)
3: ExecuteXmlReader:(XML)

例子:
SqlConnection conn = null;
try
{
conn = new SqlConnection(@"
server=./sqlexpress; integrated security=true; database=northwind");

    //SqlCommand cmd = new SqlCommand();
    //cmd.Connection = conn;
    //cmd.CommandText = @"select count(*) from employees";
    SqlCommand cmd = new SqlCommand(@"select count(*) from employees", conn);

    conn.Open();

    Console.WriteLine("{0}", cmd.ExecuteScalar());
}
catch (SqlException ex)
{
    Console.WriteLine(ex.ToString());
}
finally
{
    if (conn != null)
    {
        conn.Close();
    }
}

看看你conn打开了没,sql执行了没?

有一种技术叫做debug

你就有个cmd,然后执行了什么指令不知,然后有啥上下文不知,有没有try catch不知,最主要的,你的sql是insert,你的参数名是update……

好像没有看到执行命令语句,百度一下一大堆

很抱歉啊大家,之前上传的代码不是很详细,现在我把这一段的代码全部放上来
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();
            }
        }
    }