繁体   English   中英

通过C#连接到MySQL服务器

[英]Connecting to MySQL server via C#

这是我的代码,用于连接到MySQL服务器并获取一些信息:

using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;

namespace test_MLDropCopy
{
    class Program
    {
        static void Main(string[] args)
        {
            string connectionString = "server=xxxxx;uid=xxxxx;pwd=xxxxx";

            SqlConnection mySqlConnection = new SqlConnection(connectionString);

            string selectString = "select Symbol from today_positions.all_rttp where ServiceName like 'ML%' and ID = 137800";

            SqlCommand mySqlCommand = mySqlConnection.CreateCommand();

            mySqlConnection.Open(); 

            mySqlCommand.CommandText = selectString;

            SqlDataReader myReader = null;

            myReader = mySqlCommand.ExecuteReader();

            string Symbol = myReader["Symbol"].ToString();

            Console.WriteLine(Symbol);

            mySqlConnection.Close();


        }
    }
}

但是,这是我得到的错误:

Unhandled Exception: System.Data.SqlClient.SqlException: A network-related or in
stance-specific error occurred while establishing a connection to SQL Server. Th
e server was not found or was not accessible. Verify that the instance name is c
orrect and that SQL Server is configured to allow remote connections. (provider:
 Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)

我已经仔细检查了我使用的登录凭据。 我的代码有问题吗,还是数据库端出现了问题(脱机,权限不足等)?

您正在尝试连接到Microsoft SQL Server。

请改用Connector / Net文档。 例如入门教程

string connStr = "server=localhost;user=root;database=world;port=3306;password=******;";
MySqlConnection conn = new MySqlConnection(connStr);

注意,由于MySQL和SQL Server都实现ADO.NET接口,因此可以将IDbConnectionconn的类型(其他变量也适用相同的原理)。

您正在尝试使用Microsoft SQL Server驱动程序(仅用于连接MS SQL Server)连接到MySQL服务器。

下载并从安装MySQL连接器在这里 ,该文档在这里应该让你开始

您无法与SqlClient连接,因为它仅用于SQL Server。 您需要安装MySQL连接器或通过ODBC连接。

在这里查看MySQL连接器的示例: http : //bitdaddys.com/MySQL-ConnectorNet.html

SqlConnection类不是数据库连接的基类。 它的目的是连接到MS SQL Server数据库。 您需要的是基本的OleDbConnection或OdbcConnection; 如果您的MySql实例支持,我将使用OleDb。

我必须这样做才能将数据从SQL Server导入MySql。 MySql连接(作为根)和数据处理的相关代码为:

using MySql.Data.MySqlClient;
using MySql.Data.Types;

// snip 

    string sourceString = "Server=127.0.0.1;Uid=root;Pwd=password;Database=dbname;";
    string[] tables = { "Table1", "Table2"};

    using (MySqlConnection source = new MySqlConnection(sourceString))
    {
        source.Open();
        foreach (string table in tables)
        {
            using (MySqlCommand readCommand = new MySqlCommand("select * from " + table, source))
           {
               using (MySqlDataReader reader = readCommand.ExecuteReader())
               {
                    while (reader.Read())
                    {
                         // do work here
                    }
               }
           }
       }
   }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM