1

我有这个正在工作的代码......我从 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
4

2 回答 2

0

不是这条线:

result.NextRowcode

应该是:

result.NextRow

?

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

于 2011-03-05T09:52:22.447 回答
0

好的,这是我的 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

于 2011-03-09T11:29:12.270 回答