简体   繁体   中英

SQL C# stored procedure insert into

I want to insert values into a new line in the table tblPositions in my database Aktiendepot. Nothing happens when I run it. The values that are passed to the method should be fine.

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

Here is how I created the table

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

Here is the stored procedure

    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)

Keep breakpoints and check the execution flow of your codes.

    catch(SqlException ex)
    {
        //Error
    }

Above is your catch block where in which there is not capturing of error.Maybe the error produced is getting suppressed in the catch block.

在此处输入图片说明

Try this code

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

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM