Assuming you have a stored procedure like this one that selects either 0 (the table does not exist) or 1 (the table exists)
CREATE PROCEDURE dbo.DoesTableExist (@TableName NVARCHAR(100)) AS BEGIN IF EXISTS (SELECT * FROM sys.tables WHERE Name = @TableName) SELECT 1 ELSE SELECT 0 END
then you can write this C # code to get the value - use .ExecuteScalar() , since you expect only one row, one column:
// set up connection and command using (SqlConnection conn = new SqlConnection("your-connection-string-here")) using (SqlCommand cmd = new SqlCommand("dbo.DoesTableExist", conn)) { // define command to be stored procedure cmd.CommandType = CommandType.StoredProcedure; // add parameter cmd.Parameters.Add("@TableName", SqlDbType.NVarChar, 100).Value = "your-table-name-here"; // open connection, execute command, close connection conn.Open(); int result = (int)cmd.ExecuteScalar(); conn.Close(); }
Now result will contain either 0 if the table does not exist, or 1 if it exists.
source share