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

发布于 2024-12-16 14:56:01 字数 775 浏览 6 评论 0原文

这是我的代码:

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

Here's my code:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

浴红衣 2024-12-23 14:56:01

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

Maybe you need to deal with the problem that will occur when someone enters an invalid SKU and NO data rows are returned.

由于唯一的区别是转换为字符串,因此合理的第一步似乎是删除它。无论如何应该没有必要。

Since the only difference is the cast to string, it seems like a reasonable first step would be to remove that. It shouldn't be necessary anyway.

來不及說愛妳 2024-12-23 14:56:01

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

    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;

It's working now... here's the code if anyone is interested... I don't understand why moving the objects to outside of the method made it work. If anyone can enlighten me, I would really appreciate it.

    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;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文