IS通过ADO附加数据库

用osql莫名其妙的老是附加不上数据库,故改用ADO,一次通过,谢天谢地~

 osql.exe -E -Q "exec sp_attach_db 'XXDB', 'C:\Program Files\MSSQL\Data\XXDB.mdf', 'C:\Program Files\MSSQL\Data\XXDB_log.ldf' "

附上脚本

export prototype BOOL AttachSQLDatabase(STRING, STRING, STRING, STRING, STRING, STRING);
//////////////////////////////////////////////////////////////////////////////
// Function: AttachSQLDatabase
// Description: Attaches a SQL database to the specified server
//////////////////////////////////////////////////////////////////////////////
function BOOL AttachSQLDatabase(sDataFile, sLogFile, sServer, sDatabase, sUser, sPassword)
OBJECT oCon, oCommand;
STRING sSQL, sConnection;
STRING sError, sErrorNumber;
NUMBER nError;


begin


try


SdShowMsg ("Attaching Database " + sDatabase, TRUE);

sConnection = "Provider=SQLOLEDB.1;Password=" + sPassword + ";Persist Security Info=True;User ID=" + sUser + ";Initial Catalog=" + "Master" + ";Data Source=" + sServer;

set oCon = CoCreateObject("ADODB.Connection");
oCon.Open(sConnection);

set oCommand = CoCreateObject("ADODB.Command");
oCommand.ActiveConnection = oCon;

sSQL = "sp_attach_db '" + sDatabase + "', '" + sDataFile +"','" + sLogFile + "'";

//Display Message

oCommand.CommandText = sSQL;
oCommand.Execute();

oCon.Close();

SdShowMsg ("Attaching Database " + sDatabase, FALSE);

return TRUE; //Return Value

catch

nError = Err.Number;
sError = Err.Description;
NumToStr (sErrorNumber, nError);

//WriteLog ("Attach Database Error: " + sErrorNumber, "Description: " + sError ,1);
SdShowMsg ("Attaching Database " + sDatabase, FALSE);

return FALSE;

endcatch;

end;

posted @ 2010-07-06 15:40  Blue`  阅读(160)  评论(0编辑  收藏  举报