老大们,ASP.net(C#)连接数据库,添加,删除,只读,更新数据存储过程
救救我吧!
[此贴子已经被作者于2006-6-2 1:03:02编辑过]
如果是MS SQL Server的话,自己参考一下T-SQL语法手册。执行一些SQL语句来完成。
例如下列
使用CREATE PROCEDURE语句创建存储过程。
创建一个名为:bl_GetBlogID的存储过程,该存储过程传递一个参数,传出一个参数。
CREATE PROCEDURE [dbo].[bl_GetBlogID]
-- Add the parameters for the stored procedure here
@BlogID bigint OUTPUT,
@BlogEntryName varchar(50)
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 @BlogID = BlogID
FROM bl_Blogs
WHERE (BlogEntryName = @BlogEntryName)
END
或者使用ALTER PROCEDURE修改一个现有的存储过程:
ALTER PROCEDURE [dbo].[bl_GetBlogID]
-- Add the parameters for the stored procedure here
@BlogID bigint OUTPUT,
@BlogEntryName varchar(50)
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 @BlogID = BlogID
FROM bl_Blogs
WHERE (BlogEntryName = @BlogEntryName)
END
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
使用DROP PROCEDURE删除存储过程
USE [BlogLanHost]
GO
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[bl_GetBlogID]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[bl_GetBlogID]
if(Page.IsValid)
{
SqlCommand cm=new SqlCommand("Addproduct",cn);
cm.CommandType=CommandType.StoredProcedure;
cm.Parameters.Add(new SqlParameter("@Product_id",SqlDbType.Char,10));
cm.Parameters.Add(new SqlParameter("@Product_name",SqlDbType.VarChar,50));
cm.Parameters.Add(new SqlParameter("@Product_descnbe",SqlDbType.VarChar,50));
cm.Parameters["@Product_id"].Value=Tbx_id.Text;
cm.Parameters["@Product_name"].Value=Tbx_name.Text;
cm.Parameters["@Product_descnbe"].Value=tell.Text;
cm.Connection.Open();
try
{
cm.ExecuteNonQuery();
Response.Redirect("product.aspx");
}
catch(SqlException)
{
Lbl_note.Text="添加失败";
Lbl_note.Style["color"]="red";
}
cm.Connection.Close();
}