知道为什么一个赋值语句有效而另一个不行吗?

本文关键字:另一个 有效 赋值语句 一个 为什么 | 更新日期: 2023-09-27 18:15:14

我的代码:

string strSQL = "SELECT * from tMedia where SKU = '" + SKU + "'";
FbCommand command = new FbCommand(strSQL, databaseConn);
if (databaseConn.State == ConnectionState.Closed)
    databaseConn.Open();
FbDataReader data = command.ExecuteReader();
data.Read();  //  only one row is returned
//  assignment to "x" is empty (277?)
string x = (string)data["ProductType"].ToString();
//  find product type and set flag for later testing
//  obviously, these don't work either!
if (data["ProductType"].ToString().Contains("Video "))
    videoFormat = true;
else if (data["ProductType"].ToString().Contains("Music: "))
    audioFormat = true;
//  coProductType.Text assignment is correct
coProductType.Text = data["ProductType"].ToString();

知道为什么一个赋值语句有效而另一个不行吗?

也许您需要处理当某人输入无效SKU并且没有返回任何数据行时将发生的问题。

由于唯一的区别是转换为string,因此似乎合理的第一步是删除它。

现在可以工作了…如果有人感兴趣,这里是代码…我不明白为什么将对象移到方法外部使其工作。如果有人能给我点化一下,我将不胜感激。

    string mediaFormat = "";
    bool video;
    bool audio;
    //---------------------------    populate the detail panel    ---------------------|
    private int PopulateDetailPanel(string SKU) {
        decimal convertedMoney;
        clearDetailPanel();  //  clear out old stuff
        //  now, find all data for this SKU
        if (databaseConn.State == ConnectionState.Closed)
            databaseConn.Open();
        string strSQL = "SELECT * from tMedia where SKU = '" + SKU + "'";
        FbCommand command = new FbCommand(strSQL, databaseConn);
        //  find product type and set flag for later testing
        FbDataReader data = command.ExecuteReader();
        data.Read();  //  only one row is returned
        coProductType.Text = data["ProductType"].ToString();  //  while we're here, might as well set it now
        mediaFormat = data["ProductType"].ToString();
        if (mediaFormat.Substring(0, 6) == "Video ")
            video = true;
        else if (mediaFormat.Substring(0, 7) == "Music: ")
            audio = true;