C#远程连MYSQL数据库服务器
需要这3个DLL
MySql.Data.dll
MySQLDriverCS.dll
Renci.SshNet.dll
-------------------------------------------------------------------
using MySql.Data.MySqlClient;
using MySQLDriverCS;
using Renci.SshNet;
--------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using 系统数据通信服务器;
using 系统配置;
using 系统文件输入输出;
using 系统运行时接口;
using MySql.Data.MySqlClient;
using MySQLDriverCS;
using Renci.SshNet;
using System.Web;
using System.Net;
在项目上右击鼠标,在右侧的.NET选项卡中选择"System.Configuration"以注册引用,默认情况下Form1会继承父类Form的所有属性和方法,默认情况下Form1会继承父类Form的所有属性和方法
byte[] dat = null;
string b = "";
string fileName = "";
string weixin = "";
public Form1()
{
InitializeComponent();
SSHConnectMySql();
}
public void SSHConnectMySql()
{
string SSHHost = "110.33.201.103"; // SSH地址,也就是服务器IP地址
int SSHPort = 22; // SSH端口,一般默认22
string SSHUser = "root"; // SSH用户名
string SSHPassword = "123"; // SSH密码,这里的密码123是登陆服务器密码
MySQL服务地址被赋值为'127.0.0.1' MySQL服务主机IP地址设置为'127.0.0.1' 指定默认数据库连接端口为3306 数据库连接字符串被构建如下 首先指定数据库名称设为'shops' 然后将数据源路径设为'sqlIPA' 接着指定端口号为3306 再次设定用户名标识符以及密码均为root 最后将字符集配置为UTF-8字符集
定义一个名为 connectionInfo 的 PasswordConnectionInfo 类型变量。
设置该变量的属性值为 SSHHost、SSHPort、SSHUser 和 SSHPassword。
将 connectionInfo 对象的时间超时值设置为 30 分钟。
在一个 try-with-resources 块中:
使用 var 定义一个名为 client 的 SshClient 对象,并将其初始化为 new SshClient(connectionInfo)。
尝试执行 client.Connect() 方法。
如果连接未成功,请显示提示信息 "SSH 连接失败"。
let portFwdL = CreateForwardedPortLocal(sqlIPA, sqlport, sqlHost, sqlport); //将SQL注入请求中的远程端口映射到本地服务器指定的远程端口
client.AddClientForwardedPort(portFwdL);
portFwdL.Start();
if (!client.IsConnected)
{
MessageBox.Show("未成功建立连接,请检查网络配置");
}
MsSqlConnection instance is created by instantiating a new instance of the MsSqlConnection class.
An instance of the MySqlDataAdapter is created by creating an instance of the MySqlDataAdapter.
The MySqlDataAdapter instance configures command using the SQL SELECT statement and the connection object.
开始时,
成功建立了连接。
创建了一个新的数据集。
使用自定义的数据适配器填入了数据集。
检查第一张表中的记录数量是否大于零。
// 将二进制数据转换为字节数组
fileName = 第一张表中第一行第一列的 avatar字段,并将其转换为字符串形式。
weixin = ds.Tables[0].Rows[0]["avatar"].ToString();
fileName = "http://wx.qlogo.cn/mmopen/vi_32/Q0j4TwGTfTKPoje4X1mKC5vqKhJoamPsnvQxRia3iaWIoDTot7s1bLuZVqTKiaXeP1k22jvxuzGQjMn5vregZ/0";
//using System.Web;
using System.Net;
BitMap img = null;
HttpRequest req;
HttpWebResponse res = null;
System.Uri httpUrl = new System.Uri(weixin);
req = (HttpRequest)(WebRequest.Create(httpUrl));
req.Timeout = 180000; // 设置超时值18秒
req.User-Agent = "XXXXX"; // 注意:此处应使用单引号
req.Access-Control-Allow-Origin = "XXXXXX";
req.Method = "GET";
res = (HttpWebResponse)(getRequest().Result);
img = new BitMap(res.GetResponseStream()); // 获取图片流
// img.Save(@"E:/" + DateTime.Now.ToFileTime().ToString() + ".png"); // 随机命名
pictureBox1.Image = new BitMap(img);
// byte[] featureArray = CheckAndMarkFace(this.pictureBox1.Image);
}
}
catch (Exception ee)
{
MessageBox.Show(ee.Message);
}
finally
{
conn.Close();
}
client.Disconnect();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
}
}
