lotuscript:有关连接 SQL DB 的一些问题

发布于 2024-10-16 09:21:58 字数 2838 浏览 1 评论 0原文

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

我要在我的 SQL 数据库上添加一个“附件”字段,并且我想在正文末尾添加附件。

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

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

提前致谢。

    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

I've got this code that is working... I read from an MS SQL database some rows from a table and then send an email for each row.

I'm about to add an "attachment" field, on my SQL database and I'd like to add the attachment at the end of my body.

I have two questions: 1) what datatype should I use on MS SQL? (Binary field, maybe) and 2) if someone else has some example code, I'd really appreciate it.

A bonus question: on a more advanced version of this script, i first run by all my results from my resultset to get the IDs from the messages and then update their status on the MS SQL table.
Then I try and run by the same resultset again, to actually perform the sending....
Somehow, on the second run, I'm having trouble starting from row 1, using the same code than bellow... any advice on what's the best way to do that?: My requirement is that I have to run twice by the same resultset. :)

Thanks in advance.

    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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

千笙结 2024-10-23 09:21:58

这行不是

result.NextRowcode

应该是: 吗

result.NextRow

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

Wasn't this line:

result.NextRowcode

supposed to be:

result.NextRow

?

I don't know about MSSQL, but in DB2 we routinely use Blob/Clob binary data type to store any type of files.
I hear that many MSSQL experts recommend rather storing files on file system with only their path and file name in a DB.

枉心 2024-10-23 09:21:58

好的,这是我的 Java ScriptLibrary 中的简短代码,它使用 JDBC 连接到 DB2 并执行查询(您只需要导入数据库的 jar 并使用 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 Server 结合使用:
http://support.microsoft.com/kb/313100

OK, here's the abbreviated code from my Java ScriptLibrary that uses JDBC to connect to DB2 and execute a query (you would only need to import your db's jar-s and use com.microsoft.sqlserver.jdbc.SQLServerDriver for 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;
    }
}

To check out the examples of using LS2J to use Java classes directly from your LotusScript code, download this great LS2J samples database from Julian Robichaux:
http://www.nsftools.com/tips/NotesTips.htm#ls2jexamples

And here's a good link to start with, explains how to use JDBC with MS SQL server:
http://support.microsoft.com/kb/313100

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文