关于安装程序初始化sql server 的问题。

yuanrichard 2005-08-20 04:58:09
我写了一个数据库的程序,数据库是用的sql server,我需要在安装过程中新建一些数据库表和导入一些数据,这些都存在.sql文件中。我应该怎么做呢,那位达人指点一下。
...全文
300 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
zxyjyzxyjy 2005-08-20
  • 打赏
  • 举报
回复
在installshield中的代码
zxyjyzxyjy 2005-08-20
  • 打赏
  • 举报
回复
///////////////////////////////////////////////////////////////////////////////
//
// Function: CreateDatabase
//
// Purpose: This function will create a new database.
//
// Arguments: svServerName - The name of the SQL server to run the script on
// svDatabaseName - The name of the SQL database to run the script on
// svDriver - ADO requires this, but for SQL server you just send in "SQL Server"
// svUserName - The system account for SQL server
// svUserPassword - The password for the system account
//
// Usage:
// if (CreateDatabase("SQLServer", "MyDatabase", "SQL Server", "sa", "saPassword") = FALSE) then
//
///////////////////////////////////////////////////////////////////////////////
function BOOL CreateDatabase(svServerName, svDatabaseName, svDriver, svUserName, svUserPassword)
STRING szADOCommObjID, szADOObjID, szConnSting;
OBJECT pADOObj, pADOCommObj;
begin

// Create ADO Connection Object to connect to the SQL server
szADOObjID = "ADODB.Connection";
set pADOObj = CreateObject(szADOObjID);

// Create the SQL string to complete the connection
szConnSting = "driver={" + svDriver + "};";
szConnSting = szConnSting + "server=" + svServerName + ";";
szConnSting = szConnSting + "uid=" + svUserName + ";";
szConnSting = szConnSting + "pwd=" + svUserPassword;

// Open the ADO Connection
pADOObj.Open(szConnSting);

// Create the ADO Command object to execute the script
szADOCommObjID = "ADODB.Command";
set pADOCommObj = CreateObject(szADOCommObjID);
pADOCommObj.ActiveConnection = pADOObj;

// Execute the call to create the database
pADOCommObj.CommandText = "Create database " + svDatabaseName;
pADOCommObj.Execute();

return TRUE;
end;


///////////////////////////////////////////////////////////////////////////////
//
// Function: ExecuteSQLScript
//
// Purpose: This function will execute a SQL script.
//
// Arguments: svServerName - The name of the SQL server to run the script on
// svDatabaseName - The name of the SQL database to run the script on
// svDriver - ADO requires this, but for SQL server you just send in "SQL Server"
// svUserName - The system account for SQL server
// svUserPassword - The password for the system account
// svScriptFile - A fully qualified path to a SQL script file
// svErrLog - Reference to a string which will contain teh log of errors occured during the execution of SQL script file
//
// Usage:
// ExecuteSQLScript("SQLServer", "MyDatabase", "SQL Server", "sa", "saPassword", "C:\Scripts\tables.sql", svLog)
//
// Notes: You could change this function to accept the script as a string
// instead of the file. With a file, you can add the scripts
// as files in the installation, putting them in a directory.
// This would allow them to be reused later, or they can just be
// deleted after each script is run.
//
///////////////////////////////////////////////////////////////////////////////
function BOOL ExecuteSQLScript(svServerName, svDatabaseName, svDriver, svUserName, svUserPassword, svScriptFile, svErrLog)
OBJECT pADOObj, pADOCommObj;
STRING szADOObjID, szADOCommObjID;
STRING svLine, szConnString, szSQL, svString;
STRING szHead, szTail;
NUMBER nResult, nRecords, nBegin, nEnd;
BOOL bExitLoop;
LIST listID;
LIST listCmd;
number nRet;
string szErrorLog;
begin
szErrorLog = "";
nRet = 0;
// Create an empty string list.
listID = ListCreate(STRINGLIST);
listCmd = ListCreate(STRINGLIST);

// Read the SQL script file into the list
if (ListReadFromFile(listID, svScriptFile) < 0) then // read list from file
svErrLog = "Unable to open SQL script: " + svScriptFile + ".";
return -1;
endif;

// Go through each list item and add it to a string (which will then hold the script)
szSQL = "";
nResult = ListGetFirstString(listID, svString);
while (nResult = 0)
if (StrFind(svString, "GO") = 0) then
// Remove comments
bExitLoop = FALSE;
repeat
nBegin = StrFind(szSQL, "/*");
nEnd = StrFind(szSQL, "*/");
if ((nBegin >= 0) && (nBegin < nEnd)) then
StrSub(szHead, szSQL, 0, nBegin);
StrSub(szTail, szSQL, nEnd+2, 65535);
szSQL = szHead + szTail;
else
bExitLoop = TRUE;
endif;
until (bExitLoop);
// Add it to commands
if (StrLengthChars(szSQL) > 0) then
ListAddString(listCmd, szSQL, AFTER);
endif;
szSQL = "";
elseif (StrFind(svString, "--") = 0) then
// It is a comment, so do nothing
else
szSQL = szSQL + "\r\n" + svString;
endif;
nResult = ListGetNextString(listID, svString);
endwhile;

// Be good and clean up your trash
ListDestroy(listID);

// Create ADO Connection Object to connect to the SQL server
szADOObjID = "ADODB.Connection";
set pADOObj = CreateObject(szADOObjID);

// Create the SQL string to complete the connection
szConnString = "driver={" + svDriver + "};";
szConnString = szConnString + "server=" + svServerName + ";";
szConnString = szConnString + "uid=" + svUserName + ";";
szConnString = szConnString + "pwd=" + svUserPassword + ";";
szConnString = szConnString + "database=" + svDatabaseName;

// Open the ADO Connection
pADOObj.Open(szConnString);

// Create the ADO Command object to execute the script
szADOCommObjID = "ADODB.Command";
set pADOCommObj = CreateObject(szADOCommObjID);
pADOCommObj.ActiveConnection = pADOObj;

// Execute each command
nResult = ListGetFirstString(listCmd, svString);
while (nResult = 0)
try
// Execute the call to run the script
pADOCommObj.CommandText = svString;
pADOCommObj.Execute(nRecords ,0x81);
catch
nRet = Err.Number;

szErrorLog = szErrorLog
+ "Error: " + Err.Description + "\n"
+ "Source: " + Err.Source + "\n"
+ "Script:\n" + svString + "\n\n";
endcatch;

nResult = ListGetNextString(listCmd, svString);
endwhile;

// Be good and clean up your trash
ListDestroy(listCmd);

svErrLog = szErrorLog;
return nRet;
end;
zxyjyzxyjy 2005-08-20
  • 打赏
  • 举报
回复
prototype BOOL ExecuteSQLScript(STRING, STRING, STRING, STRING, STRING, STRING, BYREF STRING);
prototype BOOL DoesDatabaseExist(STRING, STRING, STRING, STRING, STRING);
prototype BOOL CreateDatabase(STRING, STRING, STRING, STRING, STRING);


///////////////////////////////////////////////////////////////////////////////
//
// Function: DoesDatabaseExist
//
// Purpose: This function will determine whether a given database exists.
//
// Arguments: svServerName - The name of the SQL server to run the script on
// svDatabaseName - The name of the SQL database to run the script on
// svDriver - ADO requires this, but for SQL server you just send in "SQL Server"
// svUserName - The system account for SQL server
// svUserPassword - The password for the system account
//
// Usage:
// if (DoesDatabaseExist("SQLServer", "MyDatabase", "SQL Server", "sa", "saPassword") = FALSE) then
//
///////////////////////////////////////////////////////////////////////////////
function BOOL DoesDatabaseExist(svServerName, svDatabaseName, svDriver, svUserName, svUserPassword)
OBJECT pADOConnObj, pADORecordSetObj;
STRING szADOConnObjID, szADORecordSetObjID, szConnString, szSQL;
BOOL bExists;
begin
bExists = FALSE;

// Create ADO Connection Object to connect to the SQL server
szADOConnObjID = "ADODB.Connection";
set pADOConnObj = CreateObject(szADOConnObjID);

// Create the SQL string to complete the connection
szConnString = "driver={" + svDriver + "};";
szConnString = szConnString + "server=" + svServerName + ";";
szConnString = szConnString + "uid=" + svUserName + ";";
szConnString = szConnString + "pwd=" + svUserPassword + ";";
szConnString = szConnString + "database=master";

// Open the ADO Connection
pADOConnObj.Open(szConnString);

// Create ADO Recordset object for the return
szADORecordSetObjID = "ADODB.Recordset";
set pADORecordSetObj = CreateObject(szADORecordSetObjID);

// Set some ADO Recordset properties
pADORecordSetObj.CursorType = 3;
pADORecordSetObj.ActiveConnection = pADOConnObj;

// Create the SQL string to retrieve the database if it exists
szSQL = "Select name from sysdatabases where name='" + svDatabaseName + "'";

// Use the recordset to see if the database exists
pADORecordSetObj.Open(szSQL);
if (pADORecordSetObj.RecordCount = 1) then
bExists = TRUE;
endif;

return bExists;
end;
xiaoyao19811201 2005-08-20
  • 打赏
  • 举报
回复
写一个存储过程,存储过程中是一些create表和insert数据的sql语句。
然后在vc中调用!

4,011

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 数据库
社区管理员
  • 数据库
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧