首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > .NET >

Linq to sql如何调用存储过程?

2012-01-07 
Linq to sql怎么调用存储过程??Linq to sql怎么调用存储过程??下面这个哪里出现了错误?--存储过程create P

Linq to sql怎么调用存储过程??
Linq to sql怎么调用存储过程??
下面这个哪里出现了错误?
--存储过程
create PROCEDURE [dbo].getuserinfo
@name varchar(20)
AS
BEGIN  
SELECT id,[name],object,score,[time] FROM userscore WHERE [name] != @name --存在多条记录
END

--linq to sql
[Function(Name="dbo.getuserinfo")]
public ISingleResult<userscore> getuserinfo([Parameter(DbType="VarChar(20)")] string name)
{
IExecuteResult result = this.ExecuteMethodCall(this, ((MethodInfo)(MethodInfo.GetCurrentMethod())), name);
return ((ISingleResult<userscore>)(result.ReturnValue));
}

--方法
 /// <summary>
  /// LINQ to SQL存储过程【单一结果集】
  /// </summary>
  /// <param name="name"></param>
  /// <returns></returns>
  public static ISingleResult<Model.userscore> getuserinfo2(string name)
  {
  Model.UserScoreDataContext context = null;
  ISingleResult<Model.userscore> result =null;
  try
  {
  context = new Model.UserScoreDataContext();
  result = context.getuserinfo(name);
   
  }
  catch
  {
  }
  finally
  {
  if (context != null)
  {
  context.Dispose();
  }
  } 
  return result;

  }

--
 [WebMethod]
  public List<int> getuserinfo2(string name)
  {
  List<int> list = new List<int>(0);
  int result = 0;
  ISingleResult<Model.userscore> results=DAL.Class1.getuserinfo2(name);


  foreach (Model.userscore userscore in results)
  {
  result = userscore.id;
  list.Add(result);

  }
  return list;
  }

调试出现错误: 未将对象引用设置到对象的实例。


另外哪位能不能给我提供点在.NET调用存储过程的示例,如LINQ to SQL存储过程之多个可能形状的单一结果集,LINQ to SQL存储过程之多个结果集,LINQ to SQL存储过程之带输出参数?
以及linq to sql的教程?

[解决办法]
每次看到把linq to sql 和存储过程搅在一起,
就特别郁闷!!!!
你们到底知不知道linq to sql是什么!!!
都用linq拉,
还要存储过程干什么???
[解决办法]
这种说法有一定道理啊
但是有些复杂的语句用存储过程肯定强
[解决办法]
首先你知道怎么试用Linq 吧?

在项目里添加一个Linq to SQL类,然后在服务器资源管理器里面加载你的数据库,把你的存储过程 拖到 .dbml里,现在你就可以调用你的存储过程了,以我创建的StudentMangerDataContext类为例,方法如下:

C# code
StudentMangerDataContext db = new StudentMangerDataContext();var Data = from s in db.sp_SelectStudent()  select s;
[解决办法]
5楼回答的很详细了

你把存储过程拖到右边那个框里(默认右边是接受存储过程的)

然后使用时就跟使用方法是一样的

热点排行