C#中如何调用存储过程
小弟用如下C#代码调用之后的存储过程,为什么总提示
异常详细信息:
System.Data.SqlClient.SqlException: 'read_user' 附近有语法错误。
---------------------------
SqlConnection myConn = new SqlConnection(sqlString);
SqlCommand myCmd = new SqlCommand("read_user", myConn);
myCmd.Parameters.Add("@vvfe", SqlDbType.NVarChar).Value = "user_name";
myConn.Open();
SqlDataReader myReader = myCmd.ExecuteReader();
----SQL---------------------
ALTER PROCEDURE [dbo].[read_user] (
@vvfe nvarchar(50)
)
as
BEGIN
Declare @Sql nVarchar(1000)
Set @Sql='select ' + @vvfe + ' from userinfo'
END
---------------------------
由于工作需要我现在急需很快的了解存储过程,不然可能就要失业了,各位大哥帮帮忙,小弟给诸位作揖了。
C#怎么传参数给存储过程?存储过程怎么接收?
存储过程怎么传参数给C#?C#怎么接收?
------解决方案--------------------SqlCommand cmd = new SqlCommand("exec read_user @vvfe = user_name",conn);
cmd.ExecuteReader();
参考:http://blog.54master.com/index.php/586951/viewspace-19909
------解决方案--------------------[code=C#]
/// <summary>
/// 在数据库中写入创建的房间
/// 调用CreatRoom存储过程
/*CREATE PROCEDURE CreatRoom
@Name varchar(50),
@Owner varchar(50)
AS
Insert into OnLineRooms
(R_Name,R_Owner)
values
(@Name,@Owner)
select @@rowcount
GO
*/
/// </summary>
/// <param name= "RName "> 房间名 </param>
/// <param name= "ROwner "> 房主名 </param>
/// <returns> 成功 ture </returns>
public bool CreatRoom(string RName, string ROwner)
{
OpenDB();
using (SqlCommand com = new SqlCommand( "CreatRoom ", _conn))
{
com.CommandType = System.Data.CommandType.StoredProcedure;//你的程序中没有指定是存储过程
SqlParameter paramRName = new SqlParameter( "@Name ", SqlDbType.VarChar, 50);
paramRName.Value = RName;
com.Parameters.Add(paramRName);
SqlParameter paramROwner = new SqlParameter( "@Owner ", SqlDbType.VarChar, 50);
paramROwner.Value = ROwner;
com.Parameters.Add(paramROwner);
if (com.ExecuteNonQuery() == 1)
return true;
else
return false;
}
}
[/code]
------解决方案--------------------C# code
/* CREATE PROCEDURE GetHost
@RName varchar(80),
//返回的参数
@Host varchar(80) output
AS
select @Host = R_Owner from OnLineRooms where R_Name = @RName
GO
*/
/// <summary>
/// 返回房主名
/// </summary>
/// <param name="rname">房间名</param>
/// <returns>房主名:string</returns>
public string GetHost(string rname)
{
OpenDB();
using (SqlCommand com = new SqlCommand("GetHost", _conn))
{
com.CommandType = CommandType.StoredProcedure;
SqlParameter paramName = new SqlParameter("@RName", SqlDbType.VarChar, 80);
paramName.Value