-1

我想c#看看数据库是否table等于我要求它等于打开一个特定的表单这是我尝试做的一种方式

void getsformat()
    {
        string constring = @"server=host;userid=user;password=pass";
        string Query = "select colum from table where callsign= '" + listBox1.SelectedItem + "'";
        MySqlConnection conDataBase = new MySqlConnection(constring);
        MySqlCommand cmdDataBase = new MySqlCommand(Query, conDataBase);
        MySqlDataReader myReader;

        try
        {
            conDataBase.Open();
            myReader = cmdDataBase.ExecuteReader();
            while (myReader.Read())
            {
                string sfName = myReader.GetString("sformat");
              }  
            if (sfName == "stworld")
            {
                sendtext = textBox1.Text;
                Form3 f3 = new Form3();
                 f3.Location = new Point(this.Top);

                f3.ShowDialog();  
            }
            else if (sfName == "seperate")
            {
                sendtext = textBox1.Text;
                Form3 f4 = new Form3();
                f4.Location = new Point(this.Top);

                f4.ShowDialog();  
            }


            conDataBase.Close();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }

    }

以及在“if”语句之后使用“while myreader”结束引用的另一种方式

void getsformat()
    {
        string constring = @"server=host;userid=user;password=pass";
        string Query = "select colum from table where callsign= '" + listBox1.SelectedItem + "'";
        MySqlConnection conDataBase = new MySqlConnection(constring);
        MySqlCommand cmdDataBase = new MySqlCommand(Query, conDataBase);
        MySqlDataReader myReader;

        try
        {
            conDataBase.Open();
            myReader = cmdDataBase.ExecuteReader();
            while (myReader.Read())
            {
                string sfName = myReader.GetString("sformat");

            if (sfName == "stworld")
            {
                sendtext = textBox1.Text;
                Form3 f3 = new Form3();
                f3.Location = new Point(this.Top);

                f3.ShowDialog();  
            }
            else if (sfName == "seperate")
            {
                sendtext = textBox1.Text;
                Form3 f4 = new Form3();
                 f4.Location = new Point(this.Top);

                f4.ShowDialog();  
            }

            }
            conDataBase.Close();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }

    }

我不能让他们中的任何一个工作。任何帮助将不胜感激

4

2 回答 2

0

在您的选择语句中将其更改为

listbox1.selecteditem.tostring();
于 2015-04-14T11:23:45.693 回答
0

您似乎没有正确设置连接字符串。这个:

string constring = @"server=host;userid=user;password=pass";

应该有相关资料

string constring = @"server=localhost\SQL2008;userid=myusername;password=mypass";
于 2015-04-14T10:06:45.847 回答