如何检查表中是否存在该名称

时间:2013-03-12 10:35:47

标签: c# sql

我是c#的新手,我不知道更多关于它但我必须做的任务。

这个想法是添加新员工,当用户点击添加按钮时,它必须检查数据库中员工表上是否存在名称,这样如果名称存在,则不会在表上添加,如果不存在则添加在桌子上 。

所以请任何人帮助我:(

这就是页面设计将如下图所示:

enter image description here

我通过存储过程进行了检查,我不知道它是否正确并且还有错误:

@F_name nvarchar(50),
@isExists bit output
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;

-- Insert statements for procedure here

 set @isExists = 0
if exists (SELECT F_name FROM Employee WHERE F_name =@F_name)
begin
       set @isExists=1
END
GO 

错误是:

  

Msg 102,Level 15,State 1,Procedure CheckRecord,Line 22   'END'附近的语法不正确。

5 个答案:

答案 0 :(得分:2)

我建议对代码隐藏使用此检查。您可以使用LINQ轻松完成:

using(DataContext db = new DataContext())
{
    var name = db.Employee.Where(n => n.F_name == YourNameFromTextBox).FistOrDefault();
    if(name == null)
    {
        // insert data
    }
    else
    {
        // record exist - throw error
    }
}

如果您不想使用LINQ,只需使用ADO.NET从DB读取/写入/更新:

SqlConnection dbConnection;
dbConnection = new SqlConnection(connectionString);

SqlCommand myCommand = new SqlCommand("SELECT * FROM Employee WHERE F_name=@F_name", dbConnection);
SqlParameter sqlParam = new SqlParameter("@F_name", F_name);
myCommand.Parameters.Add(sqlParam);

myCommand.Connection.Open();
SqlDataReader myReader = myCommand.ExecuteReader();

string name = "";

if (myReader.Read())
{
    name = myReader.GetString("F_name");
    // now you get 'name' from DB here, do your job
}
myReader.Close();
myCommand.Connection.Close();

答案 1 :(得分:0)

试试这个,最后再添加一个END语句

@F_name nvarchar(50),
@isExists bit output
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;

-- Insert statements for procedure here

 set @isExists = 0
if exists (SELECT F_name FROM Employee WHERE F_name =@F_name)
  begin
       set @isExists=1
  END

END

GO 

答案 2 :(得分:0)

您缺少一个END语句,因为每个BEGIN语句应该有END语句。

这将是:

@F_name nvarchar(50),
@isExists bit output
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;

-- Insert statements for procedure here

    set @isExists = 0
    if exists (SELECT F_name FROM Employee WHERE F_name =@F_name)
    begin
       set @isExists=1
    END
END
GO 

答案 3 :(得分:0)

您可以强制数据库不接受Employeename

字段中的重复

或使用代码

try
con.open
cmd.commandtext="select * from T1 where name = '" & varablename &"'"
reader=cmd.excutereader
if reader.read
msgbox"the emp. is alrady founded in the database"
exit sub
else
' do the insertion
end if
catch ex as exception
msgbox ex.massege
end try

答案 4 :(得分:0)

  

这是您编写存储过程的方法。我会说的很多方式之一。

CREATE PROCEDURE proc_yourProcName
    @F_name nvarchar(50) 
AS
BEGIN
    IF NOT EXISTS(SELECT F_name FROM Employee WHERE F_name = @F_name)
        BEGIN
          -- Your INSERT statement here
        END
    ELSE
        BEGIN
         -- Your UPDATE statement here
        END
END
  

因此,如果有任何F_Name,它将不会INSERT。如果需要,您可以在UPDATE声明中设置ELSE逻辑。