limit an SQL table to a single record and not let the c# application insert more records into that table

Asked

Viewed 45 times

0

Good Afternoon

I’m new to this c# and SQL but I made an application in c# and I have a Company table that I want to limit to a single record ( a single company) and I want the user to change or delete this record but can not enter any more. The code I made in c#

private bool IfidExists(string EmprId)
        {

            con.dataGet("Declare @EmprId INT,SELECT @EmprId= COUNT(*) FROM Empresa IF@EmprId>'1'BEGIN ROLLBACK TRANSACTION; RETURN;END ");
            DataTable dt = new DataTable();
            con.sda.Fill(dt);
            if (dt.Rows.Count > 0)
                return true;
            else
                return false;
        }
  if (Validation())

 if (IfidExists(txtEmprId.Text))
                 {
                    MessageBox.Show("Só pode Registrar uma empresa");

but won’t let me register any company

I thank you in advance for your help

  • What, in your model, defines that the company is duplicated? use field to create a unique key. And don’t write your queries that way, it’s best to use the SqlCommand

  • wouldn’t just be doing a SELECT COUNT(*) FROM Empresa? to make a simple select no need to transaction

  • Which DB ? Creates the table , inserts a record and creates a Rigger of before Insert or delete giving error in execution , a simple way.

  • Good morning Motta link string is already in the form the Company table is created, the Emprid column which is primary key and autoincrement, can you please give me an example of Trigger?

  • by "limit an SQL table to a single record" I understand that the table will only have one record is the end point , for which diacho a auto increment key ?! assuming Mysql this post does something similar

1 answer

-3

I think you’re doing a lot of things wrong... Transactions are just for writing (Insert/update) and reading is round...

You can make a generic select, and do the if of existence later, and you don’t need the declare, nor Count that is slower!

  • Good morning Carlos thank you from now on for the help but in this case what I want is for query to check if the Emprid field that is primary key and autoincrement is greater than 1 and if yes let only update and delete. thank you

Browser other questions tagged

You are not signed in. Login or sign up in order to post.