1

在这里我需要创建一个DataTable,但它没有返回一个ID。没有错误。实际上我在这里提供了一个电子邮件地址(epost)。我需要从数据库返回ID。

编码部分在这里。

public string GetAllMails()
    {
        DataConnection dc = new DataConnection();

        string e="";
        int uid=-1,sid=0;

        DataTable dt = new DataTable();
        dt.Columns.Add("id");
        dt.Columns.Add("uid");
        dt.Columns.Add("email");

        Microsoft.Office.Interop.Outlook.Application oApp;
        Microsoft.Office.Interop.Outlook._NameSpace oNS;
        Microsoft.Office.Interop.Outlook.MAPIFolder oFolder;
        Microsoft.Office.Interop.Outlook._Explorer oExp;

        oApp = new Microsoft.Office.Interop.Outlook.Application();
        oNS = (Microsoft.Office.Interop.Outlook._NameSpace)oApp.GetNamespace("MAPI");
        oFolder = oNS.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderInbox);
        oExp = oFolder.GetExplorer(false);
        oNS.Logon(Missing.Value, Missing.Value, false, true);

        Microsoft.Office.Interop.Outlook.Items items = oFolder.Items;

        SqlConnection conn = dc.SqlConnection;
        if (conn.State != ConnectionState.Open) conn.Open();
        SqlCommand cmd = new SqlCommand("GetDetailsByEmail", conn);
        cmd.CommandType = CommandType.StoredProcedure;
        SqlParameter param = new SqlParameter();

        foreach (Microsoft.Office.Interop.Outlook.MailItem mail in items)
        {
            if (mail.UnRead == true)
            {
                e=mail.SenderEmailAddress;

               // param   <-- add sql parameters
                cmd.Parameters.Add(new SqlParameter("@epost",e));

                uid = cmd.ExecuteNonQuery();

                //send to db and take the id (uid)

                //if returned id
                DataRow row = dt.NewRow();

                row["id"] = sid;
                sid++;

                row["uid"] = uid;// uid

                row["email"] = e;

                dt.Rows.Add(row);

            }
        }

        return e;
    }

存储过程在这里

ALTER Procedure [dbo].[GetDetailsByEmail]

@epost nvarchar(50)

AS
BEGIN
Select Medlems_ID,[E-post]
from dbo.Members
where [E-post] = @epost
END

当我编辑我的编码时,uid 是一个 int。

uid = cmd.ExecuteReader(); or uid = cmd.ExecuteScalar(); // it shows me conversion error.here i put as a comment
4

1 回答 1

9

如果您阅读文档,ExecuteNonQuery您将看到以下行:

针对连接执行 Transact-SQL 语句并返回受影响的行数。

你需要用来SqlCommand.ExecuteReader做你想做的事。

编辑:

以下是根据您的评论您的代码的外观。您的 sproc 返回 2 列但您只对 1 列感兴趣,这有点奇怪。此外,您是否期望多行也不清楚。

if (dc.SqlConnection.State != ConnectionState.Open) 
    dc.SqlConnection.Open();

using (var cmd = new SqlCommand("GetDetailsByEmail", dc.SqlConnection))
{
    cmd.CommandType = CommandType.StoredProcedure;

    foreach (Microsoft.Office.Interop.Outlook.MailItem mail in items)
    {
        if (mail.UnRead)
        {
            e = mail.SenderEmailAddress;

            //this is a little nasty
            if (cmd.Parameters.Count > 0)
                cmd.Parameters.RemoveAt(0);

            cmd.Parameters.AddWithValue("@epost", e);

            using (var reader = cmd.ExecuteReader())
            {
                while (reader.Read())
                {
                    uid = reader.GetInt32(0);

                    DataRow row = dt.NewRow();
                    row["id"] = sid;
                    sid++;
                    row["uid"] = uid;// uid
                    row["email"] = e;
                    dt.Rows.Add(row);
                }
            }
        }
    }
}
于 2013-02-08T08:53:35.697 回答