更新数据库字段

时间:2016-06-05 22:13:11

标签: c# c#-4.0

作为我尝试开发的应用程序的一部分,是根据服务类型更新记录。因此,Status属性的等级从1到8(进行中= 3和完成= 5)。我创建了我的代码,但它似乎无法正常工作,因为我尝试传递值并测试更新当前服务类型如下:

  

如果进展,则更新为4
  如果已完成,则更新6

class Program
{
    static void Main(string[] args)
    {
        int Bend = 4;
        int Complete = 6;

        List<int> Status = new List<int>();

        foreach (int i in Status)
        {
            if (i == 3)
            {
                SqlConnection con = new SqlConnection(@"Data Source=
              (localdb)\Projects;Initial Catalog=FLS_DB;Integrated 
               Security=True;Connect Timeout=30;Encrypt=False;");
                con.Open();
                SqlCommand cmd = new SqlCommand("Update Calls set 
                Service =@Service", con);
                cmd.CommandType = CommandType.Text;
                cmd.Parameters.AddWithValue("@Service", Bend);
                con.Open();
                int rowsAffected = cmd.ExecuteNonQuery();
                con.Close();
            }
            else if (i == 5)
            {
                SqlConnection con = new SqlConnection(@"Data Source= 
               (localdb)\Projects;Initial Catalog=FLS_DB;Integrated 
               Security=True;
               Connect Timeout=30;Encrypt=False;");
                con.Open();
                SqlCommand cmd = new SqlCommand("Update Calls set 
                   Service =@Service", con);
                cmd.CommandType = CommandType.Text;
                cmd.Parameters.AddWithValue("@Service", Complete);
                con.Open();
                int rowsAffected = cmd.ExecuteNonQuery();
                con.Close();
            }
        }
    }
}

非常感谢任何帮助!

2 个答案:

答案 0 :(得分:0)

您的代码缺少WHERE语句,只更新符合条件i == 3i == 5的记录,因此您似乎不需要循环。

您只需将服务列设置为服务列中包含值3或5的所有记录的新值Bend和Complete

using(SqlConnection con = new SqlConnection(....))
using(SqlCommand cmd = con.CreateCommand())
{
   con.Open();
   // Sets to 4 all records with 3
   cmd.CommandText = "Update Calls set Service=@Service WHERE Service=3" 
   cmd.Parameters.AddWithValue("@Service", Bend);
   int rowsUpdatedToBend = cmd.ExecuteNonQuery();

   // No need to recreate the command, just change the commandtext and
   // the value of the parameter @service
   cmd.CommandText = "Update Calls set Service=@Service WHERE Service=5" 
   cmd.Parameters["@Service"].Value = Complete
   rowsUpdatedToComplete = cmd.ExecuteNonQuery();

   MessageBox.Show("You have changed " + rowsUpdatedToBend + " rows to Bend state\r\n" + 
                   "You have changed " + rowsUpdatedToComplete + " rows to Complete state");
}

答案 1 :(得分:0)

  1. 您的连接字符串是否正确?
  2. 按条件
  3. 打开连接两次
  4. 设置断点并检查程序的运行位置
  5. 假设第一点是正确的;你可以尝试这样的事情(代码编辑):

    string commandText = "UPDATE Calls SET Service=Service + 1 WHERE Service = 3 OR Service = 5;";
    string connectionString = @"Data Source=
              (localdb)\Projects;Initial Catalog=FLS_DB;Integrated 
               Security=True;Connect Timeout=30;Encrypt=False;";
    
        using (SqlConnection connection = new SqlConnection(connectionString))
        {            
            SqlCommand command = new SqlCommand(commandText, connection);            
    
            try
            {
                connection.Open();
                int rowsAffected = command.ExecuteNonQuery();
                Console.WriteLine("RowsAffected: {0}", rowsAffected);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }