我正在使用数据表来存储员工详细信息。我需要阻止重复输入。
代码背后:
try
{
strServerSelectedName = txtServerName.Text;
string dCurrentDateTIme = Convert.ToString(DateTime.Now.ToLocalTime());
DataTable ds1 = new DataTable();
DataRow dr1 = ds1.NewRow();
iServerSelectedid = Convert.ToInt32(Session["SelectedServerId"].ToString());
ds1 = datalayer.getServiceDetails(iServerSelectedid);
ds1.Columns["servicename"].Unique = true;
string strServiceName = txtServiceName.Text;
string strDescription = txtareaDescription.Text;
string UserName = usernameshown.Text;
datalayer.insertsingleservice(iServerSelectedid, UserName, dCurrentDateTIme, strServiceName, strDescription);
ds1 = datalayer.getServiceDetails(iServerSelectedid);
grdServerDetails.DataSource = ds1;
grdServerDetails.DataBind();
}
catch(Exception ex)
{
Alert.Show(ex.Message);
}
我使用唯一键来防止代码中的重复值,但我不会帮助我恢复问题。在此先感谢
答案 0 :(得分:1)
您可以在数据表中设置PrimaryKey或constraints
答案 1 :(得分:0)
如果你想要一个独特的约束,你需要声明它:
UniqueConstraint cnst =
new UniqueConstraint(new DataColumn[] {ds1.Columns["servicename"]});
ds1.Constraints.Add(cnst);