【问题标题】:lotuscript: some questions about connecting to a SQL DBlotusscript:关于连接到 SQL DB 的一些问题
【发布时间】:2011-02-07 15:43:28
【问题描述】:

我的代码正在运行...我从 MS SQL 数据库中读取了表中的一些行,然后为每一行发送一封电子邮件。

我即将在我的 SQL 数据库中添加一个“附件”字段,我想在正文末尾添加附件。

我有两个问题:1) 我应该在 MS SQL 上使用什么数据类型? (可能是二进制字段)和 2)如果其他人有一些示例代码,我将不胜感激。

一个额外的问题:在此脚本的更高级版本上,我首先运行我的结果集中的所有结果以从消息中获取 ID,然后在 MS SQL 表上更新它们的状态。 然后我尝试再次运行相同的结果集,以实际执行发送.... 不知何故,在第二次运行时,我从第 1 行开始遇到麻烦,使用与下面相同的代码......关于什么是最好的方法的任何建议?:我的要求是我必须运行两次相同结果集。 :)

提前致谢。

    Option Public 
    Uselsx "*LSXODBC"

    Sub Initialize
    Dim session As New NotesSession
    Dim db As NotesDatabase
    Dim doc As NotesDocument
    Dim subject As String, cc As String, bcc As String, sender As String, OID As String, mailto As String, bodyNotMIME As String
    Dim body As NotesMIMEEntity


    On Error Goto errorCounter

    Set db = session.CurrentDatabase

    Gosub SendMailGeneral

    Exit Sub

    SendMailGeneral:
    Dim con As New ODBCConnection
    Dim qry As New ODBCQuery
    Dim result As New ODBCResultSet
    Dim defaultQuery As String
    Set qry.Connection = con    
    con.SilentMode = True
    If con.ConnectTo("DSN_Name","USER_NAME", "PASSWORD") Then
        Set result.Query = qry
        defaultQuery = "select TOP (10)  * from Message  where StatusType=0"
        qry.SQL = defaultQuery      
            result.Execute
            If (result.IsResultSetAvailable) Then
                Do


  result.NextRow

                Gosub GetRowFields

                Gosub SendMail

            Loop Until result.IsEndOfData
        End If
        End If
        result.Close(DB_CLOSE)  
        Return
        End Sub


    GetRowFields:
        mailto = result.GetValue("To")
        cc = result.GetValue("CC")
        bcc = result.GetValue("Bcc")
        sender = result.GetValue("Sender")
        subject = result.GetValue("Subject")
        bodyNotMIME = result.GetValue("Body")               
        OID = result.GetValue("OID")

        Return


    SendMail:
            Dim mail As NotesDocument
            Set mail = New NotesDocument(db)
            Dim stream As NotesStream
        Set stream = session.CreateStream

    'Recipients 
        mail.SendTo = mailto
        mail.CopyTo = cc
        mail.BlindCopyTo = bcc

    ' Set all sender-related fields 
        mail.ReplyTo = sender
        mail.Principal = sender
        mail.From = sender
        mail.AltFrom = sender
        mail.SendFrom = sender
        mail.INetFrom = sender
        mail.tmpDisplaySentBy = sender
        mail.tmpDisplayFrom_Preview = sender
        mail.DisplaySent = sender 

    'Body   

        Call stream.WriteText(bodyNotMIME)
        Set body = mail.CreateMIMEEntity
        Call body.SetContentFromText _
        (stream, "text/html; charser=iso-8859-1", ENC_NONE)

    'Subject    
        mail.Subject = subject

    'Send

        Call mail.Send(False, False)

        Return

【问题讨论】:

    标签: sql odbc lotusscript


    【解决方案1】:

    这行不是吗:

    result.NextRowcode
    

    应该是:

    result.NextRow
    

    ?

    我不了解 MSSQL,但在 DB2 中,我们通常使用 Blob/Clob 二进制数据类型来存储任何类型的文件。
    我听说许多 MSSQL 专家建议宁愿将文件存储在文件系统上,只在数据库中存储它们的路径和文件名。

    【讨论】:

    • 感谢您的帖子。 +code 是一个错字。我刚刚修好了。
    • 您好,感谢您的回复。 +NextRowcode 是一个错字,刚刚修复它:)。关于将文件存储在文件系统上,问题是我有一些限制:Notes 服务器无法访问除本地文件系统之外的任何其他文件系统,并且生成消息的应用程序无权访问 Notes Server 的文件系统。我想我必须将文件存储在 MS SQL Blobl/Clob 等价物上——我相信它被称为二进制类型——并祈祷我能从笔记中正确读取它——我过去遇到过一些问题,有一些数据类型——。还是谢谢!
    • @frenetix 你有没有想过在 Lotus 中操作 MsSql 中的文件的方法?我认为您必须使用 LotusScript 中的 ADODB.Stream 或使用 Java agent。如果我必须这样做,我会使用 Java。
    • 听起来有点吓人 :) 感谢 ADODB.Stream 提示,我会尝试阅读一些内容。关于使用 Java 代理:以前从来没有做过,我所有的 Notes 编码都在 LotusScript 上,我会试一试。谢谢!
    • 我最近做了一些 Java 代理,当时我不得不连接到 DB2 并提取一些数据并在 Java 小程序中创建图形。在这里,您可以找到有关如何直接从 LotusScript 代码中使用 Java 类的信息:publib.boulder.ibm.com/infocenter/domhelp/v8r0/topic/…
    【解决方案2】:

    好的,这是我的 Java ScriptLibrary 中的缩写代码,它使用 JDBC 连接到 DB2 并执行查询(您只需要导入 db 的 jar-s 并使用 com.microsoft.sqlserver.jdbc.SQLServerDriver 用于 MS SQL):

    import java.sql.*;
    import com.ibm.db2.jcc.DB2Driver;
    
    public class DB2Connection {
        protected String server;
        protected String port;
        protected String dbName;
        protected String userdb2;
        protected String pwddb2;
        protected java.sql.Connection con;
    
    
        public DB2Connection( String srv, String port, String db, String user, String pass ){
            this.server = srv;
            this.port = port;
            this.dbName = db;
            this.userdb2 = user;
            this.pwddb2 = pass;
    
            connectDB2();
        }
    
    
        public void connectDB2() {
            try {
                Class.forName("com.ibm.db2.jcc.DB2Driver"); // .newInstance();
                String url = "jdbc:db2://" + server + ":" + port + "/" + dbName;
                con = DriverManager.getConnection(url, userdb2, pwddb2);
                System.out.println("Connection to DB2 succeded");
    
            } catch (Exception e) {
                System.out.println("Error connecting DB2 Server") ;
                e.printStackTrace();
            }
        }
    
        protected ResultSet executeQuery( String queryString ) throws SQLException, Exception {
            System.out.println( "Preparing query:\t" + queryString );
            ResultSet rs = null;
            try {
                Statement stmt = con.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
                rs = stmt.executeQuery(queryString);
    
            } catch (SQLException sqle) {
                String error = ("SQLException : Could not execute query");
                throw new SQLException(error);
            } catch (Exception e) {
                String error1 = ("Exception : An Unknown error occurred.");
                throw new Exception(error1);
            }
            return rs;
        }
    
        protected int executeCountQuery( StringBuffer queryStr ){
            System.out.println( "Preparing query:\t" + queryStr );
            try {
                ResultSet rs = executeQuery( queryStr.toString( ) );
                rs.next();  //only one row in result set
                return rs.getInt(1);
    
            } catch (SQLException sqle) {
                System.out.println("SQLException: Could not get ResultSet from DB2Connection.executeQuery");
                sqle.printStackTrace();
            } catch (Exception e) {
                System.out.println("Exception : An Unknown error occurred while calling.");
                e.printStackTrace();
            }
            return 0;
        }
    
        protected int executeCountQuery( PreparedStatement ps ){
            //System.out.println( "Preparing prepared statement - query:\t" );  //+ ps.getQuery( ) );
            try {
                ResultSet rs = ps.executeQuery( );
                rs.next();  //only one row in result set
                return rs.getInt(1);
    
            } catch (SQLException sqle) {
                System.out.println("SQLException: Could not get ResultSet from DB2Connection.executeQuery");
                sqle.printStackTrace();
            } catch (Exception e) {
                System.out.println("Exception : An Unknown error occurred while calling.");
                e.printStackTrace();
            }
            return 0;
        }
    
    
        public Connection getConnection(){
            return con;
        }
    }
    

    要查看使用 LS2J 直接从 LotusScript 代码中使用 Java 类的示例,请从 Julian Robichaux 下载这个出色的 LS2J 示例数据库:
    http://www.nsftools.com/tips/NotesTips.htm#ls2jexamples

    这是一个很好的开始链接,解释了如何将 JDBC 与 MS SQL 服务器一起使用:
    http://support.microsoft.com/kb/313100

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-11-03
      • 2010-12-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-10
      • 2012-06-02
      相关资源
      最近更新 更多