C# 在数据库中更新/添加列
我正在尝试更新数据库中的一列。我想更新的专栏是noOfTrips。每次路线相同时,列noOfTrips必须添加1。这是我的密码。我希望有人能帮我解决这个问题。 提前谢谢C# 在数据库中更新/添加列,c#,database,C#,Database,我正在尝试更新数据库中的一列。我想更新的专栏是noOfTrips。每次路线相同时,列noOfTrips必须添加1。这是我的密码。我希望有人能帮我解决这个问题。 提前谢谢 string MyConS = "SERVER=localhost;" + "DATABASE=prototype_db;" + "UID=root;";`enter code here` MySqlConnect
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 + "'";
}
}
现在试试这个,:)
问候阿里·穆罕默德现在试试这个:) 问候
阿里·穆罕默德(Ali Muhammad)您的代码似乎没有问题,但必须执行updatecommand 使用类似 command.ExecuteNonQuery()
您的代码似乎还可以,但必须执行updatecommand 使用类似 command.ExecuteNonQuery() 根据你的评论 nOofTrips列不再更新 您的
nOofTrips
列是int
,因此不应使用单引号
试试这个:
command.CommandText = "UPDATE [tbl_bill_addtrips] SET nOofTrips= " + Trips.ToString() + " where RouteFrom='" + this.txtBillRouteFrom.Text + "'and RouteTo='" + this.txtBillRouteTo.Text + "'";
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);
注意:我建议您使用参数化查询来避免Sql注入攻击
带参数化查询:
command.CommandText = "UPDATE [tbl_bill_addtrips] SET nOofTrips= " + Trips.ToString() + " where RouteFrom='" + this.txtBillRouteFrom.Text + "'and RouteTo='" + this.txtBillRouteTo.Text + "'";
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()))
根据你的评论
nOofTrips列不再更新
您的nOofTrips
列是int
,因此不应使用单引号
试试这个:
command.CommandText = "UPDATE [tbl_bill_addtrips] SET nOofTrips= " + Trips.ToString() + " where RouteFrom='" + this.txtBillRouteFrom.Text + "'and RouteTo='" + this.txtBillRouteTo.Text + "'";
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);
注意:我建议您使用参数化查询来避免Sql注入攻击
带参数化查询:
command.CommandText = "UPDATE [tbl_bill_addtrips] SET nOofTrips= " + Trips.ToString() + " where RouteFrom='" + this.txtBillRouteFrom.Text + "'and RouteTo='" + this.txtBillRouteTo.Text + "'";
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()))
使用参数绑定-这将从sql注入式攻击中保存。使用参数绑定-这将从sql注入式攻击中保存。是的,它可以工作!但它只更新一次,不会持续更新。它在更新一次后停止。系统只需添加具有相同数据的另一行,nOofTrips列不再更新。请告诉我需要帮助。我是否理解正确:您想在每次数据库中的路由开始/停止与“txtBillRoute”中的路由开始/停止匹配时增加nOofTrips?第一次运行正常,但第二次运行失败,nOofTrips与第1次运行相同?隐马尔可夫模型。。请在if括号的末尾添加一个
conn2.Close()
,然后再次测试。舒尔不会“双开”Conna吗?您所说的“持续更新”到底是什么意思?也许你的逻辑中有n个错误,称之为比较请求。你能给我一个你第一次回复的示例代码吗?我已经糊涂了。求你了,是的,它有用!但它只更新一次,不会持续更新。它在更新一次后停止。系统只需添加具有相同数据的另一行,nOofTrips列不再更新。请告诉我需要帮助。我是否理解正确:您想在每次数据库中的路由开始/停止与“txtBillRoute”中的路由开始/停止匹配时增加nOofTrips?第一次运行正常,但第二次运行失败,nOofTrips与第1次运行相同?隐马尔可夫模型。。请在if括号的末尾添加一个conn2.Close()
,然后再次测试。舒尔不会“双开”Conna吗?您所说的“持续更新”到底是什么意思?也许你的逻辑中有n个错误,称之为比较请求。你能给我一个你第一次回复的示例代码吗?我已经糊涂了。请。您的表名可能会导致混淆,因此请将表名括在方括号内=>[tbl\u bill\u addtrips]
。查看我编辑的答案。如果SQL语法有错误,则会出现错误;请查看与MySQL服务器版本对应的手册,以了解在第行的RouteFrom='ASD'和RouteTo='ASD'附近使用“[tbl_bill_addtrips]SET noOfTrips=1”的正确语法1@GeLoCapongcol:您遇到的错误请按原样复制粘贴。您的SQL语法有错误;请查看与MySQL服务器版本对应的手册,以了解在“[tbl_bill_addtrips]附近使用的正确语法设置noOfTrips=1,其中RouteFrom='ASD'和RouteTo='ASD'位于第行1@GeLoCapongcol:您使用的是参数化查询还是非参数化查询?您的表名可能会导致混淆,因此请将表名括在方括号内=>[tbl\u bill\u addtrips]
。查看我编辑的答案。如果SQL语法有错误,则会出现错误;请查看与MySQL服务器版本对应的手册,以了解在第行的RouteFrom='ASD'和RouteTo='ASD'附近使用“[tbl_bill_addtrips]SET noOfTrips=1”的正确语法1@GeLoCapongcol:您遇到的错误请按原样复制粘贴。您的SQL语法有错误;请查看与MySQL服务器版本对应的手册,以了解在第行的RouteFrom='ASD'和RouteTo='ASD'附近使用“[tbl_bill_addtrips]SET noOfTrips=1”的正确语法1@GeLoCapongcol:您使用的是参数化查询还是非参数化查询?