注册 登录
编程论坛 SQL Server论坛

求助!!存储过程出错!

LoveGood 发布于 2008-11-19 10:33, 1245 次点击
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go





ALTER PROCEDURE [dbo].[AddProduct]
    @ProductName varchar(20),
    @Category char(20),
    @ProductRate money,
    @Brand char(20),
    @ProductQoh smallint,
    @ProductImgPath varchar(50),
    @ProductDescription varchar(250),

         @CategoryId int,    -- 为局部变量(不会声明)
    @BrandId int        -- 为局部变量
    
AS
         
    select @CategoryId=(select cCategoryId from Category where cCategoryName=@Category)
    select @BrandId=(select cBrandId from BrandId where cBrandName=@Brand)

BEGIN
    SET NOCOUNT ON;
    Insert into Product(vProductName,cCategoryId,mProductRate,cBrandId,siProductQoh,vProductImgPath,vProductDescription)
    values(@ProductName,@CategoryId ,@ProductRate,@BrandId,@ProductQoh,@ProductImgPath,@ProductDescription)
END



语言是c#,是这样传递的
string sql = string.Format("AddProduct'{0}','{1}','{2}','{3}','{4}','{5}','{6}'", pName, pPcategory, pRate, pBrand, pAmount, pImgPath, pDescription);

bc.ExecSQL(sql);


我自定了一个类bc ExecSQL方法是
public Boolean ExecSQL(string sQueryString)
    {
        SqlConnection conn = new SqlConnection("Server=localhost;database=MyShop;user id=sa;password=123456");

        conn.Open();
        SqlCommand comm = new SqlCommand(sQueryString, conn);

        try
        {
            comm.ExecuteNonQuery();
            conn.Close();

        }
        catch (System.Exception em)
        {

            conn.Close();
            TetMessage = "连接数据库失败" + em.ToString();
            return false;

        }
        return true;
    }




在线等!!
5 回复
#2
happynight2008-11-19 10:36
把详细的错误提示贴上来
#3
LoveGood2008-11-19 11:27
没有提示出错信息,只是还能写入数据库!
存储过程编译没错,只是不会声明存储过程的局部变量!
#4
wwttxx2008-11-19 11:47
select @CategoryId=(select cCategoryId from Category where cCategoryName=@Category)
    select @BrandId=(select cBrandId from BrandId where cBrandName=@Brand)


SQL好像不能这么传递值,可以动态生成一个SQL语句来执行这个查询
#5
球球2008-11-19 12:23
ALTER PROCEDURE [dbo].[AddProduct]
    @ProductName varchar(20),
    @Category char(20),
    @ProductRate money,
    @Brand char(20),
    @ProductQoh smallint,
    @ProductImgPath varchar(50),
    @ProductDescription varchar(250)
AS
BEGIN
    SET NOCOUNT ON;
    --声明变量
    DECLARE @CategoryId int,    -- 为局部变量(不会声明)
            @BrandId int        -- 为局部变量

    --给变量赋值
    SELECT @CategoryId = cCategoryId FROM Category
        WHERE cCategoryName = @Category
    SELECT @BrandId = cBrandId FROM BrandId
        WHERE cBrandName = @Brand

    INSERT INTO Product
    (
        vProductName,
        cCategoryId,
        mProductRate,
        cBrandId,
        siProductQoh,
        vProductImgPath,
        vProductDescription
    )
    VALUES
    (
        @ProductName,
        @CategoryId ,
        @ProductRate,
        @BrandId,
        @ProductQoh,
        @ProductImgPath,
        @ProductDescription
    )
END

[[it] 本帖最后由 球球 于 2008-11-19 12:27 编辑 [/it]]
#6
LoveGood2008-11-24 15:14
thank you!!!
1