gpt4 book ai didi

c# - SQL查询结果到变量?

转载 作者:太空宇宙 更新时间:2023-11-03 22:54:40 25 4
gpt4 key购买 nike

我通常在 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();

上无效

最佳答案

尝试跟随

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

关于c# - SQL查询结果到变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45980018/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com