id不从ExecuteNonQuery()返回;

时间:2013-02-08 08:50:32

标签: c# asp.net vsto

在这里我需要创建一个DataTable,但它不会返回一个id.There没有错误。实际上我在这里提供了一个电子邮件地址(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

1 个答案:

答案 0 :(得分: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);
                }
            }
        }
    }
}