2013-02-08 108 views
1

在這裏我需要創建一個DataTable,但它不會返回一個id.There沒有錯誤。實際上,在這裏我提供了一個電子郵件地址(epost)。並且我需要從數據庫返回id。ID不從ExecuteNonQuery()返回;

編碼部分到這裏。

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 
+0

'ExecuteNonQuery'用於INSERT,UPDATE,DELETE ...不適用於SELECT語句。使用ExecuteReader()代替 – codingbiz

+0

如果您的查詢始終返回一個值,則可以使用['ExcecuteScalar'](http://msdn.microsoft.com/zh-cn/library/system.data.sqlclient.sqlcommand.executescalar.aspx )。 –

+0

http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.executescalar.aspx – Techmaster

回答

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); 
       } 
      } 
     } 
    } 
} 
+0

現在我改變它像這樣uid = cmd.ExecuteReader();然後它給我錯誤「不能隱式地將類型system.data.sqlclient.sqlreader轉換爲'int'」 –

+0

@JehanPerera我已經用一些基於代碼的示例代碼更新了我的答案,我相信你可以修復它的任何小問題或 –

+0

Thanks for the code.thats correct..But Onething。when loops run 1st item getting without issue.when move to second item it stop with executereader line with this exception「Procedure or function have too too許多參數指定「 –