SQL C#存储过程插入

时间:2015-06-02 06:07:44

标签: c# asp.net sql-server

我想在我的数据库Aktiendepot中的表tblPositions中将值插入一个新行。我跑的时候什么都没发生。传递给方法的值应该没问题。

aspx.cs

protected void StockBuy_Click(object sender, EventArgs e)
{
    Methods CustomMethods = new Methods();
    CustomMethods.BuyStock(sSymbol, sCompany, sExchange, iQuantity, dPrice, sUsername);
}

Methods.cs

public void BuyStock(string sSymbol, string sCompany, string sExchange, int iQuantity, double dPrice, string sUsername) //Inserts stock information to database
{
        SqlConnection con = new SqlConnection("user id=admin;" +
                                          "password=1337passwort;server=localhost;" +
                                          "database=Aktiendepot; " +
                                          "connection timeout=30"); //Establishes Connection
        SqlCommand InsertStockInformation = new SqlCommand("StockBuy", con);
        InsertStockInformation.CommandType = CommandType.StoredProcedure;
        SqlParameter quantity = new SqlParameter("@quantity", SqlDbType.Int, 5,iQuantity.ToString());
        quantity.Direction = ParameterDirection.Input;
        InsertStockInformation.Parameters.Add(quantity);
        SqlParameter symbol = new SqlParameter("@symbol", SqlDbType.VarChar, 50, sSymbol);
        symbol.Direction = ParameterDirection.Input;
        InsertStockInformation.Parameters.Add(symbol);
        SqlParameter company = new SqlParameter("@company", SqlDbType.VarChar, 30, sCompany);
        company.Direction = ParameterDirection.Input;
        InsertStockInformation.Parameters.Add(company);
        SqlParameter exchange = new SqlParameter("@exchange", SqlDbType.VarChar, 20, sExchange);
        exchange.Direction = ParameterDirection.Input;
        InsertStockInformation.Parameters.Add(exchange);
        SqlParameter buymktprice = new SqlParameter("@buymktprice", SqlDbType.Float, 50, dPrice.ToString());
        buymktprice.Direction = ParameterDirection.Input;
        InsertStockInformation.Parameters.Add(buymktprice);
        SqlParameter username = new SqlParameter("@username", sUsername);
        username.Direction = ParameterDirection.Input;
        InsertStockInformation.Parameters.Add(username);
        con.Open();
        InsertStockInformation.ExecuteNonQuery();
        con.Close();
}

以下是我创建表格的方法

create table tblPositions (PosID int NOT NULL identity(1,1), PosQuantity int NOT NULL, PosSymbol varchar(10) NOT NULL, PosCompany varchar(30) NOT NULL, PosExchange varchar(20) NOT NULL,
                       PosBuyMktPrice float NOT NULL, PosBuyDate date NOT NULL, FK_PosUsername varchar(50) NOT NULL foreign key references tblUsers(UserUsername),
                       primary key (PosID));

这是存储过程

    ALTER PROCEDURE [dbo].[StockBuy]
(
@username as varchar(50),
@quantity as int,
@symbol as varchar(50),
@company as varchar(30),
@exchange as varchar(20),
@buymktprice as float
)
AS
INSERT INTO tblPositions (PosQuantity,PosSymbol,PosCompany,PosExchange,PosBuyMktPrice,PosBuyDate,FK_PosUsername) VALUES (@quantity,@symbol,@company,@exchange,@buymktprice,GETDATE(),@username)

2 个答案:

答案 0 :(得分:0)

保持断点并检查代码的执行流程。

    catch(SqlException ex)
    {
        //Error
    }

以上是您的catch块,其中没有捕获错误。可能产生的错误是在catch块中获得抑制

enter image description here

答案 1 :(得分:0)

试试此代码

try
     {
       SqlConnection con = new SqlConnection("user id=admin;" +
       "password=1337passwort;" +"server=localhost;" +
        "database=Aktiendepot; " +
         "connection timeout=30"); //Establishes Connection
     SqlCommand InsertStockInformation = new SqlCommand("StockBuy", con);
     InsertStockInformation.CommandType = CommandType.StoredProcedure;
     InsertStockInformation.Parameters.Add("@quantity",SqlDbType.Int).Value=quantity;
     InsertStockInformation.Parameters.Add("@symbol",SqlDbType.NVarChar,50).Value=sSymbol;
     InsertStockInformation.Parameters.Add("@company",SqlDbType.NVarChar,30).Value=sCompany;
     InsertStockInformation.Parameters.Add("@exchange",SqlDbType.NVarChar,20).Value=sExchange;
     InsertStockInformation.Parameters.Add("@buymktprice",SqlDbType.Float,50).Value=dPrice;
      InsertStockInformation.Parameters.Add("@username",SqlDbType.NVarChar,50).Value=sUsername;

con.Open();
 InsertStockInformation.ExecuteNonQuery();


con.Close();
 }
 catch(SqlException ex)
{
  MessageBox.Show(ex.ToString());

}