Click here to Skip to main content
15,887,596 members
Home / Discussions / C#
   

C#

 
Questionrun SQL server scripts from inside a C# code Pin
Sabry190515-Sep-08 1:20
Sabry190515-Sep-08 1:20 
AnswerRe: run SQL server scripts from inside a C# code Pin
Vimalsoft(Pty) Ltd15-Sep-08 1:28
professionalVimalsoft(Pty) Ltd15-Sep-08 1:28 
AnswerRe: run SQL server scripts from inside a C# code Pin
Eslam Afifi15-Sep-08 1:42
Eslam Afifi15-Sep-08 1:42 
AnswerRe: run SQL server scripts from inside a C# code Pin
Giorgi Dalakishvili15-Sep-08 1:42
mentorGiorgi Dalakishvili15-Sep-08 1:42 
GeneralRe: run SQL server scripts from inside a C# code Pin
Harvey Saayman15-Sep-08 4:06
Harvey Saayman15-Sep-08 4:06 
GeneralRe: run SQL server scripts from inside a C# code Pin
Giorgi Dalakishvili15-Sep-08 4:15
mentorGiorgi Dalakishvili15-Sep-08 4:15 
GeneralRe: run SQL server scripts from inside a C# code Pin
Harvey Saayman15-Sep-08 4:20
Harvey Saayman15-Sep-08 4:20 
AnswerRe: run SQL server scripts from inside a C# code Pin
Harvey Saayman15-Sep-08 4:02
Harvey Saayman15-Sep-08 4:02 
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  ");
            Console.ResetColor();
            Console.Read();
        }

        private static void getVariables()
        {
            Console.Write("Please Enter DataBase Name: ---> ");
            database = Console.ReadLine();

            Console.Write("User Name ---------------------> ");
            userName = Console.ReadLine();

            Console.Write("Password ----------------------> ");
            password = Console.ReadLine();

            Console.Clear();
        }

        private static void killDatabase()
        {
            try
            {
                Server srv = new Server(database);
                srv.KillDatabase("uniclox_db");
            }
            catch (Exception ex)
            {
                Console.ForegroundColor = ConsoleColor.Red;

                Console.WriteLine(ex.Message);
                Console.ResetColor();
            }
        }
    }
}


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

GeneralRe: run SQL server scripts from inside a C# code Pin
Sabry190515-Sep-08 21:04
Sabry190515-Sep-08 21:04 
GeneralRe: run SQL server scripts from inside a C# code Pin
Harvey Saayman15-Sep-08 21:06
Harvey Saayman15-Sep-08 21:06 
GeneralRe: run SQL server scripts from inside a C# code Pin
Sabry190516-Sep-08 4:17
Sabry190516-Sep-08 4:17 
GeneralRe: run SQL server scripts from inside a C# code Pin
Harvey Saayman16-Sep-08 4:23
Harvey Saayman16-Sep-08 4:23 
GeneralRe: run SQL server scripts from inside a C# code Pin
Sabry190517-Sep-08 20:38
Sabry190517-Sep-08 20:38 
QuestionChanging the value of text box at run time in .net 2.0 windows application Pin
tauras8115-Sep-08 0:38
tauras8115-Sep-08 0:38 
AnswerRe: Changing the value of text box at run time in .net 2.0 windows application Pin
DaveyM6915-Sep-08 0:46
professionalDaveyM6915-Sep-08 0:46 
GeneralRe: Changing the value of text box at run time in .net 2.0 windows application Pin
tauras8115-Sep-08 0:51
tauras8115-Sep-08 0:51 
GeneralRe: Changing the value of text box at run time in .net 2.0 windows application Pin
DaveyM6915-Sep-08 0:57
professionalDaveyM6915-Sep-08 0:57 
GeneralRe: Changing the value of text box at run time in .net 2.0 windows application Pin
tauras8115-Sep-08 1:03
tauras8115-Sep-08 1:03 
GeneralRe: Changing the value of text box at run time in .net 2.0 windows application Pin
DaveyM6915-Sep-08 1:17
professionalDaveyM6915-Sep-08 1:17 
GeneralRe: Changing the value of text box at run time in .net 2.0 windows application Pin
Frank Horn15-Sep-08 1:59
Frank Horn15-Sep-08 1:59 
GeneralRe: Changing the value of text box at run time in .net 2.0 windows application Pin
tauras8115-Sep-08 18:31
tauras8115-Sep-08 18:31 
AnswerRe: Changing the value of text box at run time in .net 2.0 windows application Pin
Giorgi Dalakishvili15-Sep-08 0:54
mentorGiorgi Dalakishvili15-Sep-08 0:54 
QuestionSelect Child Treeview Node Pin
Programm3r15-Sep-08 0:30
Programm3r15-Sep-08 0:30 
AnswerRe: Select Child Treeview Node Pin
Programm3r15-Sep-08 0:36
Programm3r15-Sep-08 0:36 
GeneralRe: Select Child Treeview Node Pin
DaveyM6915-Sep-08 0:44
professionalDaveyM6915-Sep-08 0:44 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.