[英]SQL C# stored procedure insert into
I want to insert values into a new line in the table tblPositions in my database Aktiendepot. 我想将值插入数据库Aktiendepot中的表tblPositions中的新行中。 Nothing happens when I run it.
当我运行它时,什么也没有发生。 The values that are passed to the method should be fine.
传递给该方法的值应该很好。
aspx.cs aspx.cs
protected void StockBuy_Click(object sender, EventArgs e)
{
Methods CustomMethods = new Methods();
CustomMethods.BuyStock(sSymbol, sCompany, sExchange, iQuantity, dPrice, sUsername);
}
Methods.cs 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. 上面是您的
catch
块,其中没有捕获错误。也许在catch
块中抑制了所产生的错误。
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());
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.