在c#.net应用程序中传递存储过程参数
我有一个存储过程,正在将其连接到我的项目,我想知道如何传入不同的参数类型: 存储过程:在c#.net应用程序中传递存储过程参数,c#,asp.net-mvc,stored-procedures,C#,Asp.net Mvc,Stored Procedures,我有一个存储过程,正在将其连接到我的项目,我想知道如何传入不同的参数类型: 存储过程: [dbo].[UploadAssignment] @studentId int , @guid uniqueidentifier , @originalfilename nvarchar(500) , @uploaddate datetime 在我的项目中: public virtual IEnumerable<T>
[dbo].[UploadAssignment]
@studentId int
, @guid uniqueidentifier
, @originalfilename nvarchar(500)
, @uploaddate datetime
在我的项目中:
public virtual IEnumerable<T> GetUploadStudentSubmission<T>(int studentId, .."How should i format the remaining parameters")
{
SqlCommand _command = new SqlCommand("dbo.UploadAssignment");
_command.CommandType = CommandType.StoredProcedure;
_command.Parameters.Add(new SqlParameter { ParameterName = "studentId",SqlDbType = SqlDbType.Int, Value = sectionId});
_command.Parameters.Add(new SqlParameter { ParameterName = "guid", SqlDbType = SqlDbType.Int, Value = guid });
_command.Parameters.Add(new SqlParameter { ParameterName = "originalfilename", SqlDbType = SqlDbType.Int, Value = originalfilename });
_command.Parameters.Add(new SqlParameter { ParameterName = "uploaddate", SqlDbType = SqlDbType.Int, Value = uploaddate });
}
公共虚拟IEnumerable GetUploadStudentSubmission(int studentId,…“我应该如何格式化其余参数”)
{
SqlCommand _command=newsqlcommand(“dbo.UploadAssignment”);
_command.CommandType=CommandType.storedProcess;
_添加(新的SqlParameter{ParameterName=“studentId”,SqlDbType=SqlDbType.Int,Value=sectionId});
_添加(新的SqlParameter{ParameterName=“guid”,SqlDbType=SqlDbType.Int,Value=guid});
_添加(新的SqlParameter{ParameterName=“originalfilename”,SqlDbType=SqlDbType.Int,Value=originalfilename});
_添加(新的SqlParameter{ParameterName=“uploaddate”,SqlDbType=SqlDbType.Int,Value=uploaddate});
}
国家:
ParameterName以@paramname
的形式指定
因此,您需要在参数名称中包含@
。除此之外,您只需像传递任何其他函数一样传递相关参数,如下所示:
public virtual IEnumerable<T> GetUploadStudentSubmission<T>(
int studentId, Guid guid, string originalfilename, DateTime uploaddate)
{
SqlCommand _command = new SqlCommand("dbo.UploadAssignment");
_command.CommandType = CommandType.StoredProcedure;
_command.Parameters.Add(new SqlParameter { ParameterName = "@studentId",SqlDbType = SqlDbType.Int, Value = sectionId});
_command.Parameters.Add(new SqlParameter { ParameterName = "@guid", SqlDbType = SqlDbType.UniqueIdentifier, Value = guid });
_command.Parameters.Add(new SqlParameter { ParameterName = "@originalfilename", SqlDbType = SqlDbType.NVarChar, Value = originalfilename });
_command.Parameters.Add(new SqlParameter { ParameterName = "@uploaddate", SqlDbType = SqlDbType.DateTime, Value = uploaddate });
}
公共虚拟IEnumerable GetUploadStudentSubmission(
int studentId、Guid、字符串原始文件名、日期时间上载日期)
{
SqlCommand _command=newsqlcommand(“dbo.UploadAssignment”);
_command.CommandType=CommandType.storedProcess;
_添加(新的SqlParameter{ParameterName=“@studentId”,SqlDbType=SqlDbType.Int,Value=sectionId});
_添加(新的SqlParameter{ParameterName=“@guid”,SqlDbType=SqlDbType.UniqueIdentifier,Value=guid});
_添加(新的SqlParameter{ParameterName=“@originalfilename”,SqlDbType=SqlDbType.NVarChar,Value=originalfilename});
_添加(新的SqlParameter{ParameterName=“@uploaddate”,SqlDbType=SqlDbType.DateTime,Value=uploaddate});
}
公共虚拟IEnumerable GetUploadStudentSubmission(int studentId、Guid Guid、字符串原始文件名、日期时间uploaddate)
{
SqlCommand _command=newsqlcommand(“dbo.UploadAssignment”);
_command.CommandType=CommandType.storedProcess;
_command.Parameters.AddWithValue(“@studentId”,sectionId);
_command.Parameters.AddWithValue(“@guid”,guid);
_command.Parameters.AddWithValue(“@originalfilename”,originalfilename);
_command.Parameters.AddWithValue(“@uploaddate”,uploaddate);
}
您还可以使用以下代码:
public virtual IEnumerable<T> GetUploadStudentSubmission<T>(int studentId, Guid guid, string originalfilename, DateTime uploaddate)
{
var command = Database.GetStoredProcCommand("[dbo].[UploadAssignment]");
Database.AddInParameter(command, "studentId", DbType.Int32, studentId);
Database.AddInParameter(command, "guid", DbType.Guid, guid);
Database.AddInParameter(command, "originalfilename", DbType.String, originalfilename);
Database.AddInParameter(command, "uploaddate", DbType.DateTime, uploaddate);
var reader = Database.ExecuteReader(command);
commandText = command.CommandAsSql();
reader.Close();
}
公共虚拟IEnumerable GetUploadStudentSubmission(int studentId、Guid Guid、字符串originalfilename、日期时间uploaddate)
{
var command=Database.GetStoredProcCommand(“[dbo].[UploadAssignment]”);
AddInParameter(命令“studentId”,DbType.Int32,studentId);
AddInParameter(命令,“guid”,DbType.guid,guid);
AddInParameter(命令,“originalfilename”,DbType.String,originalfilename);
AddInParameter(命令,“uploaddate”,DbType.DateTime,uploaddate);
var reader=Database.ExecuteReader(命令);
commandText=command.CommandAsSql();
reader.Close();
}
微软还提供了强大的企业库,除其他功能外,它还包含一个数据访问块和处理参数
查看此答案以了解更多详细信息我建议将您的
\u命令.Parameters.Add
更改为\u命令.Parameters.AddWithValue(“@paramname”,变量)
让数据库处理SqlDataType@DJKRAZE感谢您的提示,但我实际上关心的是在方法中传递参数。感谢您提供答案,但我主要关心的是在方法中传递的参数:“public virtual IEnumerable GetUploadStudentSubmission(int studentId。。“我应该如何设置其余参数的格式”)'
public virtual IEnumerable<T> GetUploadStudentSubmission<T>(int studentId, Guid guid, string originalfilename, DateTime uploaddate)
{
var command = Database.GetStoredProcCommand("[dbo].[UploadAssignment]");
Database.AddInParameter(command, "studentId", DbType.Int32, studentId);
Database.AddInParameter(command, "guid", DbType.Guid, guid);
Database.AddInParameter(command, "originalfilename", DbType.String, originalfilename);
Database.AddInParameter(command, "uploaddate", DbType.DateTime, uploaddate);
var reader = Database.ExecuteReader(command);
commandText = command.CommandAsSql();
reader.Close();
}