-2
私はこの方法でMySQLを使用してデータベースに接続しようとしましたが、コードを実行するとResponse.Write
はウェブサイトに何も書き込まれません。ASP.NETでMySQLデータベースに接続する方法は?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MySql.Data.MySqlClient;
public partial class _Default : System.Web.UI.Page
{
private MySqlConnection connection;
private string server;
private string database;
private string uid;
private string password;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
// if it is not a post back method, page renders for first time
{
connectdb();
}
}
private void connectdb()
{
server = "localhost";
database = "first_db";
uid = "root";
password = "";
string connectionString;
connectionString = "SERVER=" + server + ";" + "DATABASE=" +
database + ";" + "UID=" + uid + ";" + "PASSWORD=" + password + ";";
connection = new MySqlConnection(connectionString);
try
{
connection.Open();
Console.ReadKey();
Response.Write("connection opened");
}
catch (MySqlException ex)
{
//When handling errors, you can your application's response based
//on the error number.
//The two most common error numbers when connecting are as follows:
//0: Cannot connect to server.
//1045: Invalid user name and/or password.
switch (ex.Number)
{
case 0:
Response.Write("Cannot connect to server. Contact administrator");
break;
case 1045:
Response.Write("Invalid username/password, please try again");
break;
}
}
} //end of connectdb
}
デバッグを試したことがありますか。 – mason