将字符串参数发送到存储过程
本文关键字:存储过程 字符串 参数 | 更新日期: 2023-09-27 18:33:22
我正在研究连接到SQL数据库(MySQL 2008 asp.net C#项目。这是我的 C# 类:
public static DataTable GetUnitByName(string unit)
{
Database db = DatabaseFactory.CreateDatabase();
string sqlCommand = "GetUnitByName";
DbCommand dbCommandWrapper = db.GetStoredProcCommand(sqlCommand);
db.AddInParameter(dbCommandWrapper, "@Unit", DbType.String, unit);
DataSet ds = db.ExecuteDataSet(dbCommandWrapper);
DataTable dt = ds.Tables[0];
return dt;
}
这是存储过程:
alter PROCEDURE [dbo].[GetUnitByName]
(
@Unit nvarchar
)
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON
DECLARE @Err int
-- Insert statements for procedure here
select * from Unit where Name=@Unit
SET @Err = @@Error
RETURN @Err
END
我像这样称呼该过程:DataTable unit1 = Unit.GetUnitByName(per100gText[1] enter code here
);,每 10gText 是一个字符串列表。但它返回空数据表。与SQL Server的连接,在调试中,我看到该过程无法读取参数。请提供任何帮助。
如果你想让
proc返回参数,你需要首先将其声明为输出参数
alter PROCEDURE [dbo].[GetUnitByName]
(
@Unit nvarchar , @err int output
)
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON
-- Insert statements for procedure here
select * from Unit where Name=@Unit
SET @Err = @@Error
RETURN @Err
END