日期:2014-05-17  浏览次数:20476 次

调用存储过程时提示‘存储过程名’附近有语法错误?
我有一段存储过程
ALTER proc [dbo].[GetUsername] 
@guid varchar(50)
as
select Username from Recommended
where GUID = @guid
在.net中调用存储过程
SqlCommand cmd = new SqlCommand("GetUsername", conn);
...
SqlDataReader result = cmd.ExecuteReader();
...
运行时提示“过程或函数‘GetUsername’附近有语法错误”,
如果我直接用语句的话SqlCommand cmd = new SqlCommand("select Username from Recommended where GUID = @guid", conn);就没有问题
请问我的问题出在哪里呀?

------解决方案--------------------
cmd.CommandType = CommandType.StoredProcedure;

------解决方案--------------------
conn.open()
加参数
------解决方案--------------------
你的代码都不全,一种情况是上面他们说的忘了说命令的类型cmd.CommandType = CommandType.StoredProcedure;
还有种是没传参数,@guid 

------解决方案--------------------
你的SqlCommand cmd = new SqlCommand("GetUsername", conn);, CommandText默认是Sql语句。
要上楼上的那样显示 指明类型 cmd.CommandType=CommandType.StoredProcedure;