LINQ Stored Procedure
Have the followin开发者_Python百科g Stored procedure for a search form. I use Linq and have problem to get the result of this procedure....am I in the wrong way in procedure or in linq?
You can see my linq down here too..I have tried different ways..but now i need your help :)
ALTER PROCEDURE [dbo].[SearchPostit]
(
@message varchar(1000)='',
@writer varchar(50)='',
@mailto varchar(100)='',
@date varchar(50)=''
)
AS
SELECT P.Message AS Postit, P.Mailto AS 'Mailad till', P.[Date] AS Datum, U.UserName AS Användare
FROM PostIt P
LEFT OUTER JOIN [User] U ON P.UserId=U.UserId
WHERE P.message LIKE '%'+@message+'%' AND U.UserName LIKE '%'+@writer+'%' AND P.Mailto LIKE '%'+@mailto+'%'
AND P.Date LIKE '%'+@date+'%'
GROUP BY P.Message, U.UserName, P.Mailto, P.[Date]
System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Select, true)]
public DataSet SearchPostit(string message, string writer, string mailto, string date)
{
var dc = new LinqClassesDataContext();
List<SearchPostitResult> p = dc.SearchPostit(message, writer, mailto, date).ToList();
DataTable dt = new DataTable();
dt.Columns.Add("Message", typeof(string));
dt.Columns.Add("UserName",typeof (string));
dt.Columns.Add("E-mail", typeof(string));
dt.Columns.Add("Date",typeof(DateTime));
foreach(SearchPostitResult res in dc.SearchPostit(message, writer, mailto, date))
{
DataRow row = dt.NewRow();
row["Message"] = res.Postit;
row["UserName"] = res.User;
row["E-mail"] = res.Mailto;
row["Date"]=res.Date;
dt.Rows.Add(row);
}
DataSet ds = new DataSet();
ds.Tables.Add(dt);
return ds;
}
}
Without knowing much about the error, the only thing I can guess (and this is a total guess) is that it looks like the PostIt.Date field in the database isn't a DateTime, and you're trying to put it into a DateTime field in a DataTable. I'm not sure that works.
As a side point, why would you need the overhead of creating a datatable anyway? What's wrong with the collection Linq to Sql is giving back to you?
精彩评论