知道为什么一个assign语句有效而另一个没有?

时间:2011-11-17 20:50:29

标签: c# .net sql firebird2.1

这是我的代码:

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

3 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

由于唯一的区别是强制转换为字符串,所以似乎合理的第一步就是删除它。无论如何,它不应该是必要的。

答案 2 :(得分:0)

它现在正在工作......如果有人感兴趣,这里是代码...我不明白为什么将对象移动到方法之外使其工作。如果有人能够启发我,我会非常感激。

    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;