如何连接到ASP.NET中的mysql数据库? [英] How to connect to mysql db in ASP.NET ?
问题描述
我尝试用这种方式使用mysql连接到数据库,但是当我运行代码时...... Response.Write根本不会向网站写任何内容。
I tried it this way to connect to a database using mysql , however when i run the code...Response.Write does not write anything at all to the website.
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
}
我的尝试:
************ ************************************************** ************************************************** ************************************************** ************************************************** ****** *************************************
What I have tried:
***************************************************************************************************************************************************************************************************************************************************************
推荐答案
阅读这篇文章,它对你的问题很有帮助
如何使用C#连接MySQL [ ^ ]
Read this article , it is helpful regarding your Question
How to Connect to MySQL Using C#[^]
这篇关于如何连接到ASP.NET中的mysql数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!