本文介绍了在ado中重新索引列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

朋友们!我正在使用SQL数据库中具有ID,FName和LName列的表.在我的应用程序中,我将此表提取到数据集.现在,我需要一个列,该列应用于使全名在表中唯一.
因此,我要再添加一个称为全名的列,该列未在数据库中显示.它只会出现在数据集上,因此用户不能插入两个具有相同名字和姓氏的人.如果我将此列用作基于表达式的列,则无法向该列添加唯一约束.否则,当我从数据库中获取数据时,行在数据集中保持为空.我应该怎么办?

Hi friends ! I am using a table in sql databse having columns ID, FName and LName. In my application I fetch this table to a dataset. Now I need a column which should be used to make the full name to be unique in a table.
So i am adding one more column called full name which is not presented in database. It will be only on dataset so that the user can''t insert 2 persons with the same First and Last Name. If i used this column as expression based then I can''t add Unique constraint to this column. otherwise the rows remains null in the dataset when i fetch the data from the database. What should I do? Is there any way to do in the dataset to make the full name of a person to be unique in a dataset?

推荐答案


DataSet ds = GetDataFromDatabase();
DataColumn col = ds.Tables["TableName"].Columns.add("fullName", (typeof) string);


foreach(DataRow row in ds.Rows)
{
	row["fullName"] = row["FName"].ToString()+row["LName"].ToString();
}
col.Unique = true;
return ds;

//On Insert or Update of application
try
{
	DataRow row = ds.Tables["TableName"].NewRow();
	row.BeginEdit();
	row["FName"] = tbFName.Text;
	row["LName"] = tbLName.Text;
	row["fullName"] = tbFName.Text + tbLName.Text;
	row.EndEdit();
 	ds.Tables["TableName"].Rows.Add(row);
}
catch(Exception ex)
{
	if(ex.GetType() == typeof(ConstraintException))
	{
		MessageBox.Show("A person is already exist with the given First Name and Last Name");
	}
}



这篇关于在ado中重新索引列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-28 23:25
查看更多