运行更新查询但在C#中没有更新 [英] run update query but no update in C#

查看:74
本文介绍了运行更新查询但在C#中没有更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的数据库更新代码

当我运行这个没有错误但数据库没有更新仍然相同的价格





  string  c_time = DateTime.Now.ToString(  HH:mm:ss tt); 
string c_date = DateTime.Today.ToString( DD-MM-YYYY);

使用(SqlConnection conn = new SqlConnection( @ Data Source = MCK-PC\MCKSQL; Initial Catalog = my_db; Integrated Security = True))
{
SqlCommand newCmd = new SqlCommand( @ UPDATE emp SET last_act_date = @ last_act_date,last_act_time = @ last_act_time WHERE Card_no = @ Card_no;,conn);

newCmd.Parameters.AddWithValue( @ last_act_date,c_date);
newCmd.Parameters.AddWithValue( @ last_act_time,c_time);
newCmd.Parameters.AddWithValue( @ Card_no,card_no_input.ToString());

ConnectionState state = conn.State;
if (state == ConnectionState.Closed)
{
newCmd.Connection.Open();
newCmd.ExecuteNonQuery();
newCmd.Connection.Close();
}

else
{
newCmd.ExecuteNonQuery();
newCmd.Connection.Close();
}

}





感谢提前!

解决方案

检查 card_no_input - 这听起来像是某种输入,所以有一个商品机会你想要它的Text属性而不是调用ToString 。



如果它是一个TextBox,你的查询将尝试更新卡号为System.Forms.TextBox或类似的所有记录

This is my database update code
when i run this no errors but database not updated still same vales


string  c_time= DateTime.Now.ToString("HH:mm:ss tt");
string  c_date= DateTime.Today.ToString("dd-MM-yyyy");

using (SqlConnection conn = new SqlConnection(@"Data Source=MCK-PC\MCKSQL;Initial Catalog=my_db;Integrated Security=True"))
 {
   SqlCommand newCmd = new SqlCommand(@"UPDATE emp SET last_act_date=@last_act_date , last_act_time=@last_act_time WHERE Card_no=@Card_no;", conn);

   newCmd.Parameters.AddWithValue("@last_act_date", c_date);
   newCmd.Parameters.AddWithValue("@last_act_time", c_time);
   newCmd.Parameters.AddWithValue("@Card_no", card_no_input.ToString());

   ConnectionState state = conn.State;
   if (state == ConnectionState.Closed)
     {
       newCmd.Connection.Open();
       newCmd.ExecuteNonQuery();
       newCmd.Connection.Close();
     }

   else
     {
       newCmd.ExecuteNonQuery();
       newCmd.Connection.Close();
     }

 }



THANK IN ADVANCE!

解决方案

Check card_no_input - it sounds like it's an input of some kind, so there is a goods chance that you want it's Text property rather than calling ToString.

If it is a TextBox for example, your query will be trying t to update all records where the card number is "System.Forms.TextBox" or similar


这篇关于运行更新查询但在C#中没有更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆