This code in my project I defined a method for send command
You can analyse this method
public static bool SendCommand(string IP, string DatabaseName, string UserName, string Password, string SqlQueryScript)
{
bool result = false;
try
{
SqlConnection connection = new SqlConnection("Data Source=" + IP + ";Initial Catalog=" + DatabaseName + ";User ID=" + UserName + ";Password=" + Password);
string[] commandTextArray = SqlQueryScript.Split(new string[] { "GO" }, StringSplitOptions.RemoveEmptyEntries);
connection.Open();
foreach (string commandText in commandTextArray)
{
if (commandText.Trim() == string.Empty) continue;
SqlCommand command = new SqlCommand(commandText, connection);
command.ExecuteNonQuery();
connection.InfoMessage += new SqlInfoMessageEventHandler(myConnection_InfoMessage);
}
connection.Close();
result = true;
}
catch(Exception ex)
{
ErrorMessage.errorMessage = ex.Message + "\n (" + DateTime.Now.ToLongTimeString()+")";
}
return result;
}