检查数据表中的存在值 [英] check the exists values from the data table

查看:94
本文介绍了检查数据表中的存在值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在c#中使用3层架构。所有值都正确插入数据库table.my我想检查数据库中的值。如果它已经存在于数据表列中,如(id number)。所以我应该在标签上显示一条消息,表示该值已经存在请输入不同的价值





示例

i am using 3 tier architecture in c#.all values are inserting properly into the database table.my i want to check the value from the database. if it already exists inside the datatable column like (id number) . so one message should me display in the label that value is already exists please enter different value


Example

public void insertdata()
       {
           try
           {
               BLL obbll = new BLL();
               BEL ob = new BEL();
               ob.fileno = txtfileno.Text;

               ob.name = txtname.Text;
               ob.status = statuslist.SelectedItem.Text;
               ob.name = txtname.Text;
               ob.fathername = txtfathername.Text;
               ob.designation = Designationlist.SelectedItem.Text;
               ob.type = typelist.SelectedItem.Text;
               ob.gender = genderlist.SelectedItem.Text;
               ob.group = grouplist.SelectedItem.Text;
               ob.PH = phlist.SelectedItem.Text;
               ob.DOB = Convert.ToDateTime(txtdoj.Text);
               ob.DOR = Convert.ToDateTime(txtdor.Text);
               ob.DOJ = Convert.ToDateTime(txtdoj.Text);
               ob.joinas = joinaslist.SelectedItem.Text;
               ob.inscale = float.Parse(txtinscale.Text);
               ob.presentscale = float.Parse(txtpresentscale.Text);
               ob.address = txtaddress.Text;
               ob.mobile = txtmobileno.Text;
               ob.email = txtemail.Text;
               ob.qualification = txtqualification.Text;
               ob.briefhistory = txtbriefhistory.Text;
               ob.remarks = txtremarks.Text;
               ob.category = Categorylist.SelectedItem.Text;
               ob.promotedas = "";
               ob.briefhistory = txtbriefhistory.Text;
               ob.MCAPdate = Convert.ToDateTime("1/1/1999");
               ob.substantivepost = "";
               ob.mcapgrantedinscale = 00;
               ob.dateofpromotion = Convert.ToDateTime("1/1/1999");

               obbll.insertpersonaldetails(ob);
           }
           catch(Exception ex)
           {
               labfileno.Text = ex.ToString();
           }
       }

推荐答案

在我看来,处理这种要求的最佳方法是让数据库防止重复。这意味着您要为表创建主键或使用唯一约束。请参阅

- 创建主键 [ ^ ]

- 创建唯一约束 [ ^ ]



这意味着您事先不检查任何内容,只是尝试插入数据。如果密钥已经存在,您将收到异常,您应该正确处理。



如果您没有使用数据库作为后端但只使用DataTable仍然使用约束。请参阅 UniqueConstraint Class [ ^ ]
In my opinion the best way to handle this kind of requirement is to let the database prevent duplicates. This would mean that you create either a primary key for the table or a unique constraint. See
- Create Primary Keys[^]
- Create Unique Constraints[^]

This would mean that you don't check anything beforehand, just try to insert the data. If the key already exists you will receive an exception and you should handle that properly.

If you're not using a database as a backend but only a DataTable you can still use constraints. See UniqueConstraint Class[^]


public bool IsExits()

{

SqlConnection sqlConn = new SqlConnection(ConfigurationManager.ConnectionStrings [ConnectionString] .ToString());

SqlCommand sqlCmd = new SqlCommand(your stored proc,sqlConn);

sqlCmd.Parameters.AddWithValue(@ Id,id);

sqlCmd.CommandType = CommandType.StoredProcedure;

sqlConn.Open();

SqlDataAdapter sda = new SqlDataAdapter(sqlCmd);

DataTable ds = new DataTable();

sda.Fill(ds);

if(dt.rows.count> 0)

{

返回true;

}

返回f另外;

}



public void insertdata()

{

试试

{

bool flag = Isexists(你的身份证);

if(flag)

{

//已经存在

}

其他

{

BLL obbll =新BLL( );

BEL ob = new BEL();

ob.fileno = txtfileno.Text;



ob.name = txtname.Text;

ob.status = statuslist.SelectedItem.Text;

ob.name = txtname.Text;

ob.fathername = txtfathername.Text;

ob.designation = Designationlist.SelectedItem.Text;

ob.type = typelist.SelectedItem.Text;

ob.gender = genderlist.SelectedItem.Text;

ob.group = grouplist.SelectedItem.Text;

ob.PH = ph list.SelectedItem.Text;

ob.DOB = Convert.ToDateTime(txtdoj.Text);

ob.DOR = Convert.ToDateTime(txtdor.Text);

ob.DOJ = Convert.ToDateTime(txtdoj.Text);

ob.joinas = joinaslist.SelectedItem.Text;

ob.inscale = float .Parse(txtinscale.Text);

ob.presentscale = float.Parse(txtpresentscale.Text);

ob.address = txtaddress.Text;

ob.mobile = txtmobileno.Text;

ob.email = txtemail.Text;

ob.qualification = txtqualification.Text;

ob.briefhistory = txtbriefhistory.Text;

ob.remarks = txtremarks.Text;

ob.category = Categorylist.SelectedItem.Text;

ob.promotedas =;

ob.briefhistory = txtbriefhistory.Text;

ob.MCAPdate = Convert.ToDateTime(1/1/1999);

ob.substantivepost =;

ob.mcapgrantedinscale = 00;

ob.dateofpromotion = Convert.ToDateTime(1/1/1999);



obbll.insertpersonaldetails(ob);

}

catch(exception ex)

{

labfileno.Text = ex.ToString();

}

}

}
public bool IsExits()
{
SqlConnection sqlConn = new SqlConnection(ConfigurationManager.ConnectionStrings[ConnectionString].ToString());
SqlCommand sqlCmd = new SqlCommand("your stored proc", sqlConn);
sqlCmd.Parameters.AddWithValue("@Id", id);
sqlCmd.CommandType = CommandType.StoredProcedure;
sqlConn.Open();
SqlDataAdapter sda = new SqlDataAdapter(sqlCmd);
DataTable ds = new DataTable ();
sda.Fill(ds);
if(dt.rows.count>0)
{
return true;
}
return false;
}

public void insertdata()
{
try
{
bool flag=Isexists(your id);
if(flag)
{
// already exists
}
else
{
BLL obbll = new BLL();
BEL ob = new BEL();
ob.fileno = txtfileno.Text;

ob.name = txtname.Text;
ob.status = statuslist.SelectedItem.Text;
ob.name = txtname.Text;
ob.fathername = txtfathername.Text;
ob.designation = Designationlist.SelectedItem.Text;
ob.type = typelist.SelectedItem.Text;
ob.gender = genderlist.SelectedItem.Text;
ob.group = grouplist.SelectedItem.Text;
ob.PH = phlist.SelectedItem.Text;
ob.DOB = Convert.ToDateTime(txtdoj.Text);
ob.DOR = Convert.ToDateTime(txtdor.Text);
ob.DOJ = Convert.ToDateTime(txtdoj.Text);
ob.joinas = joinaslist.SelectedItem.Text;
ob.inscale = float.Parse(txtinscale.Text);
ob.presentscale = float.Parse(txtpresentscale.Text);
ob.address = txtaddress.Text;
ob.mobile = txtmobileno.Text;
ob.email = txtemail.Text;
ob.qualification = txtqualification.Text;
ob.briefhistory = txtbriefhistory.Text;
ob.remarks = txtremarks.Text;
ob.category = Categorylist.SelectedItem.Text;
ob.promotedas = "";
ob.briefhistory = txtbriefhistory.Text;
ob.MCAPdate = Convert.ToDateTime("1/1/1999");
ob.substantivepost = "";
ob.mcapgrantedinscale = 00;
ob.dateofpromotion = Convert.ToDateTime("1/1/1999");

obbll.insertpersonaldetails(ob);
}
catch(Exception ex)
{
labfileno.Text = ex.ToString();
}
}
}


这篇关于检查数据表中的存在值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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