C#从文本框中获取变量,并在ExecuteOnQuery中作为变量使用

C#从文本框中获取变量,并在ExecuteOnQuery中作为变量使用,c#,sql,sql-server,executenonquery,C#,Sql,Sql Server,Executenonquery,我有windows窗体应用程序。 我所需要的就是在使用ExecuteOnQuery时将变量从textbox加载到脚本中 我的代码: var connString = @"Data Source=serwer01;Initial Catalog=PolsatCyfrowy;Integrated Security=True;MultipleActiveResultSets=True"; FileInfo file = new FileInfo("C:\\Users\\Kolejki.sql"); S

我有windows窗体应用程序。 我所需要的就是在使用ExecuteOnQuery时将变量从textbox加载到脚本中

我的代码:

var connString = @"Data Source=serwer01;Initial Catalog=PolsatCyfrowy;Integrated Security=True;MultipleActiveResultSets=True";
FileInfo file = new FileInfo("C:\\Users\\Kolejki.sql");
SqlConnection conn = new SqlConnection(connString);
Server server = new Server(new ServerConnection(conn));
conn.Open();
string script = file.OpenText().ReadToEnd();
server.ConnectionContext.ExecuteNonQuery(script);
我的脚本名为:Kolejki.sql看起来:

declare variable1 VARCHAR (50)
set variable1 = '1234'
select * from table1 where id in(@variable1)
如何设置variable1=(textbox1中的文本?textbox1.text)
我只需要使用ExecuteOnQuery,因为它的脚本太长,不能使用SQLCommand—这只是一个例子。

参数化查询是将数据从C#安全地传递到SQL语句的方法,但是它不能与()中的
一起工作。
,请看是的,我知道,这是解释问题的唯一示例。为什么不使用storeprocedures?因为您没有在此服务器上创建StoredProcedures的权限。您应该获得创建存储过程的权限。通过从文本文件中读取sql然后执行来实现这一点会导致灾难。它容易受到sql注入的攻击,我的朋友bobby tables喜欢这种类型的东西。