-2

Is there a way to search a database so that if a user puts in an input such as a Username, then it would go through the database and look for the same exact name of a table, in C#?

Example: if A input was Test, then it would search the database for a table that has the name Test.

Ibrennan208
  • 1,345
  • 3
  • 14
  • 31
  • Are you asking how to [Get all table names of a particular database](https://stackoverflow.com/questions/3913620/get-all-table-names-of-a-particular-database-by-sql-query)? – Ibrennan208 Oct 05 '22 at 01:06
  • Or perhaps you're asking [How to directly execute sql query in c#](https://stackoverflow.com/questions/21709305/how-to-directly-execute-sql-query-in-c)? – Ibrennan208 Oct 05 '22 at 01:08
  • I am asking how to search threw a database for a table with the same exact name as the user input. for example User input = test, and a table in a database = test – Danielplayz21 Oct 05 '22 at 06:25
  • You should read through the links I provided. They will get you exactly that. It may require some reading and piecing together, but that's expected because you are actually asking about how to do multiple things. – Ibrennan208 Oct 05 '22 at 17:05

1 Answers1

0

If your database is sql server, your tables are all in sys.tables, so this will do it...

select * from sys.tables T where T.name = @searchTerm

Search your conscience, though. There is generally no good reason to have a query like this in your app code. Your code is where you commit yourself: a named table, a selection of columns.

bbsimonbb
  • 27,056
  • 15
  • 80
  • 110
  • Very similar to the suggested [Get all table names of a particular database](https://stackoverflow.com/questions/3913620/get-all-table-names-of-a-particular-database-by-sql-query). The top answer there with a simple `WHERE name = @tableName` would do it. This is basically a duplicate question. – Ibrennan208 Oct 05 '22 at 17:03