Skip to content

Read and alter Sql Server object in c#

bhola kumar

Asked May 21, 2015, 3:32 AM EDT1 answer
Share
LinkedInX
I have written one sample Sql stored procedure.
USE [Db]
VB.NET
  GO

  SET ANSI_NULLS ON
  GO
  SET QUOTED_IDENTIFIER ON
  GO
  ALTER PROCEDURE [dbo].[usp_TestProcedure]
  AS
  BEGIN

     SET NOCOUNT ON;
     PRINT 'Good evening'
 END
Its working fine. Now I want to read and alter this stored procedure (and db object like function and view) using c#. Can it be done using c# ?
I am using Server 2014 and C#4.5.

1 Answer

Know the answer? Post it. Somebody with the same question will find it here.

  • Manoj Bhoir
    0

    Why not? You can alter any type of SQL Object like Table, View, Function, Store Procedure using ALTER Command. You can Use SQLCommand Class and ExecuteNonQuery method.

    See below sample code which will show you how to alter table and add new column :

    Java
    string execSQL = "DECLARE @sup nvarchar(15); " 
    + "SET @sup = '" + suppliernotxt + "'; " 
    + "EXEC ('ALTER TABLE ProductNormalDB  ADD ' + @sup + ' nvarchar(20) NULL')" 
    SqlCommand cmd2 = new SqlCommand(execSQL, con);
    // Here con is your SQLConnection Object. 
    cmd2.ExecuteNonQuery();
    Same you can use for other objects of SQL Server.

     
    0