Microsoft SQL Server has an SDK library that provides various commands equivalent to the Manager function. As long as the corresponding Dll is referenced, the corresponding commands can be executed (. SQL.
1. reference the corresponding DLL (version: SQL 2008)
REFERENCE The following DLL, where the file is located: Install Mssql directory + Microsoft SQL Server \ 100 \ SDK \ Assemblies
Microsoft. SqlServer. ConnectionInfo. dll
Microsoft. SqlServer. Dmf. dll
Microsoft. SqlServer. Management. Sdk. Sfc. dll
Microsoft. SqlServer. ServiceBrokerEnum. dll
Microsoft. SqlServer. Smo. dll
Microsoft. SqlServer. SqlClrProvider. dll
Microsoft. SqlServer. SqlEnum. dll
Ii. Method
# Region Execute file of SQL
Public bool ExecuteSqlFile (string userId, string password, string server, string file)
{
Bool result = true;
Try
{
String sqlConnectionString = string. Format ("server = {2}; database = master; uid = {0}; pwd = {1}", userId, password, server );
FileInfo fileInfo = new FileInfo (file );
StringBuilder script = new StringBuilder (fileInfo. OpenText (). ReadToEnd ());
SqlConnection conn = new SqlConnection (sqlConnectionString );
Microsoft. SqlServer. Management. Smo. Server sqlServer = new Microsoft. SqlServer. Management. Smo. Server (new Microsoft. SqlServer. Management. Common. ServerConnection (conn ));
SqlServer. ConnectionContext. ExecuteNonQuery (script. ToString ());
}
Catch
{
Result = false;
}
Return result;
}
# Endregion
You can reference this method to execute a file to a remote database. The reference method is as follows:
Result = executesqlfile (dbusername, dbpassword, dbserver, filename );