how to i search if there is a same id in a database?

后端 未结 1 770
刺人心
刺人心 2021-01-29 15:40

How do i check if there is a same id when you trying to add a data to database?

For example, if i have Name \"Leo Chan\" with id \'5\' in the list

and when i wan

相关标签:
1条回答
  • 2021-01-29 16:21
    DataTable dt = new DataTable();
    string query = string.format("SELECT * from table WHERE ID = {0}", ID);
    
    using(SqlDataAdapter sda = new SqlAdapter(query, con))
    {
         sda.fill(dt)
         if(dt.Rows.count > 0)
         {
             showErrorMessage();
             return;
         }
    }
    

    I know there are multiple ways to read from a database, but I like using SqlDataAdapters, because you really don't have to create a SqlConnection or a SqlCommand. SqlDataAdapter does it for you.

    0 讨论(0)
提交回复
热议问题