如何使用linq to sql验证将PK插入数据库 [英] How do I validate Inserting PK to database using linq to sql

查看:84
本文介绍了如何使用linq to sql验证将PK插入数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须使用文本框在sql数据库中插入值。 ID字段是主键。现在,如果用户输入重复的ID值,则会有一个消息框显示错误警报,如果该值不重复,则会将数据插入数据库。问题是当Id已经在数据库中时它显示错误,但是当输入新ID时,数据不会存储到数据库中。请帮忙!

守则如下.....

I have to insert values in a sql database using textbox. The ID field is the primary key. Now If the user inputs duplicate ID value there will be a messagebox displaying error alert and if the value is not duplicate then it will insert data to database. The problem is when the Id is already in database it is showing the error but when the new ID is entered the data is not stored to database. Kindly help!
The Code is as follows.....

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace CustomerMaster
{
    public partial class CustomerMaster : System.Web.UI.Page
    {

        private void AddData()
        {
            
            OperationsDataContext AddDb = new OperationsDataContext();
            CustomerMast cm = new CustomerMast();
            
                        
            cm.CustomerCode = txtCustCde.Text;
            cm.CustomerName = txtCustName.Text;
            cm.CustomerAddress = txtCustAdd.Text;
            cm.BrkrCode = txtBrkCode.Text;
            cm.BrokerName = txtBrkName.Text.
            cm.CrrCode = txtCrrCode.CarrierCode;
            cm.CarrierName =  txtCrrName.Text;
	cm.StnCode=sm.StationCode;
            cm.StationName = txtStnName.Text;
            cm.VatNo=txtVatNo.Text;
            cm.TinNo=txtTinNo.Text;
            cm.TMCO=txtTMCONo.Text;
            cm.PanNo = txtPAN.Text;

var chk = from c in AddDb.CustomerMasts where c.CustomerCode == txtCustCde.Text select c;
            foreach (CustomerMast c in chk)
            {
                if (c.CustomerCode == txtCustCde.Text)
                {
                    Response.Write("Error! CustomerCode Exits");

                }
                else if(c.CustomerCode != txtCustCde.Text)
                {
                    AddDb.BrokerMasts.InsertOnSubmit(bm);
                    AddDb.CarrierMasts.InsertOnSubmit(crm);
                    AddDb.StationMasts.InsertOnSubmit(sm);
                    AddDb.CustomerMasts.InsertOnSubmit(cm);

                    AddDb.SubmitChanges();
                    
                }
            }
           
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
          AddData();
            
    }
    }
}

推荐答案

你的测试是多余的,因为你已经查询仅匹配记录。在这种情况下,如果没有匹配就没有行,因此你永远不会在循环中点击else子句。



相反,它足以检查如果有任何匹配的记录。下面的示例显示了如何(注意您可以将条件直接放入Any()而不是指定Where())。



Your test is redundant, since you are already querying only for matching records. In this case, you will get no rows if there is no match, therefore you will never hit the else clause in the loop.

Instead, it is enough to check if there are any matching records at all. The example below shows how (note that you can put a condition straight into Any() instead of specifying Where()).

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
 
namespace CustomerMaster
{
    public partial class CustomerMaster : System.Web.UI.Page
    {
 
        private void AddData()
        {
            
            OperationsDataContext AddDb = new OperationsDataContext();
            CustomerMast cm = new CustomerMast();
            
                        
            cm.CustomerCode = txtCustCde.Text;
            cm.CustomerName = txtCustName.Text;
            cm.CustomerAddress = txtCustAdd.Text;
            cm.BrkrCode = txtBrkCode.Text;
            cm.BrokerName = txtBrkName.Text.
            cm.CrrCode = txtCrrCode.CarrierCode;
            cm.CarrierName =  txtCrrName.Text;
            cm.StnCode=sm.StationCode;
            cm.StationName = txtStnName.Text;
            cm.VatNo=txtVatNo.Text;
            cm.TinNo=txtTinNo.Text;
            cm.TMCO=txtTMCONo.Text;
            cm.PanNo = txtPAN.Text;
			
	    if(AddDb.CustomerMasts.Any(c=>c.CustomerCode == txtCustCde.Text))
            {
	        Response.Write("Error! CustomerCode Exits");
	    }
	    else
	    {
                AddDb.BrokerMasts.InsertOnSubmit(bm);
                AddDb.CarrierMasts.InsertOnSubmit(crm);
                AddDb.StationMasts.InsertOnSubmit(sm);
                AddDb.CustomerMasts.InsertOnSubmit(cm);

	        AddDb.SubmitChanges();
            }         
        }
 
        protected void btnAdd_Click(object sender, EventArgs e)
        {
            AddData();
        }
    }
}


这篇关于如何使用linq to sql验证将PK插入数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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