在*.aspx文件中如何使用存储过程?

yusuan 2003-08-24 08:20:31
存储过程代码如下:

--在向MailList表添加记录时,检查是否有重复的记录存在
CREATE proc Check_MailList
@Email nvarchar(60),
@RecordCount bit OUTPUT
As
Select @RecordCount = (Select Count(Email) From MailList Where Email=@Email)
GO

在页面中要实现这样的功能,通过Email地址变量查找数据库中的记录,如果为1则显示已经有这条记录,添加失败;如果为0则添加新记录。
请问这样的功能在*.aspx文件中语句如何写?
...全文
76 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
blackcatiii 2003-08-24
  • 打赏
  • 举报
回复
sorry,I forget to set the CommandType property :(

cmd.CommandType = CommandType.StoredProcedure;
blackcatiii 2003-08-24
  • 打赏
  • 举报
回复
write the codes like below (I prefer to the code-behind file .aspx.cs):

SqlConnection conn = new SqlConnection(strConnect); //get the connection object
SqlCommand cmd = new SqlCommand("Check_MailList",conn);
SqlParameter para = new SqlParameter("@Email",SqlDbType.NVarChar);
para.Value = txtMail.Text;
cmd.Parameters.Add(para);//define the parameters of the command object
para = new SqlParameter("@RecordCount",SqlDbType.Bit);
para.Direction = ParameterDirection.Output;
cmd.Parameters.Add(para);
try
{
conn.Open();//open the connection
cmd.ExecuteNonQuery();//execute the stored procedure
if ((bool)para["@RecordCount"])//the output value can be got now
{
//exist the record,do nothing
}
else
{
//do insert operation
}
}
catch(Exception x)
{
//do something to handle the exception
throw(x); //continue throwing the exception
}
finally
{
//do some cleanning work
if (conn.State == ConnectionState.Open)
{
conn.close();
}
}
cmsoft 2003-08-24
  • 打赏
  • 举报
回复
SqlCommand cmd = new SqlCommand(procName,Conn);
cmd.CommandType = CommandType.StoredProcedure;
Oldman 2003-08-24
  • 打赏
  • 举报
回复
Like normal sql

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧