//我给出的查询看起来像
//插入tablename(主机,实例)值(@SA_CONSOLE_HOSTNAME,value),在表名中我创建了列host和instance,其中host是我的本地机器名。每次代码在不同的系统中运行时,我都不想输入机器名称 所以为它创建了一个变量和分配的机器名称,
//我创建了一个存根并将机器名传递给它
String scriptAndStub;
scriptAndStub =
"DECLARE @SA_CONSOLE_HOSTNAME VARCHAR(256)\n" + \\ creating a variable
"SET @SA_CONSOLE_HOSTNAME = @HOSTNAME@\n";
scriptAndStub += script;
executeScript.CommandText = scriptAndStub;
executeScript.Parameters.AddWithValue("@HOSTNAME@", 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 :(得分:0)
FIXED IT BY DECLARING IT IN THE MAIN FUNCTION AND CLEARING THE @HOSTNAME@..WORKS FINE..
String scriptAndStub;
scriptAndStub =
"DECLARE @SA_CONSOLE_HOSTNAME VARCHAR(256)\n" + \\ creating a variable
"SET @SA_CONSOLE_HOSTNAME = @HOSTNAME@\n";
executescript.parameters.clear();
scriptAndStub += script;
executeScript.CommandText = scriptAndStub;
executeScript.Parameters.AddWithValue("@HOSTNAME@", 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);
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");
}
}
}
}