2013-04-24 109 views
0

//我在看起来像了声明标量变量@@ SA_CONSOLE_HOSTNAME

//插入到表名(主机,实例)值(@SA_CONSOLE_HOSTNAME,值)查询,在表名我已经创建的列主机和主机将作为我的本地机器名称的实例。我不想每次代码在不同的系统 因此创造了一个可变的,并且机器名称,将其运行时输入计算机名,

//我创建了一个存根并通过计算机名给它

String scriptAndStub; 

scriptAndStub = 
    "DECLARE @SA_CONSOLE_HOSTNAME VARCHAR(256)\n" + \\ creating a variable 
    "SET @SA_CONSOLE_HOSTNAME = @[email protected]\n"; 
scriptAndStub += script; 

executeScript.CommandText = scriptAndStub; 
executeScript.Parameters.AddWithValue("@[email protected]", Environment.MachineName); \\passing machine name 

//当我点击文本框检查语法按钮,其产生错误

private void btnSyntaxCheck_Click(object sender, EventArgs e) 
     { 
      try 
      { 
       tsStatus.Text = "Checking Syntax..."; 
       LogMessage(DA_Base.Constants.ERROR_LEVEL_DEBUG, "SQLScripting::SyntaxCheck", "Query: " + rtbScript.Text); 
       using (SqlCommand myCommand = new SqlCommand(string.Empty, Connection)) 
       { 
        foreach (string script in Regex.Split(rtbScript.Text, "^GO\r?$", RegexOptions.Multiline | RegexOptions.IgnoreCase)) 
         { 
          if (!String.IsNullOrEmpty(script)) 
          { 
          myCommand.CommandText = "SET PARSEONLY ON\n" + script; 
          myCommand.ExecuteNonQuery(); 
          } 
         } 
       } 
       tsStatus.Text = "Checking Syntax Complete. No errors reported."; 
      } 
      catch (Exception exc) \\error has been caught here.... 
      { 
       tsStatus.Text = exc.Message; 
       MessageBox.Show(exc.Message, "SQL Syntax Check"); 
      } 
     } 
    } 
} 

回答

0
FIXED IT BY DECLARING IT IN THE MAIN FUNCTION AND CLEARING THE @[email protected] FINE.. 


String scriptAndStub; 

scriptAndStub = 
    "DECLARE @SA_CONSOLE_HOSTNAME VARCHAR(256)\n" + \\ creating a variable 
    "SET @SA_CONSOLE_HOSTNAME = @[email protected]\n"; 
    executescript.parameters.clear(); 
scriptAndStub += script; 

executeScript.CommandText = scriptAndStub; 
executeScript.Parameters.AddWithValue("@[email protected]", Environment.MachineName); \\passing machine name 

//当我点击TEX检查语法按钮t盒产生错误

private void btnSyntaxCheck_Click(object sender, EventArgs e) 
     { 
      try 
      { 
       tsStatus.Text = "Checking Syntax..."; 
       LogMessage(DA_Base.Constants.ERROR_LEVEL_DEBUG, "SQLScripting::SyntaxCheck", "Query: " + rtbScript.Text); 

       String scriptAndStub; 
       scriptAndStub = "DECLARE @SA_CONSOLE_HOSTNAME VARCHAR(256)\n"; 

       using (SqlCommand myCommand = new SqlCommand(string.Empty, Connection)) 
       { 
        foreach (string script in Regex.Split(rtbScript.Text, "^GO\r?$", RegexOptions.Multiline | RegexOptions.IgnoreCase)) 
         { 
          if (!String.IsNullOrEmpty(script)) 
          { 
          myCommand.CommandText = "SET PARSEONLY ON\n" + script; 
          myCommand.ExecuteNonQuery(); 
          } 
         } 
       } 
       tsStatus.Text = "Checking Syntax Complete. No errors reported."; 
      } 
      catch (Exception exc) \\error has been caught here.... 
      { 
       tsStatus.Text = exc.Message; 
       MessageBox.Show(exc.Message, "SQL Syntax Check"); 
      } 
     } 
    } 
}