Dim sr1 As String
Dim cslj As New adodb.Connection
sr1 = "EXEC master..xp_cmdshell 'bcp" & """ SELECT * FROM " & a & ".dbo.PM_StandTbl""" & "  queryout  " & temp & "StandTbl.txt -c -S" & """" & c & """" & " -U" & """flying""" & " -P" & """123""" & "'"
cslj.Execute sr1这是一个数据导出的语句,为什么在运行的时候没有报错,而却找不到结果。是不是sql语句没有取出数据呢?单步跟踪可以执行到!请各位高手帮忙看看,,谢谢!

解决方案 »

  1.   

    你用 ?sql  輸入出到 Immediate window  看一下是否正確。
      

  2.   

    这是所有的代码:Option Explicit
    Dim sql As String
    Dim Cslj As New ADODB.Connection
    'Dim tr As New ADODB.Recordset
     Dim Xtsjljc As String
    Dim tr As Recordset
    Dim temp, n, t1, sr, sr1, a, c, d, e As StringPrivate Sub Form_Load()
       
        
        Text1.Text = Dir1.Path
        temp = Text1.Text
        n = Form_user.Text2(2).Text
        d = Form_user.Text2(1).Text
        e = Form_user.Text2(0).Text    Xtsjljc = "Provider=SQLOLEDB.1;"    Xtsjljc = Xtsjljc + "Persist Security Info=False;"    Xtsjljc = Xtsjljc + "Data Source=" + Trim(n) + ";"    Xtsjljc = Xtsjljc + " Initial Catalog=" + "Master" + ";"
    '    Text1.Text = Xtsjljc
        Cslj.Open Xtsjljc, e, d
        
        
    End SubPrivate Sub Dir1_Change()
        Text1.Text = Dir1.Path
        temp = Text1.Text
    End SubPrivate Sub Drive1_Change()    t1 = Drive1.Drive
        Dir1.Path = t1
            
    End SubPrivate Sub CancelButton_Click()
        Cslj.Close
        Unload Me
    End SubPrivate Sub OKButton_Click()    If Cw_DataEnvi.DataConnect.State = 1 Then Cw_DataEnvi.DataConnect.Close
            Cw_DataEnvi.DataConnect.Open Xtsjljc, e, d
        
        sql = "select * from HDsystem_Databases where number='0001 '"
        Set tr = Cw_DataEnvi.DataConnect.Execute(sql)
        
        
        If Not tr.EOF Then
            a = tr.Fields("DataBasesName")
            c = tr.Fields("servername")
        End If    If Right(temp, 1) = "\" Then
            temp = temp
        Else
            temp = temp & "\"
        End If
        
        sr1 = "EXEC master..xp_cmdshell 'bcp" & """ SELECT * FROM " & a & ".dbo.PM_StandTbl""" & "  queryout  " & temp & "StandTbl.txt -c -S" & """" & c & """" & " -U" & """flying""" & " -P" & """123""" & "'"
        Set tr = Cw_DataEnvi.DataConnect.Execute(sr1)
        tr.Close
        sr1 = "EXEC master..xp_cmdshell 'bcp" & """ SELECT * FROM " & a & ".dbo.PM_StandTblName""" & "  queryout  " & temp & "StandTblName.txt -c -S" & """" & c & """" & " -U" & """flying""" & " -P" & """123""" & "'"
        Set tr = Cw_DataEnvi.DataConnect.Execute(sr1)
        tr.Close
        sr1 = "EXEC master..xp_cmdshell 'bcp" & """ SELECT * FROM " & a & ".dbo.PM_Formula""" & "  queryout  " & temp & "Formula.txt -c -S" & """" & c & """" & " -U" & """flying""" & " -P" & """123""" & "'"
        Set tr = Cw_DataEnvi.DataConnect.Execute(sr1)
        tr.Close
        MsgBox ("数据导出已完成!")
        
    End Sub
      

  3.   

    上面是我后来用的另一种方法,但是也不行。以下是最初的代码::Option Explicit
    Dim sql As String
    Dim Cslj As New ADODB.Connection
    Dim tr As New ADODB.Recordset
     Dim Xtsjljc As String
    'Dim tr as Recordset
    Dim temp, n, t1, sr, sr1, a, c, d, e As StringPrivate Sub Form_Load()
       
        
        Text1.Text = Dir1.Path
        temp = Text1.Text
        n = Form_user.Text2(2).Text
        d = Form_user.Text2(1).Text
        e = Form_user.Text2(0).Text    Xtsjljc = "Provider=SQLOLEDB.1;"    Xtsjljc = Xtsjljc + "Persist Security Info=False;"    Xtsjljc = Xtsjljc + "Data Source=" + Trim(n) + ";"    Xtsjljc = Xtsjljc + " Initial Catalog=" + "Master" + ";"    Cslj.Open Xtsjljc, e, d
        
        
    End SubPrivate Sub Dir1_Change()
        Text1.Text = Dir1.Path
        temp = Text1.Text
    End SubPrivate Sub Drive1_Change()    t1 = Drive1.Drive
        Dir1.Path = t1
            
    End SubPrivate Sub CancelButton_Click()
        Cslj.Close
        Unload Me
    End SubPrivate Sub OKButton_Click()    Dim tr As New ADODB.Recordset
        sql = "select * from HDsystem_Databases where number='0001 '"    tr.Open sql, Cslj
        
        If Not tr.EOF Then
            a = Trim(tr.Fields("DataBasesName"))
            c = Trim(tr.Fields("servername"))
        End If
        If Right(temp, 1) = "\" Then
            temp = temp
        Else
            temp = temp & "\"
        End If
        
        sr1 = "EXEC master..xp_cmdshell 'bcp" & """ SELECT * FROM " & a & ".dbo.PM_StandTbl""" & "  queryout  " & temp & "StandTbl.txt -c -S" & """" & c & """" & " -U" & """flying""" & " -P" & """123""" & "'"
        Cslj.Execute sr1
        sr1= "EXEC master..xp_cmdshell 'bcp" & """ SELECT * FROM " & a & ".dbo.PM_StandTblName""" & "  queryout  " & temp & "StandTblName.txt -c -S" & """" & c & """" & " -U" & """flying""" & " -P" & """123""" & "'"
        cslj.Execute sr1
        sr1= "EXEC master..xp_cmdshell 'bcp" & """ SELECT * FROM " & a & ".dbo.PM_Formula""" & "  queryout  " & temp & "Formula.txt -c -S" & """" & c & """" & " -U" & """flying""" & " -P" & """123""" & "'"
        cslj.Execute sr1    tr.Close
        MsgBox ("数据导出已完成!")
        
    End Sub
      

  4.   

    你在什么地方导出?在什么地方找的?是在自己机子上还是server上?如果server和本机不是同一台,是不是看看server那边 有没有 StandTbl.txt 我看不懂你的程序呵呵,乱猜得
      

  5.   

    我在我的机上导的,在我的机上和server上都找不到。在查询分析器里执行可以在server上找得到,在我机上是没有的。