我正在调用C#代码中的SQL查询。 如何保持我已经在SQL中声明的变量的声明? 这是我的代码:记住sql中变量的声明
SqlConnection connection = new SqlConnection(conString);
connection.Open();
string s = @"
declare @ttt int
set @ttt = 6
select @ttt";
SqlCommand cmd = new SqlCommand(s, connection);
int retVal = (int)cmd.ExecuteScalar();
s = @"select @ttt";
cmd.CommandText = s;
retVal = (int)cmd.ExecuteScalar();
connection.Close();
误差,在第二的ExecuteScalar()是:必须声明标量变量 “@ttt”。 (因为我做的计算等在我的C#代码,我必须将它拆分成多个查询。 的问题是从C#代码连接和SQL查询的范围。
正如前面的答案所指出的,你的SQL变量超出了命令之间的范围。然而,更侧面的方法是将数据存储在持续**连接**的[本地临时表](http://www.programmers-corner.com/article/76)中。 –