如果数据库中存在电话号码和国家/地区代码,如何更新相同的member_id

时间:2014-09-03 06:30:06

标签: c# asp.net-mvc

 public class RegistrationController : ApiController
    {
        public DefaultRespons GetRegister(int os_id, string device_id, int country_code, long mobile_no)
        {
            LociDataClassesDataContext dc = new LociDataClassesDataContext();
            registration reg = new registration();
            reg.os_id = os_id;
            reg.device_id = device_id;
            reg.country_code = country_code;
            reg.mobile_number = mobile_no;
            reg.verification_code = new Random().Next(1000, 9999);
            dc.registrations.InsertOnSubmit(reg);
            dc.SubmitChanges();
            Twilio.TwilioRestClient client = new Twilio.TwilioRestClient("ACcount", "token");
            Twilio.SMSMessage message = client.SendSmsMessage("+16782493911", "+" + reg.country_code + "" + reg.mobile_number, "Your verification code for Locii is: " + reg.verification_code);
            if (message.RestException != null)
                Debug.WriteLine(message.RestException.Message);
            return new DefaultRespons(1, "OK",Registration.getResponse(reg));
        }

        public DefaultRespons GetActivate(int registration_id, int verification_code)
        {
            LociDataClassesDataContext dc = new LociDataClassesDataContext();
            registration reg = dc.registrations.Where(r => r.id == registration_id && r.verification_code == verification_code && r.registration_date==null).SingleOrDefault();




            if (reg!=null)
            {
                List<registration> previous = dc.registrations.Where(r => r.mobile_number == reg.mobile_number && r.country_code == reg.country_code).ToList();
                foreach (registration r in previous)
                {
                    member mem = dc.members.Where(mb => mb.registration_id == r.id).SingleOrDefault();
                    if (mem!=null)
                        mem.online_status = -1;
                }
                member m = new member();
                m.registration_id = reg.id;
                m.online_status = 0;
                reg.registration_date = DateTime.Now;
                dc.members.InsertOnSubmit(m);
                dc.SubmitChanges();
                return new DefaultRespons(1, "Activated", Activation.getResponse(m));
            }
            else
            {
                return new DefaultRespons(1, "Failed", "");
            }
        }

这是我的代码,我正在创建新的Member_id。当我输入以下参数并且我从代码激活然后作为响应,创建新的Member_id id并返回。现在我想当我注册相同的电话号码和国家代码,其会员ID已经创建我想要返回相同的member_id它不应该更新新的会员ID请帮我如何检查数据库中已存在的电话号码和国家代码并返回相同的会员ID。请帮帮我,我无法做到如何检查。

2 个答案:

答案 0 :(得分:0)

嗨Anil试试这个示例代码: 根据您的代码进行必要的更改

public int CheckUser(int countrycode, long mobileno)
{
LociDataClassesDataContext dc = new LociDataClassesDataContext();
int id = from b in dc.registrations
         where b.country_code.Equals(countrycode) && b.mobile_number.Equals(mobileno)
         select b.registration_id;
return id;
}

    public DefaultRespons GetRegister(int os_id, string device_id, int country_code, long mobile_no)
    {
        LociDataClassesDataContext dc = new LociDataClassesDataContext();
        int reg_id = CheckUser(country_code, mobile_no);
        if (reg_id == 0)
        {
            registration reg = new registration();
            reg.os_id = os_id;
            reg.device_id = device_id;
            reg.country_code = country_code;
            reg.mobile_number = mobile_no;
            reg.verification_code = new Random().Next(1000, 9999);
            dc.registrations.InsertOnSubmit(reg);
            dc.SubmitChanges();
            Twilio.TwilioRestClient client = new Twilio.TwilioRestClient("AC3c23fee017f23f5061a6b5d3be6f74da", "6fe81560f88f3850c5ad5d4a7b8a5f50");
            Twilio.SMSMessage message = client.SendSmsMessage("+16782493911", "+" + reg.country_code + "" + reg.mobile_number, "Your verification code for Locii is: " + reg.verification_code);
            if (message.RestException != null)
                Debug.WriteLine(message.RestException.Message);
            return new DefaultRespons(1, "OK", Registration.getResponse(reg));
        }
        else
        {

            //your code what you want to do with the reg_id
        }
    }

答案 1 :(得分:0)

@Arijit - 为了获得最佳实践,请确保不要在代码示例中包含您的身份验证令牌,或者至少确保在您共享它时重置它。谢谢!