更新/添加数据库中的列

时间:2013-12-06 11:52:07

标签: c# database

我正在尝试更新数据库中的列。我要更新的列是noOfTrips。每次路由相同时,noOfTrips列必须添加1.这是我的代码。我希望有人可以帮我解决这个问题。  提前致谢。

              string MyConS = "SERVER=localhost;" +
             "DATABASE=prototype_db;" +
             "UID=root;";`enter code here`


                MySqlConnection conn = new MySqlConnection(MyConS);
                MySqlCommand comm = conn.CreateCommand();
                conn.Open();
                comm.CommandText = "Select RouteTo,RouteFrom,nOofTrips from tbl_bill_addtrips where RouteFrom = '" + txtBillRouteFrom.Text + "' and RouteTo ='" + txtBillRouteTo.Text + "'";
                MySqlDataReader Reader;
                Reader = comm.ExecuteReader();
                while (Reader.Read())
                {
                    int Trips = 1;
                    string to = Reader["RouteTo"].ToString();
                    string From = Reader["RouteFrom"].ToString();
                    Trips = Convert.ToInt32(Reader["nOofTrips"].ToString());


                    if (From.ToString() == txtBillRouteFrom.Text && to.ToString() == txtBillRouteTo.Text)
                    {
                        Trips++;
                        MySqlConnection conn2 = new MySqlConnection(MyConS);
                        conn2.Open();
                        MySqlCommand command = conn2.CreateCommand();
                        command.CommandText = "UPDATE tbl_bill_addtrips SET nOofTrips= '" + Trips.ToString() + "' where  RouteFrom='" + this.txtBillRouteFrom.Text + "'and RouteTo='" + this.txtBillRouteTo.Text + "'";


                    }
                }

3 个答案:

答案 0 :(得分:3)

您的代码似乎一切正常,但您必须执行updatecommand。

使用类似

的内容
  

command.ExecuteNonQuery();

答案 1 :(得分:1)

来自您的评论

  

nOofTrips列不再更新

您的nOofTrips列为int,因此您不应该提供单引号。

试试这个:

command.CommandText = "UPDATE [tbl_bill_addtrips] SET nOofTrips= " + Trips.ToString() + " where  RouteFrom='" + this.txtBillRouteFrom.Text + "'and RouteTo='" + this.txtBillRouteTo.Text + "'";

注意:我建议您使用参数化查询来避免Sql注入攻击。

使用参数化查询:

command.CommandText = "UPDATE [tbl_bill_addtrips] SET nOofTrips=@nooftrips where  RouteFrom=@routefrom and RouteTo=@routeto";

command.Parameters.AddWithValue("@nooftrips",Trips);
command.Parameters.AddWithValue("@routefrom",this.txtBillRouteFrom.Text);
command.Parameters.AddWithValue("@routeto",this.txtBillRouteTo.Text);

解决方案2:试试这个:

command.CommandText = "UPDATE tbl_bill_addtrips SET nOofTrips= " + Trips.ToString() + " where  RouteFrom='" + this.txtBillRouteFrom.Text.Trim() + "' and RouteTo='" + this.txtBillRouteTo.Text.Trim() + "'";

解决方案3:

替换它:

if (From.ToString() == txtBillRouteFrom.Text && to.ToString() == txtBillRouteTo.Text)

有了这个:

if (From.ToString().Equals(txtBillRouteFrom.Text.Trim()) && to.ToString().Equals(txtBillRouteTo.TextTrim()))

答案 2 :(得分:0)

现在试试这个,:)

    string MyConS = "SERVER=localhost;" +
                 "DATABASE=prototype_db;" +
                 "UID=root;";`enter code here`


                    MySqlConnection conn = new MySqlConnection(MyConS);
                    MySqlCommand comm = conn.CreateCommand();
                    conn.Open();
                    comm.CommandText = "Select RouteTo,RouteFrom,nOofTrips from tbl_bill_addtrips where RouteFrom = '" + txtBillRouteFrom.Text + "' and RouteTo ='" + txtBillRouteTo.Text + "'";
                    MySqlDataReader Reader;
                    Reader = comm.ExecuteReader();
int Trips = 1;                    
while (Reader.Read())
                    {

                        string to = Reader["RouteTo"].ToString();
                        string From = Reader["RouteFrom"].ToString();
                        Trips = Convert.ToInt32(Reader["nOofTrips"].ToString());


                        if (From.ToString() == txtBillRouteFrom.Text && to.ToString() == txtBillRouteTo.Text)
                        {
                            Trips++;
                            MySqlConnection conn2 = new MySqlConnection(MyConS);
                            conn2.Open();
                            MySqlCommand command = conn2.CreateCommand();
                            command.CommandText = "UPDATE tbl_bill_addtrips SET nOofTrips= '" + Trips.ToString() + "' where  RouteFrom='" + this.txtBillRouteFrom.Text + "'and RouteTo='" + this.txtBillRouteTo.Text + "'";


                        }
                    }

<强>问候
阿里穆罕默德