SQL查询结果变量?

时间:2017-08-31 11:08:12

标签: c# sql oracle

我通常在VB中执行此操作并且更容易,这是我第一次使用C#而我无法弄明白。我只是想读一行,检查它是否有行,并将该列中存在的数字分配给StockQuan

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

上无效

1 个答案:

答案 0 :(得分:3)

尝试以下

 StockQuan = Convert.ToInt32(cmd.ExecuteScalar());