Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. run SQL server scripts from inside a C# code

run SQL server scripts from inside a C# code

Scheduled Pinned Locked Moved C#
csharpdatabasesql-serversysadminhelp
13 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Sabry1905
    wrote on last edited by
    #1

    Hello All i am wondering how to call a batch of SQL server scripts from inside a C# code Thanx for your help

    V E G H 4 Replies Last reply
    0
    • S Sabry1905

      Hello All i am wondering how to call a batch of SQL server scripts from inside a C# code Thanx for your help

      V Offline
      V Offline
      Vimalsoft Pty Ltd
      wrote on last edited by
      #2

      Its happenig in these Articles http://www.codeproject.com/script/Articles/MemberArticles.aspx?amid=3942021[^]

      Vuyiswa Maseko, Sorrow is Better than Laughter, it may Sadden your Face, but It sharpens your Understanding VB.NET/SQL7/2000/2005 http://vuyiswamb.007ihost.com http://Ecadre.007ihost.com vuyiswam@tshwane.gov.za

      1 Reply Last reply
      0
      • S Sabry1905

        Hello All i am wondering how to call a batch of SQL server scripts from inside a C# code Thanx for your help

        G Offline
        G Offline
        Giorgi Dalakishvili
        wrote on last edited by
        #3

        You can use SqlCommand class to execute commands against sql database.

        Giorgi Dalakishvili #region signature my articles #endregion

        H 1 Reply Last reply
        0
        • S Sabry1905

          Hello All i am wondering how to call a batch of SQL server scripts from inside a C# code Thanx for your help

          E Offline
          E Offline
          Eslam Afifi
          wrote on last edited by
          #4

          You can use the SqlConnection and SqlCommand in the System.Data.SqlClient namespace. Or you can use the ServerConnection in the Microsoft.SqlServer.Management.Common namespace (reference the Microsoft.SqlServer.ConnectionInfo assembly).

          Eslam Afifi

          1 Reply Last reply
          0
          • S Sabry1905

            Hello All i am wondering how to call a batch of SQL server scripts from inside a C# code Thanx for your help

            H Offline
            H Offline
            Harvey Saayman
            wrote on last edited by
            #5

            i wrote a program a while back to run all my sql scripts for me, and its FORCES the db to drop connections aswell(this is for dev purposes only) it uses SQLCMD.exe, and you need sql2005 for that... heres the code if your interested

            using System;
            using System.Collections.Generic;
            using System.Linq;
            using System.Text;
            using System.Diagnostics;
            using System.Threading;
            using System.IO;
            using Microsoft.SqlServer.Management.Smo;
            using Microsoft.SqlServer.Management.Smo.Wmi;
            using Microsoft.SqlServer.Management.Common;

            namespace CreateDB
            {
            public class Program
            {
            public static string database;
            public static string userName;
            public static string password;

                static void Main(string\[\] args)
                {
                    getVariables();
            
                    FileStream fs = new FileStream(@"C:\\scripts.txt", FileMode.Open, FileAccess.Read);
                    StreamReader sr = new StreamReader(fs);
            
                    killDatabase();
            
                    string line = sr.ReadLine();
                    while (line != null)
                    {
                        ProcessStartInfo StartInfo = new ProcessStartInfo("sqlcmd", "-S " + database + " -d master" + " -U " + userName + " -P " + password + " -i " + line);
                        Process myProcess = new Process();
            
                        StartInfo.UseShellExecute = false;
                        StartInfo.RedirectStandardOutput = true;
            
                        myProcess.StartInfo = StartInfo;
                        myProcess.Start();
            
                        Console.Write("Started Process --> ");
                        myProcess.WaitForExit();
            
                        StreamReader outputReader = myProcess.StandardOutput;
                        Console.WriteLine("Finnished Process ---> output:" + "\\r\\n");
                        Console.WriteLine(outputReader.ReadToEnd());
            
                        Console.WriteLine();
                        Console.WriteLine("|------------------------------------------------------------------------------|");
                        Console.WriteLine();
            
                        line = sr.ReadLine();
                    }
                    Console.ForegroundColor = ConsoleColor.Green;
                    Console.WriteLine("DDDD      OOOO    N    N  EEEEEE  ");
                    Console.WriteLine("D   D    O    O   NN   N  E       ");
                    Console.WriteLine("D    D  O      O  N N  N  EEE     ");
                    Console.WriteLine("D    D  O      O  N  N N  EEE     ");
                    Console.WriteLine("D   D    O    O   N   NN  E       ");
                    Console.WriteLine("DDDD      OOOO    N    N  EEEEEE  ");
                    C
            
            S 1 Reply Last reply
            0
            • G Giorgi Dalakishvili

              You can use SqlCommand class to execute commands against sql database.

              Giorgi Dalakishvili #region signature my articles #endregion

              H Offline
              H Offline
              Harvey Saayman
              wrote on last edited by
              #6

              i tried that when i wrote the 1st version of my "execute SQL scripts" app, it didn't work, although i thought i would :doh: check my other post to see what i ended up doing

              Harvey Saayman - South Africa Junior Developer .Net, C#, SQL you.suck = (you.Passion != Programming & you.Occupation == jobTitles.Programmer) 1000100 1101111 1100101 1110011 100000 1110100 1101000 1101001 1110011 100000 1101101 1100101 1100001 1101110 100000 1101001 1101101 100000 1100001 100000 1100111 1100101 1100101 1101011 111111

              G 1 Reply Last reply
              0
              • H Harvey Saayman

                i tried that when i wrote the 1st version of my "execute SQL scripts" app, it didn't work, although i thought i would :doh: check my other post to see what i ended up doing

                Harvey Saayman - South Africa Junior Developer .Net, C#, SQL you.suck = (you.Passion != Programming & you.Occupation == jobTitles.Programmer) 1000100 1101111 1100101 1110011 100000 1110100 1101000 1101001 1110011 100000 1101101 1100101 1100001 1101110 100000 1101001 1101101 100000 1100001 100000 1100111 1100101 1100101 1101011 111111

                G Offline
                G Offline
                Giorgi Dalakishvili
                wrote on last edited by
                #7

                Thanks for the info.

                Giorgi Dalakishvili #region signature my articles #endregion

                H 1 Reply Last reply
                0
                • G Giorgi Dalakishvili

                  Thanks for the info.

                  Giorgi Dalakishvili #region signature my articles #endregion

                  H Offline
                  H Offline
                  Harvey Saayman
                  wrote on last edited by
                  #8

                  your welcome :)

                  Harvey Saayman - South Africa Junior Developer .Net, C#, SQL you.suck = (you.Passion != Programming & you.Occupation == jobTitles.Programmer) 1000100 1101111 1100101 1110011 100000 1110100 1101000 1101001 1110011 100000 1101101 1100101 1100001 1101110 100000 1101001 1101101 100000 1100001 100000 1100111 1100101 1100101 1101011 111111

                  1 Reply Last reply
                  0
                  • H Harvey Saayman

                    i wrote a program a while back to run all my sql scripts for me, and its FORCES the db to drop connections aswell(this is for dev purposes only) it uses SQLCMD.exe, and you need sql2005 for that... heres the code if your interested

                    using System;
                    using System.Collections.Generic;
                    using System.Linq;
                    using System.Text;
                    using System.Diagnostics;
                    using System.Threading;
                    using System.IO;
                    using Microsoft.SqlServer.Management.Smo;
                    using Microsoft.SqlServer.Management.Smo.Wmi;
                    using Microsoft.SqlServer.Management.Common;

                    namespace CreateDB
                    {
                    public class Program
                    {
                    public static string database;
                    public static string userName;
                    public static string password;

                        static void Main(string\[\] args)
                        {
                            getVariables();
                    
                            FileStream fs = new FileStream(@"C:\\scripts.txt", FileMode.Open, FileAccess.Read);
                            StreamReader sr = new StreamReader(fs);
                    
                            killDatabase();
                    
                            string line = sr.ReadLine();
                            while (line != null)
                            {
                                ProcessStartInfo StartInfo = new ProcessStartInfo("sqlcmd", "-S " + database + " -d master" + " -U " + userName + " -P " + password + " -i " + line);
                                Process myProcess = new Process();
                    
                                StartInfo.UseShellExecute = false;
                                StartInfo.RedirectStandardOutput = true;
                    
                                myProcess.StartInfo = StartInfo;
                                myProcess.Start();
                    
                                Console.Write("Started Process --> ");
                                myProcess.WaitForExit();
                    
                                StreamReader outputReader = myProcess.StandardOutput;
                                Console.WriteLine("Finnished Process ---> output:" + "\\r\\n");
                                Console.WriteLine(outputReader.ReadToEnd());
                    
                                Console.WriteLine();
                                Console.WriteLine("|------------------------------------------------------------------------------|");
                                Console.WriteLine();
                    
                                line = sr.ReadLine();
                            }
                            Console.ForegroundColor = ConsoleColor.Green;
                            Console.WriteLine("DDDD      OOOO    N    N  EEEEEE  ");
                            Console.WriteLine("D   D    O    O   NN   N  E       ");
                            Console.WriteLine("D    D  O      O  N N  N  EEE     ");
                            Console.WriteLine("D    D  O      O  N  N N  EEE     ");
                            Console.WriteLine("D   D    O    O   N   NN  E       ");
                            Console.WriteLine("DDDD      OOOO    N    N  EEEEEE  ");
                            C
                    
                    S Offline
                    S Offline
                    Sabry1905
                    wrote on last edited by
                    #9

                    Thanks Harvey, this is exactly what i was looking for.

                    H 1 Reply Last reply
                    0
                    • S Sabry1905

                      Thanks Harvey, this is exactly what i was looking for.

                      H Offline
                      H Offline
                      Harvey Saayman
                      wrote on last edited by
                      #10

                      Glad to help bud, let me know if you have any troubles with it

                      Harvey Saayman - South Africa Junior Developer .Net, C#, SQL you.suck = (you.Passion != Programming & you.Occupation == jobTitles.Programmer) 1000100 1101111 1100101 1110011 100000 1110100 1101000 1101001 1110011 100000 1101101 1100101 1100001 1101110 100000 1101001 1101101 100000 1100001 100000 1100111 1100101 1100101 1101011 111111

                      S 1 Reply Last reply
                      0
                      • H Harvey Saayman

                        Glad to help bud, let me know if you have any troubles with it

                        Harvey Saayman - South Africa Junior Developer .Net, C#, SQL you.suck = (you.Passion != Programming & you.Occupation == jobTitles.Programmer) 1000100 1101111 1100101 1110011 100000 1110100 1101000 1101001 1110011 100000 1101101 1100101 1100001 1101110 100000 1101001 1101101 100000 1100001 100000 1100111 1100101 1100101 1101011 111111

                        S Offline
                        S Offline
                        Sabry1905
                        wrote on last edited by
                        #11

                        i have an issue in running my script for each line in the script i got this a message like this Started Process --> Sqlcmd: ')': Invalid filename. Finnished Process ---> output: !!!

                        H 1 Reply Last reply
                        0
                        • S Sabry1905

                          i have an issue in running my script for each line in the script i got this a message like this Started Process --> Sqlcmd: ')': Invalid filename. Finnished Process ---> output: !!!

                          H Offline
                          H Offline
                          Harvey Saayman
                          wrote on last edited by
                          #12

                          can you post what you have in scripts.txt... and is that file located on the C:\ drive?

                          Harvey Saayman - South Africa Junior Developer .Net, C#, SQL you.suck = (you.Passion != Programming & you.Occupation == jobTitles.Programmer) 1000100 1101111 1100101 1110011 100000 1110100 1101000 1101001 1110011 100000 1101101 1100101 1100001 1101110 100000 1101001 1101101 100000 1100001 100000 1100111 1100101 1100101 1101011 111111

                          S 1 Reply Last reply
                          0
                          • H Harvey Saayman

                            can you post what you have in scripts.txt... and is that file located on the C:\ drive?

                            Harvey Saayman - South Africa Junior Developer .Net, C#, SQL you.suck = (you.Passion != Programming & you.Occupation == jobTitles.Programmer) 1000100 1101111 1100101 1110011 100000 1110100 1101000 1101001 1110011 100000 1101101 1100101 1100001 1101110 100000 1101001 1101101 100000 1100001 100000 1100111 1100101 1100101 1101011 111111

                            S Offline
                            S Offline
                            Sabry1905
                            wrote on last edited by
                            #13

                            i had sent you the script through mail, yes the script lie on the C:\ partition

                            1 Reply Last reply
                            0
                            Reply
                            • Reply as topic
                            Log in to reply
                            • Oldest to Newest
                            • Newest to Oldest
                            • Most Votes


                            • Login

                            • Don't have an account? Register

                            • Login or register to search.
                            • First post
                              Last post
                            0
                            • Categories
                            • Recent
                            • Tags
                            • Popular
                            • World
                            • Users
                            • Groups