C# 通过C连接MySQL服务器#
这是我的代码,用于连接MySQL服务器并获取一些信息:C# 通过C连接MySQL服务器#,c#,mysql,C#,Mysql,这是我的代码,用于连接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 conn
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 改用文档。例如,从
请注意,由于MySQL和SQL Server都实现了ADO.NET接口,因此可以将其作为
conn
的类型(相同的原则适用于其他变量)。您正在尝试连接到Microsoft SQL Server
改用文档。例如,从
请注意,由于MySQL和SQL Server都实现了ADO.NET接口,因此您可以将其设置为
conn
(相同的原则适用于其他变量)。您正在尝试使用Microsoft SQL Server驱动程序连接到MySQL服务器(仅用于连接到MS SQL Server)
从下载并安装MySQL连接器,文档将帮助您开始使用Microsoft SQL server驱动程序连接MySQL服务器(仅用于连接MS SQL server)
从下载并安装MySQL连接器,这些文档将帮助您开始您无法连接到SqlClient,因为它仅用于SQL Server。您需要安装MySQL连接器或通过ODBC进行连接 下面是MySQL连接器的示例:
您无法连接SqlClient,因为它仅用于SQL Server。您需要安装MySQL连接器或通过ODBC进行连接 下面是MySQL连接器的示例:
SqlConnection类不是DB连接的基类;其目的是连接到MS SQL Server DBs。您需要的是基本的OLEDB连接或ODBC连接;如果您的MySql实例支持OleDb,我会使用它。SqlConnection类不是DB连接的基类;其目的是连接到MS SQL Server DBs。您需要的是基本的OLEDB连接或ODBC连接;如果您的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
}
}
}
}
}
我必须这样做才能将数据从SQLServer导入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
}
}
}
}
}