C#如何从MySQL数据库Wampserver读取数据
我正试图使用此方法从Wampserver读取表,但收到一条错误消息“在建立与SQL Server的连接时发生与网络相关或特定于实例的错误。找不到或无法访问该服务器。请验证实例名称是否正确,以及SQL Server是否配置为允许远程连接。”。(提供程序:命名管道提供程序,错误:40-无法打开到SQL Server的连接) 当我ping localhost时,所有ping都被接收到。这个代码正确吗C#如何从MySQL数据库Wampserver读取数据,c#,sql,wampserver,C#,Sql,Wampserver,我正试图使用此方法从Wampserver读取表,但收到一条错误消息“在建立与SQL Server的连接时发生与网络相关或特定于实例的错误。找不到或无法访问该服务器。请验证实例名称是否正确,以及SQL Server是否配置为允许远程连接。”。(提供程序:命名管道提供程序,错误:40-无法打开到SQL Server的连接) 当我ping localhost时,所有ping都被接收到。这个代码正确吗 private void button5_Click(object sender, Event
private void button5_Click(object sender, EventArgs e)
{
SqlConnection myConnection = new SqlConnection("user id=root;" +
"password=pass;server=localhost;" +
"database=database; " +
"connection timeout=10");
string query = "select * from table";
SqlCommand cmd = new SqlCommand(query, myConnection);
myConnection.Open(); // the error
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(tabelsql);
myConnection.Close();
da.Dispose();
}
如果您使用的是WampServer,那么这意味着您使用的是MySQL,对吗 MySQL和SQL Server不一样。
SQLConnection
、SQLCommand
和SQLDataAdapter
用于连接到SQL Server(微软的RDBMS),而不是MySQL
要从.NET访问MySQL数据库,您可以使用。如果您使用的是WampServer,那么这意味着您使用的是MySQL,对吗 MySQL和SQL Server不一样。
SQLConnection
、SQLCommand
和SQLDataAdapter
用于连接到SQL Server(微软的RDBMS),而不是MySQL
要从.NET访问MySQL数据库,您可以使用。SqlCommand和SqlDataAdapter是MS SQL ADO.NET本机客户端的一部分,只能用于MS SQL Server。WAMP似乎包括MySQL。为此,您可能需要使用找到的MySQL ADO.NET驱动程序。并提供了一些使用D读取MySQL数据的示例代码AtReader。SqlCommand和SqlDataAdapter是MS SQL ADO.NET本机客户端的一部分,只能用于MS SQL Server。WAMP似乎包含MySql。为此,您可能希望使用找到的MySql ADO.NET驱动程序。并提供一些示例代码,用于使用DataReader读取MySql数据。从m位于wamp服务器中的MySql数据库。 如果wamp服务器位于本地主机中
Add reference ..
using MySql.Data.MySqlClient;
And after this..
write below public partial class this connection query..
MySqlConnection cn = new
MySqlConnection
("server=localhost;database=database;userid=root;password=;charsetutf8;");
write this GetData() in your form load event or below InitializeComponent...
private void GetData()
{
cn.Open();
MySqlDataAdapter adp = new MySqlDataAdapter("SELECT * from
tablename", cn);
DataTable dt = new DataTable();
adp.Fill(dt);
dataGridViewName.DataSource = dt;
adp.Dispose();
cn.Close();
}
从wamp服务器中的MySql数据库中读取单个表。 如果wamp服务器位于本地主机中
Add reference ..
using MySql.Data.MySqlClient;
And after this..
write below public partial class this connection query..
MySqlConnection cn = new
MySqlConnection
("server=localhost;database=database;userid=root;password=;charsetutf8;");
write this GetData() in your form load event or below InitializeComponent...
private void GetData()
{
cn.Open();
MySqlDataAdapter adp = new MySqlDataAdapter("SELECT * from
tablename", cn);
DataTable dt = new DataTable();
adp.Fill(dt);
dataGridViewName.DataSource = dt;
adp.Dispose();
cn.Close();
}
WampServer使用MySQL,使用什么连接到MySQL?@JaneAbrams我添加了到MySQL连接器的链接WampServer使用MySQL,使用什么连接到MySQL?@JaneAbrams我添加了到MySQL连接器的链接