Jane Abrams
Jane Abrams

Reputation: 35

C# How to Read from MySQL database Wampserver

I am trying to read a table from a Wampserver using this, but I get an error message "A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)"

When I ping localhost all the pings are received. Is this code correct?

    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();
    }

Upvotes: 2

Views: 12584

Answers (3)

Lalit
Lalit

Reputation: 61

To Read a single table from MySql database which is in wamp server. If wamp-server is in localhost then,

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();
    }

Upvotes: 1

Ken
Ken

Reputation: 844

SqlCommand and SqlDataAdapter are part of the MS SQL ADO.NET native client and can only be used for MS Sql Server. WAMP appears to include MySql. For that, you'll likely want to use the MySql ADO.NET driver found here. And this article provides some sample code for reading MySql data utilizing a DataReader.

Upvotes: 1

Dennis Traub
Dennis Traub

Reputation: 51654

If you're using a WampServer than that means you're using MySQL, right?

MySQL and SQL Server are not the same. SQLConnection, SQLCommand and SQLDataAdapter are used to connect to SQL Server (the RDBMS from Microsoft), not MySQL.

To access a MySQL database from .NET you can use the MySQL Connector.

Upvotes: 2

Related Questions