asp.net调用存储过程
存储过程如下
CREATE procedure Pr_AddFile
@FileName varchar(100),
@FileUrl varchar(200),
@FileKind varchar(100)
AS
Declare @RecordCount as int
SET @RecordCount=(select count(*) FROM File1 WHERE FileName=@FileName)
IF @RecordCount<1
BEGIN
INSERT INTO File1([FileName],FileUrl,FileKind) VALUES (@FileName,@FileUrl,@FileKind)
Return @@Identity
END
GO
配置文件
<appSettings>
<add key="ConnectionString" value="server=wcj;database=files;uid=sa;pwd="/>
</appSettings>
具体实现过程
string strconn = System.Configuration.ConfigurationManager.AppSettings["ConnectionString"].ToString();
try
{
SqlConnection conn = new SqlConnection(strconn);
conn.Open();
SqlCommand cmmd = new SqlCommand();
cmmd.CommandType = CommandType.StoredProcedure; //数据库执行存储过程
cmmd.CommandText = "Pr_AddFile";//存储过程名
cmmd.Connection = conn;
//参数的集合
SqlParameter[] parameters ={
new SqlParameter("@FileName", SqlDbType.VarChar, 100),
new SqlParameter("@FileUrl", SqlDbType.VarChar, 200),
new SqlParameter("@FileKind", SqlDbType.VarChar, 200),
new SqlParameter("@ReturnValue",SqlDbType.Int,4)
};
//为每个参数赋值
parameters[0].Value = sFileName.ToString();
parameters[1].Value = sFileUrl.ToString();
parameters[2].Value = sFileKind.ToString();
parameters[3].Direction = ParameterDirection.ReturnValue;
foreach (SqlParameter parameter in parameters)
{
cmmd.Parameters.Add(parameter);
}
//执行存储过程
cmmd.ExecuteNonQuery();
int parameterFileID = int.Parse(cmmd.Parameters["@ReturnValue"].Value.ToString());//返回参数
return parameterFileID;
}
catch (Exception ex)
{
throw new Exception("数据库连接失败!", ex);
}
finally
{
}