Click here to Skip to main content
15,887,485 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In my web application i am using SQL server 2008 .. but for some purpose i want to check one table in the Mysql database...is it possible to access sql and mysql in one web application.
In web config i had given this connection strings

XML
<connectionStrings>
    <add name="sqlDB" connectionString="Data Source=localhost;Initial Catalog=SQLDatabase;User ID=user1;Password=passwd1" providerName="System.Data.SqlClient" />

 <add name="mysqlDB" connectionString="Data Source=localhost;Initial Catalog=MYSQLDatabase;User ID=user2;Password=passwd2" providerName="MySql.Data.MySqlClient" />


  </connectionStrings>





in my cs file.


public void test()
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["mysqlDB"].ToString());
con.Open();
SqlCommand cmd = new SqlCommand("select * from jobs ", con);

SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
}


but the connection is not establishing..It is showing the error like this


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


How can i solve this ....
Posted
Comments
[no name] 27-Jun-13 9:42am    
You would need to use the MySQL classes to access a MySQL database, not the classes for SQL Server.
ashriv 27-Jun-13 9:42am    
are you trying to access both db simultaneously or One after another?
Member 9492907 27-Jun-13 9:46am    
i want to use both simultaneously..is it possible??

C#
string connStr = "server=localhost;user=root;database=world;port=3306;password=******;";
        MySqlConnection conn = new MySqlConnection(connStr);
        try
        {
            Console.WriteLine("Connecting to MySQL...");
            conn.Open();

            string sql = "SELECT Name, HeadOfState FROM Country WHERE Continent='Oceania'";
            MySqlCommand cmd = new MySqlCommand(sql, conn);
            MySqlDataReader rdr = cmd.ExecuteReader();

            while (rdr.Read())
            {
                Console.WriteLine(rdr[0]+" -- "+rdr[1]);
            }
            rdr.Close();
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.ToString());
        }

        conn.Close();
        Console.WriteLine("Done.");
 
Share this answer
 
Comments
Member 9492907 28-Jun-13 1:21am    
I had added the reference MySql.Data.dll for getting MySql.Data.MySqlClient namespace...But when i run the application it is showing the error like..Mysql reference is not found..what s the solution for this..
Member 9492907 28-Jun-13 2:19am    
I am trying to connect it with the server database not localhost..i had given the ip address for the server name..but it is showing like "Unable to connect to any of the specified MySQL hosts."...
You can do it ,but your above code will work only for sqlserver ,for mysql there are other classes
,you cant do in the same way.
For mysql connection with Asp.Net please follow the link

Connecting to MySQL Database using C# and .NET[^]

Hope it will make you more clear.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900