SQL C#存储过程插入到

SQL C#存储过程插入到,c#,asp.net,sql-server,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, dP

我想在数据库Aktiendepot中的tblPositions表的新行中插入值。当我运行它时,什么都不会发生。传递给方法的值应该是正确的

aspx.cs

protected void StockBuy_Click(object sender, EventArgs e)
{
    Methods CustomMethods = new Methods();
    CustomMethods.BuyStock(sSymbol, sCompany, sExchange, iQuantity, dPrice, sUsername);
}
方法.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)

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

    catch(SqlException ex)
    {
        //Error
    }
上面是您的
catch
块,其中没有捕获错误。可能产生的错误在
catch
块中被抑制


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

    catch(SqlException ex)
    {
        //Error
    }
上面是您的
catch
块,其中没有捕获错误。可能产生的错误在
catch
块中被抑制

试试这段代码

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());

}
试试这个代码

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());

}

你的
StockBuy
定义是什么?看起来不错。请提供StockBuy SP.@SonerGönül补充道,你是否调试了你的代码,并逐行查看发生了什么事?@Orlando:请在问题中详细说明。我一点也不清楚你到底是什么意思。你的
StockBuy
定义是什么?看起来不错。请提供StockBuy SP.@SonerGönül补充道,你是否调试了你的代码,并逐行查看发生了什么事?@Orlando:请在问题中详细说明。我一点也不清楚你到底是什么意思。你的
StockBuy
定义是什么?看起来不错。请提供StockBuy SP.@SonerGönül补充道,你是否调试了你的代码,并逐行查看发生了什么事?@Orlando:请在问题中详细说明。我一点也不清楚你到底是什么意思。@Orlando还是同一期?@Orlando还是同一期?@Orlando还是同一期?