需要检查表中的某些标志是否处于活动状态

时间:2017-03-02 10:49:00

标签: c# sql asp.net web-services

需要检查表格列中的某些标志是否有效。

如果只有标志是活动的,则应执行更新语句。 (我的标志 HR_Active User_Active 如果只有那些标志是“1”更新语句应该执行)如果“0”应该通过Web服务中的段落。

表名是:HS_HR_EMPLOYEE

下面我提到了我的代码片段。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Data;
using System.Configuration;
using System.Data.SqlClient;

namespace XXX_WebService
{

    [WebService(Namespace = "http://XXXXXXX.com/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]

    public class EmployeeService : System.Web.Services.WebService
    {


        [WebMethod(Description = "Update Employee Email Address")]
        public int Update(string USRACC_USER_ID, string EMP_OFFICE_EMAIL)
        {
            string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
            using (SqlConnection con = new SqlConnection(constr))
            {
                using (SqlCommand cmd = new SqlCommand("UPDATE HS_HR_EMPLOYEE SET EMP_OFFICE_EMAIL = @EMP_OFFICE_EMAIL FROM HS_SM_USER_ACCOUNT WHERE HS_SM_USER_ACCOUNT.EMP_NUMBER = HS_HR_EMPLOYEE.EMP_NUMBER AND USRACC_USER_ID = @USRACC_USER_ID"))
                {
                    cmd.Parameters.AddWithValue("@USRACC_USER_ID", USRACC_USER_ID);
                    cmd.Parameters.AddWithValue("@EMP_OFFICE_EMAIL", EMP_OFFICE_EMAIL);
                    cmd.Connection = con;
                    con.Open();
                    int row = cmd.ExecuteNonQuery();
                    con.Close();
                 //return row;
                }

                string emp_number ="-1";
                 using (SqlCommand cmd = new SqlCommand("SELECT EMP_NUMBER FROM HS_SM_USER_ACCOUNT WHERE USRACC_USER_ID=@USRACC_USER_ID"))
                {
                    using (SqlDataAdapter sda = new SqlDataAdapter())
                    {
                    cmd.Parameters.AddWithValue("@USRACC_USER_ID", USRACC_USER_ID);
                        cmd.Connection = con;
                        sda.SelectCommand = cmd;
                        using (DataTable dt = new DataTable())
                        {
                            dt.TableName = "HS_SM_USER_ACCOUNT";
                            sda.Fill(dt);
                            if(dt.Rows.Count > 0)
                            emp_number =  dt.Rows[0][0]==DBNull.Value ? "-1" : dt.Rows[0][0].ToString();
                        }
                    }
                }

                using (SqlCommand cmd = new SqlCommand("INSERT INTO HS_EMP_EMAIL_TEMP_LOG (EMP_NUMBER,USRACC_USER_ID,EMP_OFFICE_EMAIL,LOG_DATE_TIME) VALUES(@EMP_NUMBER,@USRACC_USER_ID,@EMP_OFFICE_EMAIL,GETDATE())"))
                {
                    cmd.Parameters.AddWithValue("@EMP_NUMBER", emp_number);
                    cmd.Parameters.AddWithValue("@USRACC_USER_ID", USRACC_USER_ID);
                    cmd.Parameters.AddWithValue("@EMP_OFFICE_EMAIL", EMP_OFFICE_EMAIL);
                    cmd.Connection = con;
                    con.Open();
                    int row = cmd.ExecuteNonQuery();
                    con.Close();
                   // return row;
                }
                return 1;
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

在更新语句的where子句

中添加标记过滤器
UPDATE HS_HR_EMPLOYEE SET EMP_OFFICE_EMAIL = @EMP_OFFICE_EMAIL FROM HS_SM_USER_ACCOUNT WHERE HS_SM_USER_ACCOUNT.EMP_NUMBER = HS_HR_EMPLOYEE.EMP_NUMBER AND USRACC_USER_ID = @USRACC_USER_ID" and HS_HR_EMPLOYEE.HR_Active = 1 and User_Active = 1))

或者您可以先使用select语句检查表中的标志。