且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在C#中执行SQL脚本

更新时间:2023-01-23 10:15:35

取决于脚本的含义。你的意思是任意的SQL脚本文件?在那种情况下,我不确定。但是如果你的意思是单独的语句,比如创建一个存储过程,那么下面的技术似乎对我来说很好。

 SqlConnection conn =  new  SqlConnection(  MyConnectionString); 
conn.Open();
SqlCommand cmd = new SqlCommand( CREATE程序SelectThree AS BEGIN SELECT 3 END,conn);
cmd.ExecuteNonQuery();



如果在文本文件中有一堆这样的命令,通常必须在命令之间放置GO。在这种情况下,您可以通过拆分文件来解析文件,只要看到换行符,然后是GO,接着是换行符。然后,您可以使用上述技术来运行每个命令。这在大多数情况下都有效。



如果这对你不起作用,我会使用SQL Profiler来跟踪与SQL Server 2005数据库的所有交互然后使用SMO对该数据库运行脚本。这应该告诉你至少运行什么SQL,以便SMO能够完成它的工作。然后,您可以在所有版本的SQL Server(2000,2005,2008)中使用这些相同的技术。棘手的部分是,如果SMO在发送SQL Server查询之前进行任何复杂的解析。但是,嘿,当你到达那个时,穿过那座桥。



P.S. SMO - 管理您的SQL Server!似乎表明您可以将SMO与SQL Server 2000一起使用。也许你不能在2000数据库中使用2005功能?


我们一直在使用sqlcmd.exe [ ^ ]运行脚本,它在SQL Server 2000和2005中非常可靠,具有常规和快速的风格。文档说它应该与SQL 2008类似。



这个工具是在2005年和2008年安装的,但对于2000,你需要从这里下载 MSDN [ ^ ]

Hey all

I need to execute SQL scripts from a C# application, the trick is that if possible I need to be able to do it with SQL Server 2000, 2005 & 2008.

Googling around I''ve seen that it can be done using the SQL SMO objects, in fact I''ve used SQL SMO before, but it was only introduced in SQL 2005 and up as far as I can tell.

So my question is are there any other ways to execute SQL scripts from C# code that will support SQL 2000 and up incl express editions.

Any suggestions or links appreciated :)

<edit>
With scripts I mean scripts that create a database, update a database(add or alter tables or columns) and backup the database.
</edit>

Depends on what you mean by "scripts". Do you mean arbitrary SQL script files? In that case, I''m not sure. But if you mean individual statements, such as to create a stored procedure, then the below technique seems to work fine for me.
SqlConnection conn = new SqlConnection("MyConnectionString");
conn.Open();
SqlCommand cmd = new SqlCommand("CREATE PROCEDURE SelectThree AS BEGIN SELECT 3 END", conn);
cmd.ExecuteNonQuery();


If you have a bunch of such commands in a text file, usually GO must be placed between the commands. In that case, you could just parse the file by splitting wherever you see a newline, followed by a "GO", followed by a newline. Then you could use the above technique to run each command. That would work in most cases.

If that doesn''t work for you, I''d use SQL Profiler to track all interaction with a SQL Server 2005 database and then use SMO to run a script against that database. That should tell you at least what SQL is run so that SMO can do its job. You might then be able to use those same techniques with all versions of SQL Server (2000, 2005, 2008). The tricky part would be if SMO does any complex parsing before it sends queries SQL Server. But, hey, cross that bridge when you get to it.

P.S. "SMO - Manage your SQL Server!" seems to indicate that you can use SMO with SQL Server 2000. Maybe you just can''t use 2005 features with a 2000 database?


We''ve been using sqlcmd.exe[^] to run scripts and it''s been solid with SQL Server 2000 and 2005, regular and express flavors. The docs say that it should perform similarly with SQL 2008.

This tool is installed with 2005 and 2008 but for 2000 you''ll need to download it from here MSDN[^]