怎样用sql语句把sqlserver2000中的数据库db1中的表倒入到aceess文件中的表?(80分)

L

lunyx

Unregistered / Unconfirmed
GUEST, unregistred user!
怎样用sql语句把sqlserver2000中的数据库db1中的表倒入到aceess文件中的表?例如: <br>现有sql数据库:db1,server=server1,user=sa,psw=sa &nbsp; ;要求把数据库中的两个表tClass,中的数据倒入到 &nbsp; E:/WLGP.mdb中的表tClass中,WLGP.mdb的密码为admin; <br>sql语句该怎么写?请高手指点一下啊。<br>试了一下:<br>insert &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; into &nbsp; &nbsp; &nbsp; OPENROWSET &nbsp; <br>('Microsoft.Jet.OLEDB.4.0','E:/WLGP.mdb';'admin';'admin',tclass) select Classid,ClassName from &nbsp;tClass<br><br>也不行,报错: <br>服务器: &nbsp; 消息 &nbsp; 7399,级别 &nbsp; 16,状态 &nbsp; 1,行 &nbsp; 3 <br>OLE &nbsp; DB &nbsp; 提供程序 &nbsp; 'Microsoft.Jet.OLEDB.4.0' &nbsp; 报错。验证失败。 <br>[OLE/DB &nbsp; provider &nbsp; returned &nbsp; message: &nbsp; 无法启动应用程序。工作组信息文件丢失,或是已被其它用户以独占方式打开。] <br>OLE &nbsp; DB &nbsp; 错误跟踪[OLE/DB &nbsp; Provider &nbsp; 'Microsoft.Jet.OLEDB.4.0' &nbsp; IDBInitialize::Initialize &nbsp; returned &nbsp; 0x80040e4d: &nbsp; &nbsp; 验证失败。]。 <br><br>我没设立工作组,就只有一个密码而已。
 
我一般笨办法,读取一条记录写一条记录。
 
//将2000中的表复制到ACCESS的函数<br>//参数1:2000的连接定义,参数2:access的连接定义,参数3:源表名,参数4:目标表名,参数5:复制条件<br>//复制成功,返回True,失败返回false<br>Function CopySqlToAccess(SourceAdo,TargetAdo:TadoConnection; SourceName,TargetName:String;<br>&nbsp; &nbsp;WhereStr:String; FieldLists:String='*'):Boolean;<br>var tpExecStr:WideString;<br>begin<br>&nbsp; if isTableExist(TargetAdo,TargetName) then<br>&nbsp; &nbsp; TargetAdo.Execute('Drop Table '+TargetName);<br>&nbsp; if not isTableExist(SourceAdo,SourceName) then begin<br>&nbsp; &nbsp; Result:=False;<br>&nbsp; &nbsp; Exit;<br>&nbsp; end;<br>{ &nbsp;if isEmptyTable(SourceAdo,SourceName) then<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; ShowMsg('请求复制的表&lt;'+SourceName+'&gt;是一个空表!');<br>&nbsp; &nbsp; &nbsp; Result:=False;<br>&nbsp; &nbsp; &nbsp; Exit;<br>&nbsp; &nbsp; end; }<br>&nbsp; tpExecStr:=SourceAdo.ConnectionString;<br>&nbsp; tpExecStr:=GetSqlConn(tpExecStr);<br>&nbsp; tpExecStr:='Select '+FieldLists+' into '+TargetName+' From '+SourceName+' '+tpExecStr;<br>&nbsp; try<br>&nbsp; &nbsp; TargetAdo.Execute(tpExecStr);<br>&nbsp; &nbsp; //删除不需要的数据,注意条件表达式是需要的数据,要取反<br>&nbsp; &nbsp; if Length(trim(WhereStr))&gt;0 then begin<br>&nbsp; &nbsp; &nbsp; tpExecStr:='Delete From '+TargetName+' Where not ( '+WhereStr+' )';<br>&nbsp; &nbsp; &nbsp; TargetAdo.Execute(tpExecStr);<br>&nbsp; &nbsp; end;<br>&nbsp; &nbsp; Result:=True;<br>&nbsp; Except<br>&nbsp; &nbsp; on ErrType:Exception do begin<br>// &nbsp; &nbsp; &nbsp;ShowMsg(ErrType.Message); &nbsp; 这一句自己改成其他的提示语句吧<br>&nbsp; &nbsp; &nbsp; Result:=False;<br>&nbsp; &nbsp; end;<br>&nbsp; end;<br>end;<br><br>涉及到的相关函数<br>Function IsTableExist(AdoConn:TadoConnection; TableName:String):Boolean;<br>var<br>&nbsp; MyStringList:TStrings;<br>&nbsp; tpBool:Boolean;<br>&nbsp; i:Integer;<br>begin<br>&nbsp; tpBool:=False;<br>&nbsp; MyStringList := TStringList.Create;<br>&nbsp; try<br>&nbsp; &nbsp; tpBool:=AdoConn.Connected;<br>&nbsp; &nbsp; if Not AdoConn.Connected then<br>&nbsp; &nbsp; &nbsp; AdoConn.Connected:=True;<br>&nbsp; &nbsp; AdoConn.GetTableNames(MyStringList, False);<br>&nbsp; &nbsp; Result:=TRUE;<br>&nbsp; &nbsp; For i:=0 to MyStringList.Count-1 do<br>&nbsp; &nbsp; &nbsp; if UpperCase(MyStringList.Strings)=UpperCase(TableName) then<br>&nbsp; &nbsp; &nbsp; &nbsp; Break;<br>&nbsp; &nbsp; if i=MyStringList.Count then<br>&nbsp; &nbsp; &nbsp; Result:=FALSE;<br>&nbsp; finally<br>&nbsp; &nbsp; MyStringList.Free;<br>&nbsp; &nbsp; AdoConn.Connected:=tpBool;<br>&nbsp; end;<br>end;<br><br>//因为连SQL2k的连接串与连Access的串格式不同,因此需要能过下列函数将<br>//将连2k的串转换成连ACCESS的串<br>Function GetSqlConn(ConnStr:WideString):WideString;<br>var tpRtn:WideString;<br>&nbsp; tpStr:String;<br>&nbsp; tpConn:TStrings;<br>begin<br>//两个连接串的比较<br>//in [ODBC] [ODBC;Driver=SQL Server;UID=sa;PWD=levi;Server=127.0.0.1;DataBase=DocSystem;]<br>//Provider=SQLOLEDB.1;Password=levi;Persist Security Info=True;User ID=sa;<br>//Initial Catalog=DocSystem;Data Source=127.0.0.1<br>&nbsp; tpConn:=nil;<br>&nbsp; tpRtn:=ConnStr+';';<br>&nbsp; try<br>&nbsp; &nbsp; tpConn:=TStringList.Create;<br>&nbsp; &nbsp; while length(trim(tpRtn))&gt;0 do begin<br>&nbsp; &nbsp; &nbsp; tpConn.Add(copy(tpRtn,1,pos(';',tpRtn)-1));<br>&nbsp; &nbsp; &nbsp; delete(tpRtn,1,pos(';',tpRtn));<br>&nbsp; &nbsp; end;<br>&nbsp; &nbsp; tpRtn:='in [ODBC] [ODBC;Driver=SQL Server;UID=';<br>&nbsp; &nbsp; tpStr:=tpConn.Values['User ID'];<br>&nbsp; &nbsp; tpRtn:=tpRtn+tpStr+';PWD=';<br>&nbsp; &nbsp; tpStr:=tpConn.Values['Password'];<br>{ &nbsp; &nbsp;if StrisEmpty(tpStr) then &nbsp;//密码为空<br>&nbsp; &nbsp; &nbsp;//进一步判断空的原因是连接未保存密码造成的,<br>&nbsp; &nbsp; &nbsp;//还是本来使用的就是空密码<br>&nbsp; &nbsp; &nbsp; if tpConn.IndexOfName('Password')&lt;0 then<br>&nbsp; &nbsp; &nbsp; &nbsp; tpStr:=''; &nbsp;}<br>&nbsp; &nbsp; tpRtn:=tpRtn+tpStr+';Server=';<br>&nbsp; &nbsp; tpStr:=tpConn.Values['Data Source'];<br>&nbsp; &nbsp; tpRtn:=tpRtn+tpStr+';DataBase=';<br>&nbsp; &nbsp; tpStr:=tpConn.Values['Initial Catalog'];<br>&nbsp; &nbsp; tpRtn:=tpRtn+tpStr+';]';<br>&nbsp; finally<br>&nbsp; &nbsp; tpConn.Free;<br>&nbsp; end;<br>&nbsp; Result:=tpRtn;<br>end;<br><br>只要连接SQL2000与ACCESS的两个ADO连接正确,保证能正确复制
 
levi说的很详细,只是通过两个ado的连接复制表我已经实现了,我希望用sql代码来实现该功能而已
 
经过GetSqlConn转换后得到的就是SQL语句代码啊,你可以显示一下Result的内容,就可以知道语句怎么写了。
 
不成,还是提示同样的错误:<br>&nbsp;无法启动应用程序。工作组信息文件丢失,或是已被其它用户以独占方式打开
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
I
回复
0
查看
467
import
I
I
回复
0
查看
611
import
I
顶部