首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

有哪位大神知道这个代码的异常?异常提示必须声明标量变量@MyProductID

2012-12-20 
有哪位大神知道这个代码的错误?错误提示必须声明标量变量@MyProductIDnamespace zmf_test1{class Program{

有哪位大神知道这个代码的错误?错误提示必须声明标量变量@MyProductID
namespace zmf_test1
{
    class Program
    {
        static void Main(string[] args)
        {
            SqlConnection dataConnection = new SqlConnection();
            try
            {
                SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder();
                //  builder.DataSource = ".\\SQLExpress";
                builder.DataSource = "maggie-PC";
                builder.InitialCatalog = "Northwind";
                builder.IntegratedSecurity = true;
                dataConnection.ConnectionString = builder.ConnectionString;
                dataConnection.Open();

                Console.Write("Please enter a ProductName (less than 2 characters): ");
                string productName = Console.ReadLine();

                SqlCommand dataCommand = new SqlCommand();
                dataCommand.Connection = dataConnection;
                dataCommand.CommandType = CommandType.Text;
                dataCommand.CommandText =
                    "CREATE PROCEDURE AddProduct  @MyProductID int OUTPUT,   @MyProductName nvarchar(40),   @MySupplierID int,   @MyCategoryID int,  @MyQuantityPerUnit nvarchar(20), @MyUnitPrice money,  @MyUnitsInStock smallint,   @MyUnitsOnOrder smallint,    @MyReorderLevel smallint,     @MyDiscontinued bit" +
                    " AS INSERT INTO Products (  ProductName, SupplierID, CategoryID, QuantityPerUnit,  UnitPrice, UnitsInStock, UnitsOnOrder, ReorderLevel,  Discontinued) "+
                    " VALUES (  @MyProductName, @MySupplierID, @MyCategoryID, @MyQuantityPerUnit, "+
                    "@MyUnitPrice, @MyUnitsInStock, @MyUnitsOnOrder, @MyReorderLevel,"+


                    "@MyDiscontinued   )";
                dataCommand.CommandText =
                    "AddProduct(@MyProductID,@MyProductName, @MySupplierID, @MyCategoryID, @MyQuantityPerUnit, " +
                    "@MyUnitPrice, @MyUnitsInStock, @MyUnitsOnOrder, @MyReorderLevel," +
                    "@MyDiscontinued )";
                dataCommand.CommandText =
                    " SELECT @MyProductID = SCOPE_IDENTITY() "
                    int productId = 78;
                    int supplierId = 2;
                    int categoryId = 3;
                    string quantityPerUnit = "1000ml";
                    Decimal unitPrice =8;
                    int unitinstock = 20;
                    int unitsOnOrder = 6;
                    int ReorderLevel = 10;
                    int discontinued =0;
                SqlParameter param = new SqlParameter("@ProductIdParam", SqlDbType.Int,2);
                param.Value = productId;
                dataCommand.Parameters.Add(param);
                SqlParameter param1 = new SqlParameter("@ProductNameParam", SqlDbType.NChar, 50);
                param1.Value = productName;
                dataCommand.Parameters.Add(param1);
                SqlParameter param2 = new SqlParameter("@SupplierIdParam", SqlDbType.Int, 20);
                param1.Value = supplierId;
                dataCommand.Parameters.Add(param2);


                
                SqlParameter param3 = new SqlParameter("@CategoryIDParam", SqlDbType.Int,20);
                param1.Value =categoryId;
                dataCommand.Parameters.Add(param3);
                SqlParameter param4 = new SqlParameter("@QuantityPerUnitParam", SqlDbType.NChar,20);
                param1.Value =quantityPerUnit;
                dataCommand.Parameters.Add(param4);
                SqlParameter param5 = new SqlParameter("@UnitPriceParam", SqlDbType.Money, 20);
                param1.Value =unitPrice;
                dataCommand.Parameters.Add(param5);
                SqlParameter param6 = new SqlParameter("@ UnitsInStockParam", SqlDbType.SmallInt,20);
                param1.Value =unitinstock;
                dataCommand.Parameters.Add(param6);
                SqlParameter param7 = new SqlParameter("@UnitsOnOrderParam", SqlDbType.SmallInt,20);
                param1.Value =unitsOnOrder;
                dataCommand.Parameters.Add(param7);
                SqlParameter param8 = new SqlParameter("@ ReorderLevelParam", SqlDbType.SmallInt,20);
                param1.Value =ReorderLevel;
                dataCommand.Parameters.Add(param8);
                SqlParameter param9 = new SqlParameter("@DiscontinuedParam", SqlDbType.Bit, 1);
                param1.Value =discontinued;
                dataCommand.Parameters.Add(param9);
                Console.WriteLine("About to find orders for Products {0}\n\n",productName);  
                SqlDataReader dataReader = dataCommand.ExecuteReader();
                Console.ReadLine();
                dataReader.Close();



            }
            catch (SqlException e)
            {
                Console.WriteLine("Error accessing the database: {0}", e.Message);
            }
            finally
            {
                dataConnection.Close();
            }
        }
    }
}

[最优解释]
有output的那些,使用时,要先定一个变量来接收,虽然看的头晕,但是貌似没看到你先定义再执行存储过程。另外你这代码也太》。。。。干嘛不放到SQLServer里面做好了再调用?
[其他解释]
就是做用vs2008操作数据库的练习
[其他解释]

引用:
就是做用vs2008操作数据库的练习
先把你的存储过程建在SQLServer里面调试好在放到vs里面去用
[其他解释]
做出来啦,谢谢啦

热点排行