2

これは単純な解決策であるはずですが、Visual Studio 2012 では、sqlCon はフィールドですが、タイプのように使用され、Textbox1 と同じエラーが発生するというエラーが表示されます...アセンブリ参照または適切な接続インポートが欠落している可能性がありますか? このシンプルなルートを続けていきたいと思います。

    MySqlConnection sqlCon = new MySqlConnection("Server=***;Port=***;Database=***;Uid=***;Pwd=***;");
    MySqlCommand commandText = new MySqlCommand ("SELECT count(Dues) From Students");
        sqlCon.CommandText = "SELECT * count(Dues) FROM Students";
        sqlCon.Connection = sqlCon;
        TextBox1.Text = sqlCon.ExecuteScalar().ToString();
4

2 に答える 2

4
  • 接続を開く
  • 使用usingステートメント
  • Try-catchブロックを使用

スニペット、

string connStr = "Server=***;Port=***;Database=***;Uid=***;Pwd=***;";
string query = "SELECT count(Dues) From Students";
using(MySqlConnection sqlCon = new MySqlConnection(connStr))
{
    using(MySqlCommand sqlComm = new MySqlCommand())
    {
        sqlComm.Connection = sqlCon;
        sqlComm.CommandText = query;

        try
        {
            sqlCon.Open();
            TextBox1.Text = sqlComm.ExecuteScalar().ToString();
        }
        catch(MySqlException ex)
        {
            MessageBox.Show(ex.ToString());
        }
    }
}
于 2012-12-27T06:33:34.000 に答える
1

MySqlConnection sqlCon = new MySqlConnection("Server=***;Port=***;Database=***;Uid=***;Pwd=***;");
MySqlCommand commandText = new MySqlCommand ("SELECT count(Dues) From Students");

//sqlCon is of type MySqlConnection which is derived from DbConnection
sqlCon.CommandText = "SELECT * count(Dues) FROM Students";

//sqlCon has no Connection property, and why are you even assigning sqlCon to that property
sqlCon.Connection = sqlCon;

//ofcourse this will fail
TextBox1.Text = sqlCon.ExecuteScalar().ToString();

あなたが達成しようとしていることは次のとおりだと思います:

MySqlConnection sqlCon = new MySqlConnection("Server=***;Port=***;Database=***;Uid=***;Pwd=***;");
MySqlCommand command = new MySqlCommand ("SELECT count(Dues) From Students");

try
{
  sqlCon.Open();
  command.Connection = sqlCon;
  TextBox1.Text = command.ExecuteScalar().ToString();
}
finally
{
  sqlCon.Close();
}
于 2012-12-27T07:06:19.043 に答える