我通常在VB中做到这一点,它更容易,这是我第一次在C#中,我无法弄清楚。我只是想读取一行,检查其是否具有行和分配中存在的该列StockQuan
SQL查询结果给变量?
public int StockQuan;
public int NewAmount;
public const string ConnectionStringToORACLE = "Provider=OraOLEDB.Oracle.1;Data Source=Souarce;User ID=user;Password=pass;";
public OleDbDataReader rowread;
public OleDbDataReader Read_quan(string txtPartNo, int txtAmount)
{
var conn = new OleDbConnection(ConnectionStringToORACLE);
conn.Open();
string query = "select QUANTITY_IN_STOCK from MPCS.SHOP_INV_STOCK where SI_KEY = ? AND LOCATION = ?";
var cmd = new OleDbCommand(query, conn);
cmd.Parameters.AddWithValue("SI_KEY", txtPartNo);
cmd.Parameters.AddWithValue("LOCATION", "HQ-TC");
//rowread = cmd.ExecuteReader();
//rowread.Read();
//if (rowread.HasRows)
//{
//while (rowread.Read())
//{
StockQuan = (int)cmd.ExecuteScalar();
//}
NewAmount = StockQuan - txtAmount;
//}
//else
//{
// MessageBox.Show("Error reading quantity. Check with Administrator", "Error", MessageBoxButton.OK, MessageBoxImage.Warning);
//}
return rowread;
}
我注释掉大部分事物的数量,只是简化我要去哪里错了,现在错误是(int)cmd.ExecuteScalar();
会发生什么,如果你运行上面的代码并没有匹配的记录(即返回0行)? – mjwills
_您执行SQL的方式确实有效,但您可以考虑使用轻量级的ORM,例如https://github.com/StackExchange/Dapper。它可能会让你的生活变得更轻松一些._ – mjwills
DB中QUANTITY_IN_STOCK的类型是什么? –