在这里我需要创建一个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