i am new to run SQL Scripts using sqlcmd in C#. i saw some code in Internet but i am not understanding how it Works.
string path = string.Empty;
OpenFileDialog opd = new OpenFileDialog();
opd.Filter = "sql files|*.sql";
if (opd.ShowDialog() == DialogResult.OK)
{
path = opd.FileName;//Here i am 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 for the process exits. The WaitForExit() method doesn't work
if (process.HasExited)
break;
Thread.Sleep(500);
}
i am not understanding how these three lines are 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 i am doing this means i want to run a SQL SCRIPT the script which execute to create a database. but i want to do using sqlcmd. In Client Place if i execute my .exe file it finish my work(to attach database to Server).
Please help me regarding this.