0

この接続情報を、views フォルダーと同じファイル レベルの Webconfig と、views フォルダーの Webconfig の両方に追加しました。これは Web.Debug.config にもあります。

<connectionStrings>
<add name="MySqlServer"
connectionString="Datasource=foo.bar.net;Database=testasp;uid=Slendy;pwd=istall;"
    providerName="MySql.Data.MySqlClient"/>
 </connectionStrings>

そして、MySqlException「指定されたMySQLホストのいずれにも接続できません」が原因で、アダプターでクラッシュするハウツーからこの関数をコピーして貼り付けました.Fill(ds)。/data/details/1 を開いたとき

//
    // GET: /Data/Details
    public string Details()
    {
        // Get the MySQL connection string stored in the Web.config
        string cnnString = ConfigurationSettings.AppSettings["ConnectionString"];

        // Create a connection object and data adapter
        MySqlConnection cnx = new MySqlConnection(cnnString);
        MySqlDataAdapter adapter = new MySqlDataAdapter();

        // Create a SQL command object
        string cmdText = "select '3232' as ah;";
        MySqlCommand cmd = new MySqlCommand(cmdText, cnx);

        // Set the command type to StoredProcedure
        cmd.CommandType = CommandType.StoredProcedure;

        // Create and fill a DataSet
        DataSet ds = new DataSet();
        adapter.SelectCommand = cmd;
        adapter.Fill(ds);

        return "::::";
    }

私のテストSQLテーブルは、ローカルホストではなく私のウェブサイトにあり、次のとおりです

a   b   c
1   2013-02-02 14:08:53 324
2   2013-02-02 14:08:53 342234

http://dev.mysql.com/doc/refman/5.1/en/connector-net-tutorials-asp-roles.htmlのガイドに従っていましたが、編集する適切なファイルが見つかったかどうかはわかりません。また、自分のコンピューターがアクセスできるように、dreamhost にアクセス許可を設定しました。

更新: cnnString が NULL になります

4

1 に答える 1

0

ConfigurationSettings は ConfigurationManager である必要があり、コード行を次のように設定する必要があることがわかりました

string cnnString = ConfigurationManager.ConnectionStrings["MySqlServer"].ToString(); 

ここで、「MySqlServer」は構成ファイル内の接続文字列名と一致します。

これは良い助けでした: C# Configuration Manager . 接続文字列

于 2013-02-03T22:10:57.100 に答える