How to run a SQL Server 2008 R2 Database Script using sqlcmd in C#? -


i new run sql scripts using sqlcmd in c#. saw code in internet not understanding how works.

string path = string.empty;             openfiledialog opd = new openfiledialog();             opd.filter = "sql files|*.sql";             if (opd.showdialog() == dialogresult.ok)             {                 path = opd.filename;//here taking database sqlscript             }         string tmpfile = path.gettempfilename();             sqlconnectionstringbuilder connection=new sqlconnectionstringbuilder(@"data source=lptp2\lptp2;initial catalog=database;integrated security=true");         string argument = string.format(@" -s {0} -d {1} -i ""{2}"" -o ""{3}""",             @".\sqlexpress", "database", path, tmpfile);          // append user/password if not use integrated security         if (!connection.integratedsecurity)             argument += string.format(" -u {0} -p {1}", "sa", "abc@123");          var process = process.start("sqlcmd.exe", argument);         process.startinfo.useshellexecute = false;         process.startinfo.createnowindow = true;         process.start();         while (true)         {             // wait process exits. waitforexit() method doesn't work             if (process.hasexited)                 break;             thread.sleep(500);         } 

i not understanding how these 3 lines working

 string tmpfile = path.gettempfilename();             sqlconnectionstringbuilder connection=new sqlconnectionstringbuilder(@"data source=lptp2\lptp2;initial catalog=hemotrace;integrated security=true");         string argument = string.format(@" -s {0} -d {1} -i ""{2}"" -o ""{3}""",             @".\sqlexpress", "hemotrace", path, tmpfile);          // append user/password if not use integrated security         if (!connection.integratedsecurity)             argument += string.format(" -u {0} -p {1}", "sa", "abc@123"); 

why doing means want run sql script script execute create database. want using sqlcmd. in client place if execute .exe file finish work(to attach database server).

please me regarding this.

string tmpfile = path.gettempfilename(); 

declare string variable called tmpfile , use path.gettempfilename() generate unique temp file name , store in variable

sqlconnectionstringbuilder connection=new  sqlconnectionstringbuilder(@"data source=lptp2\lptp2;initial catalog=hemotrace; integrated security=true"); 

use sqlconnectionstringbuilder class build sql server connection string. doesn't connect anything, generates connection string.

string argument = string.format(@" -s {0} -d {1} -i ""{2}"" -o ""{3}""", @".\sqlexpress", "hemotrace", path, tmpfile); 

declare string called argument , set a bunch of characters, including path temp file generated earlier. bunch of characters suitable use arguments sqlcmd command line.

// append user/password if not use integrated security if (!connection.integratedsecurity) argument += string.format(" -u {0} -p {1}", "sa", "abc@123"); 

use property of sqlconnectionstringbuilder class work out if should add command line switch indicate trusted security.

after of run:

var process = process.start("sqlcmd.exe", argument); 

if dump fill string out you'll find can run on command line.

sqlcmd command line program, incidentally needs installed on client machine.

the command line program takes bunch of arguments have built in previous lines of code.

there issues in code:

  • you need have sqlcmd.exe istalled work.
  • you hard code trusted security in string, load special class use class work out if you're using trusted security... you've hard coded it!
  • you hard code server in connection string hard code different server in arguments sqlcmd
  • it appears connection string (the middle line) totally redundant in case.

Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -