2011年3月21日 星期一

How to execute an sql script file using C#

參考
--
2. Add the using section.



using Microsoft.SqlServer.Management.Smo;

using Microsoft.SqlServer.Management.Common;



3. The hris_db1.sql is the sql script file.



string strConnData = “Data Source=.;Initial Catalog=maser;Integrated Security=True”;

SqlConnection connData = new SqlConnection(strConnData);

FileInfo file = new FileInfo(@”C:\Databases\hris_db1.sql”);

string script = file.OpenText().ReadToEnd();

Server server = new Server(new ServerConnection(connData));

server.ConnectionContext.ExecuteNonQuery(script);

file.OpenText().Close();

connData.Close();

沒有留言:

張貼留言