How to create stored procedure through c# at runtime?
-
Hello, How to create stored procedure dynamically in c#.net. Because some of my requirements needs such a situation. Please help with some example code. Thanks and Regards
Just execute the statements as you did for normal query. But why you need to create a SP run time? That doesn't make any sense.
Navaneeth How to use google | Ask smart questions
-
Just execute the statements as you did for normal query. But why you need to create a SP run time? That doesn't make any sense.
Navaneeth How to use google | Ask smart questions
-
Just execute the statements as you did for normal query. But why you need to create a SP run time? That doesn't make any sense.
Navaneeth How to use google | Ask smart questions
N a v a n e e t h wrote:
But why you need to create a SP run time? That doesn't make any sense.
He might be coding and OR mapper :)
Giorgi Dalakishvili #region signature My Articles Asynchronous Registry Notification Using Strongly-typed WMI Classes in .NET [^] My blog #endregion
-
N a v a n e e t h wrote:
But why you need to create a SP run time? That doesn't make any sense.
He might be coding and OR mapper :)
Giorgi Dalakishvili #region signature My Articles Asynchronous Registry Notification Using Strongly-typed WMI Classes in .NET [^] My blog #endregion
-
Hi Navaneeth, Because of requirement, I need to create tables and stored procedures at runtime. This is created at runtime because it is managed on clients system or database server during installation of application. Thanks for reply.
Well, Try the following code
string query = @"CREATE PROCEDURE SampleProcedure
AS
BEGIN
SELECT 'From Sample Procedure'
END";
using (SqlConnection connection = new SqlConnection(@"YourConnectionString"))
using (SqlCommand command = new SqlCommand(query,connection))
{
connection.Open();
command.ExecuteNonQuery();
}If you have many SQL statements to be executed, I'd put it in a file and read from there to execute.
Navaneeth How to use google | Ask smart questions
-
Well, Try the following code
string query = @"CREATE PROCEDURE SampleProcedure
AS
BEGIN
SELECT 'From Sample Procedure'
END";
using (SqlConnection connection = new SqlConnection(@"YourConnectionString"))
using (SqlCommand command = new SqlCommand(query,connection))
{
connection.Open();
command.ExecuteNonQuery();
}If you have many SQL statements to be executed, I'd put it in a file and read from there to execute.
Navaneeth How to use google | Ask smart questions
Could you please explain? What is the problem in this create procedure QueryString. "CREATE PROCEDURE test.dbo.Operator @MasterField varchar(300) AS SELECT SubOperatorField FROM SubOperatorInfo WHERE MasterField=@MasterField AND departField IS NOT NULL AND SubOperatorField=somevalue"; this querystring fires an exception i.e.:- 'CREATE PROCEDURE' does not allow specifying the database name as a prefix to the object name. Must declare the variable '@MasterField'.
-
Could you please explain? What is the problem in this create procedure QueryString. "CREATE PROCEDURE test.dbo.Operator @MasterField varchar(300) AS SELECT SubOperatorField FROM SubOperatorInfo WHERE MasterField=@MasterField AND departField IS NOT NULL AND SubOperatorField=somevalue"; this querystring fires an exception i.e.:- 'CREATE PROCEDURE' does not allow specifying the database name as a prefix to the object name. Must declare the variable '@MasterField'.
try taking the database name out of the name of the procedure You should be already in the context of the database, e.g the SqlConnection is already connected to "test". James
James Simpson Web Developer imebgo@hotmail.com P S - This is what part of the alphabet would look like if Q and R were eliminated
Mitch Hedberg -
try taking the database name out of the name of the procedure You should be already in the context of the database, e.g the SqlConnection is already connected to "test". James
James Simpson Web Developer imebgo@hotmail.com P S - This is what part of the alphabet would look like if Q and R were eliminated
Mitch Hedberg