Click here to Skip to main content
15,896,359 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have folder which has stored procedures . I want to create one c# form application where "add" button, "database name" , "server name" and browse for " stored procedures file location" are the inputs. The task i want to do is when i fill "dbname" , "servername" and browse folder of stored procedures. and click "add" button , all stored procedures from folder gets add to stored procedure folder of database.
Is this possible?

if yes ,Suggest me ways to achieve this task.
thanks !

edited :
code below

C#
SqlConnection con = new SqlConnection("myconstring");
              string filePath = txtsend.Text +"\\";
          string[] files = Directory.GetFiles((filePath));

          foreach (string path in files)
          {
              string proc = File.ReadAllText(path);

              SqlCommand cmd = new SqlCommand(proc,con);

               con.Open();
               cmd.ExecuteNonQuery();
               MessageBox.Show("procedures added");
               con.Close();
           }
         }
Posted
Updated 30-Oct-15 21:39pm
v2

Yes, Its possible and simple.
Just read the all files into the folder one by one and execute the file string into database.
C#
string[] filePaths = Directory.GetFiles(Server.MapPath("your Procedure folder path"));
foreach (string filePath in filePaths)
{
     string proc= File.ReadAllText(filePath);
     //Connect DataBase
     //Pass the "proc" string on executenonquery() methode.
}
 
Share this answer
 
Comments
Member 11543226 30-Oct-15 7:15am    
it works for some procedures but shows error for some "incorrect syntax near GO" , but all procedures does fine job.
Dave Kreskowiak 30-Oct-15 9:22am    
You cannot use GO in the scripts if you execute the from your code.

You have to remove those lines from the scripts before you execute them.
Member 11543226 31-Oct-15 2:56am    
I remove go and all now program run succsessfully but procedures not added to database
Yes, It's possible.

As per your comment it's working for few procedures and for few it's throwing some error?

Then which are the procedures you got error, those procedures contains some syntactical errors. Please resolve those first and then execute your application.
 
Share this answer
 
Comments
Member 11543226 31-Oct-15 3:34am    
No errors in procedures when i execute it from managment studio it executes without errors but from program it not added to database->programmability->stored procedure

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900