你这种我没有用过,我用的是 strConnection="Provider=SQLOLEDB.1; Persist Security Info=False; UID=sa; PWD=; Initial Catalog = yourdatabase; Data Source=yourcomputer;" ****** 设定初始连接数据库 www.vckbase.com 有ado实例,你去看看 大体上 CoInitialize(NULL); _ConnectionPtr m_pConnection; _CommandPtr m_pCommand; m_pConnection.CreateInstance("ADODB.Connection"); m_pCommand.CreateInstance("ADODB.Command"); m_pConnection->Open(strConnection,....) m_pCommand与m_pConnection关联; m_pCommand->Execute(youSql) m_pCommand->Close(); m_pConnection->Close(); CoUninitialize();生成脚本要配合xp_cmdshell 看下面(转) 用一个MSSQL自带的实用工具scptxfr.exe可以实现,路径要正确 declare @cmd varchar(1000) set @cmd = 'master.dbo.xp_cmdshell ' + '''c:\"Microsoft ' + 'SQL Server"' + '\MSSQL\Upgrade\scptxfr.exe ' + ' /s YourServerName /p YourSAPassword /I /d YourDBName /f ' + 'c:\YourDBName.sql''' exec (@cmd)工具参数说明: SCPTXFR /s <server> /d <database> {[/I] | [/P <password>]} {[/F <script files directory>] | [/f <single script file>]} /q /r /O /T /A /E /C <CodePage> /N /X /H /G /Y /? /s - Indicates the source server to connect to. /d - Indicates the source database to script. /I - Use integrated security. /P - Password to use for 'sa'. Note that login ID is always 'sa'. If /P not used or if a password does not follow the flag, a null password is used. Not compatible with /I. /F - The directory into which the script files should be generated. This means one file is generated for each category of objects. /f - The single file in which all script is to be saved. Not compatible with /F. /q - Use quoted identifiers in the generated scripts. /r - Include drop statements for the objects in the script. /O - Generate OEM script files. Cannot be used with /A or /T. This is the default behavior. /T - Generate UNICODE script files. Cannot be used with /A or /O. /A - Generate ANSI script files. Cannot be used with /T or /O. /? - Command line help. /E - Stop scripting when error occurs. Default behavior is to log the error, and continue. /C - Indicate the CodePage which overrides the server CodePage. /N - Generate ANSI PADDING. /X - Script SPs and XPs to separate files. /H - Generate script files without header (default: with header). /G - Use the specified server name as the prefix for the generated output files(to handle dashes in server name). /Y - Generate script for Extended Properties (valid for 8.x server only). 我是再网吧上网,所以只能说大概,不方便调试。
to njtlxm(njtlxm):怎么在VC下执行还原操作,能给一些示例代码吗??
看SQL里的restore语句说明即可,记住首先得连上master库
能不能给个例子呢,比如,怎么连接数据库,restore语句如何写,我自己做实验试总是错
做一个.bat文件不就可以了吗!
我做过,这样可以的: 用VC+ADO做的程序。 1。登陆SQL Server的数据库Master 2。用ADO产生数据库 1)...Format("IF EXISTS (SELECT name FROM master.dbo.sysdatabases WHERE name = N'%s') DROP DATABASE %s", m_strDBName)...) m_strSQL.Format("Create database %s", m_strDBName) 2)断开数据库连接 strSQL.Format("EXEC sp_detach_db '%s'", m_strDBName); ExecuteSQL(strSQL); 3)连接自己的”数据文件“ strSQL.Format("declare @path varchar(250) \ declare @path1 varchar(300) declare @path2 varchar(300) EXEC master.dbo.xp_regread 'HKEY_LOCAL_MACHINE', 'SOFTWARE\\Microsoft\\MSSQLSERVER\\setup', 'SQLPath',@path output set @path = @path + '\\data\\' set @path1 = @path +'%s.mdf' set @path2 = @path +'%s_log.LDF' EXEC sp_attach_db '%s', @path1, @path2", m_strDBName,m_strDBName,m_strDBName); OK!
只要目标机上有ODBC32就可以.
MS SQL的安装初始化也是用这个的.
参数见于MSSQL-HELP-实用工具.
你打包的时候把它带上.不过我没注意MS有没有许可分发啊.
// 创建数据库
BOOL CAdoX::CreateDatabase(LPCSTR lpszDatabaseName)
{
// 检查数据库是否已存在
HANDLE hFile = NULL;
WIN32_FIND_DATA wfd;
hFile = ::FindFirstFile(lpszDatabaseName, &wfd);
if (hFile != INVALID_HANDLE_VALUE)
{
m_strDatabase = lpszDatabaseName;
::FindClose(hFile);
return FALSE;
} ::FindClose(hFile); _CatalogPtr pCatalog = NULL;
// 创建数据库连接信息
CString strConnection = _T("");
strConnection.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=%s",
lpszDatabaseName);
// 创建数据库
try
{
pCatalog.CreateInstance(__uuidof(Catalog));
pCatalog->Create((_bstr_t)strConnection);
Close();
}
catch(_com_error &e) // 捕捉异常
{
CString strError = _T("");
strError.Format("\n\tSource: %s\n\tDescription: %s",
(_bstr_t)e.Source(), (_bstr_t)e.Description());
MessageBox(NULL, strError, "错误", MB_ICONERROR);
return FALSE;
}
catch(...) // 捕捉不知名错误信息
{
MessageBox(NULL, "不知名错误。", "错误", MB_ICONERROR);
return FALSE;
} return TRUE;
}
你导出数据库脚本时不要导出说明性标题,导出的结构大致如下,每一句的后面都有一个go
作为该句的结束符,用CStdioFile一次读一行,遇到go就把把前面读到的内容读到CString中,如果一句包含多行的,把每次ReadString的内容+空格+下一行拼起来组成长sql语句
然后Ado.command.Eeecute,然后把文件前面的内容删了,继续从文件头处理执行第一句,...,再删除再处理。如果嫌慢的话,不删也行,具体算法多得时,就罗嗦而已
IF EXISTS (SELECT name FROM master.dbo.sysdatabases WHERE name = N'People')
DROP DATABASE [People]
GOCREATE DATABASE [People] ON (NAME = N'People_Data', FILENAME = N'D:\Program Files\Microsoft SQL Server\MSSQL\data\People_Data.MDF' , SIZE = 2, FILEGROWTH = 10%) LOG ON (NAME = N'People_Log', FILENAME = N'D:\Program Files\Microsoft SQL Server\MSSQL\data\People_Log.LDF' , SIZE = 1, FILEGROWTH = 10%)
COLLATE Chinese_PRC_CI_AS
GOexec sp_dboption N'People', N'autoclose', N'false'
GOexec sp_dboption N'People', N'bulkcopy', N'false'
GOexec sp_dboption N'People', N'trunc. log', N'false'
GOexec sp_dboption N'People', N'torn page detection', N'true'
GOexec sp_dboption N'People', N'read only', N'false'
GOexec sp_dboption N'People', N'dbo use', N'false'
GOexec sp_dboption N'People', N'single', N'false'
GO
exec xp_cmdshell 'osql /Usa /P /i f:\\createdatabase.sql'我试过了可以
exec master..xp_cmdshell 'osql /Usa /P /i f:\\createdatabase.sql',NO_OUTPUT
********
也就是说我在ado编程中直接执行这个exec master..xp_cmdshell 'osql /Usa /P /i f:\\createdatabase.sql',NO_OUTPUT
是吧,还有,我在sql server下建个数据库后,可不可以利用sql server的功能自动导出(产生)一个createdatabase.sql文件啊,但是在这之前怎么连接数据库呢,CString strConnection = _T("Provider=MSDASQL;Driver={SQL Server};Server=MOBILE-ONH17WL0;Trusted_Connection=Yes;Database=InnerrangeDatabase;uid=sa;pdw=");是不是如上所示,把Database=InnerrangeDatabase;去掉就可以了
strConnection="Provider=SQLOLEDB.1; Persist Security Info=False; UID=sa; PWD=; Initial Catalog = yourdatabase; Data Source=yourcomputer;"
******
设定初始连接数据库
www.vckbase.com
有ado实例,你去看看
大体上
CoInitialize(NULL);
_ConnectionPtr m_pConnection;
_CommandPtr m_pCommand;
m_pConnection.CreateInstance("ADODB.Connection");
m_pCommand.CreateInstance("ADODB.Command");
m_pConnection->Open(strConnection,....)
m_pCommand与m_pConnection关联;
m_pCommand->Execute(youSql)
m_pCommand->Close();
m_pConnection->Close();
CoUninitialize();生成脚本要配合xp_cmdshell
看下面(转)
用一个MSSQL自带的实用工具scptxfr.exe可以实现,路径要正确
declare @cmd varchar(1000)
set @cmd = 'master.dbo.xp_cmdshell ' +
'''c:\"Microsoft ' +
'SQL Server"' +
'\MSSQL\Upgrade\scptxfr.exe ' +
' /s YourServerName /p YourSAPassword /I /d YourDBName /f ' +
'c:\YourDBName.sql'''
exec (@cmd)工具参数说明:
SCPTXFR /s <server> /d <database> {[/I] | [/P <password>]}
{[/F <script files directory>] | [/f <single script file>]}
/q /r /O /T /A /E /C <CodePage> /N /X /H /G /Y /?
/s - Indicates the source server to connect to.
/d - Indicates the source database to script.
/I - Use integrated security.
/P - Password to use for 'sa'. Note that login ID is always 'sa'.
If /P not used or if a password does not follow the flag,
a null password is used. Not compatible with /I.
/F - The directory into which the script files should be generated.
This means one file is generated for each category of objects.
/f - The single file in which all script is to be saved.
Not compatible with /F.
/q - Use quoted identifiers in the generated scripts.
/r - Include drop statements for the objects in the script.
/O - Generate OEM script files. Cannot be used with /A or /T.
This is the default behavior.
/T - Generate UNICODE script files. Cannot be used with /A or /O.
/A - Generate ANSI script files. Cannot be used with /T or /O.
/? - Command line help.
/E - Stop scripting when error occurs.
Default behavior is to log the error, and continue.
/C - Indicate the CodePage which overrides the server CodePage.
/N - Generate ANSI PADDING.
/X - Script SPs and XPs to separate files.
/H - Generate script files without header (default: with header).
/G - Use the specified server name as the prefix for the generated
output files(to handle dashes in server name).
/Y - Generate script for Extended Properties (valid for 8.x server
only).
我是再网吧上网,所以只能说大概,不方便调试。
用VC+ADO做的程序。
1。登陆SQL Server的数据库Master
2。用ADO产生数据库
1)...Format("IF EXISTS (SELECT name FROM master.dbo.sysdatabases WHERE
name = N'%s') DROP DATABASE %s", m_strDBName)...)
m_strSQL.Format("Create database %s", m_strDBName)
2)断开数据库连接
strSQL.Format("EXEC sp_detach_db '%s'", m_strDBName);
ExecuteSQL(strSQL);
3)连接自己的”数据文件“
strSQL.Format("declare @path varchar(250) \
declare @path1 varchar(300) declare @path2 varchar(300) EXEC master.dbo.xp_regread 'HKEY_LOCAL_MACHINE', 'SOFTWARE\\Microsoft\\MSSQLSERVER\\setup', 'SQLPath',@path output set @path = @path + '\\data\\' set @path1 = @path +'%s.mdf' set @path2 = @path +'%s_log.LDF' EXEC sp_attach_db '%s', @path1, @path2",
m_strDBName,m_strDBName,m_strDBName);
OK!
3)就是恢复数据文件的重要步骤,
您可以将当前程序下自己已经存在的数据文件,
拷贝到数据库服务器对应的Data文件夹下;
然后,再执行 数据文件连接 语句。