我已將此連接信息添加到與views文件夾相同的文件級別的Webconfig中,以及視圖文件夾中的一個。它也在Web.Debug.config中。如何將asp.net mvc連接到非本地mySQL數據庫?
<connectionStrings>
<add name="MySqlServer"
connectionString="Datasource=foo.bar.net;Database=testasp;uid=Slendy;pwd=istall;"
providerName="MySql.Data.MySqlClient"/>
</connectionStrings>
後來才知道有這個功能,我複製從如何做的崩潰對adapter.Fill(DS),因爲個MySqlException的粘貼「無法連接到任何指定的MySQL主機。」當我打開/數據/信息/ 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
它不應該是任何比這更難:\ –
這就是我的想法。也許我沒有編輯正確的machine.config文件。在我的電腦上搜索了86個結果。 –
oy。在web.config和web.debug.config之間,這些東西可能會很痛苦。在解決方案資源管理器中找到它否則,我希望我有什麼補充。這是一個例行的問題,而且你沒有做任何明顯錯誤的事情。 –