1

How do I create a view dynamically in SQL Server using C#?

2 Answers 2

5

Something like this, obviously your connection code will be different (better):

SqlConnection conn = null;
conn = new SqlConnection("yourConnectionString");
conn.Open();
string strSQLCommand = "CREATE VIEW vw_YourView AS SELECT YOurColumn FROM YourTable";
SqlCommand command = new SqlCommand(strSQLCommand, conn); 
string returnvalue = (string)command.ExecuteScalar(); 
conn.Close();
Sign up to request clarification or add additional context in comments.

Comments

3

You can use the following code to write the query for a view:

query = " Create View [Viewname] Select ....";

Execute the query.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.