c#读sql server数据添加到MySQL数据库
using System;
using System.Collections.Generic;
using System.Text;
using Console = System.Console;
using Microsoft.Data.Odbc;
using System.Data;
using System.Data.SqlClient;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
try
{
connSqlserver();
}
catch (OdbcException MyOdbcException)
{
for (int i = 0; i < MyOdbcException.Errors.Count; i++)
{
Console.Write("ERROR #" + i + "\n" +
"Message: " + MyOdbcException.Errors[i].Message + "\n" +
"Native: " + MyOdbcException.Errors[i].NativeError.ToString() + "\n" +
"Source: " + MyOdbcException.Errors[i].Source + "\n" +
"SQL: " + MyOdbcException.Errors[i].SQLState + "\n");
}
}
Console.ReadLine();
}
public void ceshi()
{
try
{
string MyConString = "DRIVER={MySQL ODBC 3.51 Driver};" +
"SERVER=localhost;" +
"DATABASE=jy;" +
"UID=root;" +
"PASSWORD=123;" +
"OPTION=3;CharSet=gb2312;";
OdbcConnection MyConnection = new OdbcConnection(MyConString);
MyConnection.Open();
Console.WriteLine("\n !!! success, connected successfully !!!\n");
//Create a sample table
OdbcCommand MyCommand = new OdbcCommand("DROP TABLE IF EXISTS my_odbc_net", MyConnection);
MyCommand.ExecuteNonQuery();
MyCommand.CommandText = "CREATE TABLE my_odbc_net(id int, name varchar(20), idb bigint)";
MyCommand.ExecuteNonQuery();
//Insert
MyCommand.CommandText = "INSERT INTO my_odbc_net VALUES(10,'" + filter("中国,''") + "', 300)";
Console.WriteLine("INSERT, Total rows affected:" + MyCommand.ExecuteNonQuery()); ;
//Insert
MyCommand.CommandText = "INSERT INTO my_odbc_net VALUES(20,'mysql',400)";
Console.WriteLine("INSERT, Total rows affected:" + MyCommand.ExecuteNonQuery());
//Insert
MyCommand.CommandText = "INSERT INTO my_odbc_net VALUES(20,'mysql',500)";
Console.WriteLine("INSERT, Total rows affected:" + MyCommand.ExecuteNonQuery());
//Update
MyCommand.CommandText = "UPDATE my_odbc_net SET id=999 WHERE id=20";
Console.WriteLine("Update, Total rows affected:" + MyCommand.ExecuteNonQuery());
//COUNT(*)
MyCommand.CommandText = "SELECT COUNT(*) as TRows FROM my_odbc_net";
Console.WriteLine("Total Rows:" + MyCommand.ExecuteScalar());
//Fetch
MyCommand.CommandText = "SELECT * FROM my_odbc_net";
OdbcDataReader MyDataReader;
MyDataReader = MyCommand.ExecuteReader();
while (MyDataReader.Read())
{
if (string.Compare(MyConnection.Driver, "myodbc3.dll") == 0)
{
Console.WriteLine("Data:" + MyDataReader.GetInt32(0) + " " +
MyDataReader.GetString(1) + " " +
MyDataReader.GetInt64(2)); //Supported only by Connector/ODBC 3.51
}
else
{
Console.WriteLine("Data:" + MyDataReader.GetInt32(0) + " " +
MyDataReader.GetString(1) + " " +
MyDataReader.GetInt32(2)); //BIGINTs not supported by Connector/ODBC
}
}
//Close all resources
MyDataReader.Close();
MyConnection.Close();
}
catch (OdbcException MyOdbcException)//Catch any ODBC exception ..
{
for (int i = 0; i < MyOdbcException.Errors.Count; i++)
{
Console.Write("ERROR #" + i + "\n" +
"Message: " + MyOdbcException.Errors[i].Message + "\n" +
"Native: " + MyOdbcException.Errors[i].NativeError.ToString() + "\n" +
"Source: " + MyOdbcException.Errors[i].Source + "\n" +
"SQL: " + MyOdbcException.Errors[i].SQLState + "\n");
}
}
}
public static void connSqlserver()
{
string MyConString = "DRIVER={MySQL ODBC 3.51 Driver};" +
"SERVER=localhost;" +
"DATABASE=zs;" +
"UID=root;" +
"PASSWORD=123;" +
"OPTION=3;CharSet=gb2312;";
OdbcConnection MyConnection = new OdbcConnection(MyConString);
MyConnection.Open();
OdbcCommand MyCommand = new OdbcCommand("DROP TABLE IF EXISTS my_odbc_net", MyConnection);
MyCommand.ExecuteNonQuery();
//使用轻量级的SqlDataReader显示数据
//指定Sql Server提供者的连接字符串
string connString = "Data Source=PC-200908231053\\SQLEXPRESS;database=occupationNew;User id=sa;PWD=123";
//建立连接对象
SqlConnection Sqlconn = new SqlConnection(connString);
//打开连接
Sqlconn.Open();
string thisCommand = "select * from Article where ID >"+ 1243931905062 +"order by ID";
//创建SqlDataAdapter对象,有两个参数,一个是查询字符串,一个是连接对象
SqlDataAdapter SqlDap = new SqlDataAdapter(thisCommand, Sqlconn);
//创建DataSet对象
DataSet thisDataset = new DataSet();
//使用SqlDataAdapter的Fill方法填充DataSet,有两个参数,一个是创建的DataSet实例,一个是填入的表
SqlDap.Fill(thisDataset, "informations");
//显示查询结果
foreach (DataRow theRow in thisDataset.Tables["informations"].Rows)
{
//Console.WriteLine(theRow["InformationId"] + "\t" + theRow["companyName"]);
//MyCommand.CommandText = "INSERT INTO information(companyname,course,major,number,sex,pay,request,informationsource,datetime,job,workprovinci,detailplace,isbin,enddate,existcourse,academymajororder) "
// + "VALUES('" + filter(theRow["CompanyName"]) + "','" + filter(theRow["Course"]) + "','" + filter(theRow["Major"]) + "','" + filter(theRow["Number"]) + "','" + filter(theRow["Sex"]) + "','" + filter(theRow["Pay"]) + "','" + filter(theRow["request"]) + "','" + filter(theRow["informationSource"]) + "','" + filter(theRow["DateTime"]) + "','" + filter(theRow["Job"]) + "','" + filter(theRow["WorkProvince"]) + "','" + filter(theRow["DetailPlace"]) + "','" + filter(theRow["IsBin"]) + "','" + filter(theRow["EndDate"]) + "','" + filter(theRow["ExistCourse"]) + "','" + filter(theRow["AcademyMajorOrder"]) + "')";
Console.WriteLine(theRow["ID"] + "\t" + theRow["Title"]);
MyCommand.CommandText = "INSERT INTO downloads(title,fenlei,content,lint,updatetime,click) "
+ "VALUES('" + filter(theRow["Title"]) + "','" + filter(theRow["CategoryID"]) + "','" + filter(theRow["Content"]) + "','" + filter(theRow["Author"]) + "','" + filter(theRow["DateTime"]) + "','" + filter(theRow["Hits"]) + "')";
//Console.WriteLine(theRow["ID"] + "\t" + theRow["Title"]);
//MyCommand.CommandText = "INSERT INTO xinwens(title,fenlei,content,username,updatetime,click) "
// + "VALUES('" + filter(theRow["Title"]) + "','" + "" + "','" + filter(theRow["Content"]) + "','" + filter("大学生就业指导中心") + "','" + filter(theRow["DateTime"]) + "','" + 0 + "')";
Console.WriteLine("INSERT, Total rows affected:" + MyCommand.ExecuteNonQuery());
}
Sqlconn.Close();
Console.ReadLine();
}
public static string filter(object text)
{
System.Text.Encoding GB2312 = System.Text.Encoding.GetEncoding("GB2312");
System.Text.Encoding UTF8 = System.Text.Encoding.UTF8;
byte[] data = GB2312.GetBytes(text.ToString());
string msg = GB2312.GetString(data);
return msg.Replace(",", ",").Replace("'", "’").Replace("―", "-");
}
}
}
c#读sql server数据添加到MySQL数据库的更多相关文章
- SQL Server connect to MySQL SQL Server通过LinkServer访问MySQL数据库,并操作mysql数据库代码
SQL Server 中需要访问MySQL的数据,可以通过调用MySQL的ODBC驱动,在SQL Server中添加LinkServer的方式实现. 1.从MySQL网站下载最新的MySQL ODBC ...
- SQL Server 和 Oracle 以及 MySQL 数据库
推荐:https://www.zhihu.com/question/19866767 三者是目前市场占有率最高(依安装量而非收入)的关系数据库,而且很有代表性.排行第四的DB2(属IBM公司),与Or ...
- Oracle DBLink跨数据库访问SQL server数据同步 踩坑实录
项目需求:这里暂且叫A公司吧,A公司有一套人事管理软件,需要与我们公司的软件做人员信息同步,A公司用的是SQL server数据库,我们公司用的Oracle,接口都不会开发(一万句"fuck ...
- SQL Server数据转MySql
正好用到SQL Server数据转MySql的知识,就分享一下, 准备:需要用到 Navicat Premium 百度上下载就好 1.打开连接MySQL数据库,新建数据库,双击数据库点击导入 2.导 ...
- SQL Server 数据导入Mysql具体教程
SQLServer2005数据导入Mysql到具体教程(測试) SQL SERVER数据导入MYSQL文件夹 1.Navicat for MySQL 版本号10.0.9 2.创建目标数据库 3.创 ...
- Microsoft SQL Server Migration Assistant for MySQL(从MySQL迁移表数据到MSSQL2008R2)_3
环境: 英文版(Windows7 64 bit + MySQL8.0.18 64 bit + MSSQL2008R2 64 bit) Microso ...
- SQL Server的链接服务器(MySQL、Oracle、Ms_sql、Access、SYBASE)
原文:SQL Server的链接服务器(MySQL.Oracle.Ms_sql.Access.SYBASE) 一.使用 Microsoft OLE DB Provider For ODBC 链接MyS ...
- 数据库 --> SQL Server 和 Oracle 以及 MySQL 区别
SQL Server 和 Oracle 以及 MySQL 区别 三者是目前市场占有率最高(依安装量而非收入)的关系数据库,而且很有代表性.排行第四的DB2(属IBM公司),与Oracle的定位和架构非 ...
- 浅谈SQL Server数据内部表现形式
在上篇文章 浅谈SQL Server内部运行机制 中,与大家分享了SQL Server内部运行机制,通过上次的分享,相信大家已经能解决如下几个问题: 1.SQL Server 体系结构由哪几部分组成? ...
随机推荐
- 开源项目Zookeeper、Doozer、etcd进行总结
Jason Wilder的一篇博客对分别对常见的服务发现开源项目Zookeeper.Doozer.etcd进行了总结介绍: Zookeeper是一个用户维护配置信息.命名.分布式同步以及分组服务的集中 ...
- CentOS6上ftp服务器搭建实战
1.安装程序包 [root@node1 ~]$ yum install -y vsftpd[root@node1 ~]$ yum install -y lftp # 安装测试软件 2.启动vsftpd ...
- 查看局域网指定IP的电脑名
nbtstat -a 192.168.0.139 节点 IP 址址: [192.168.0.140] 范围 ID: [] NetBIOS 远程计算机名称表 名称 类型 状态 ------------- ...
- python中的进程池和线程池
Python标准模块-concurrent.futures #1 介绍 concurrent.futures模块提供了高度封装的异步调用接口 ThreadPoolExecutor:线程池,提供异步调用 ...
- PythonWEB框架之Tornado
前言 Tornado(龙卷风)和Django一样是Python中比较主流的web框架,Tornado 和现在的主流 Web 服务器框架也有着明显的区别:Tornado自带socket,并且实现了异步非 ...
- Java数组声明、初始化
维数组的声明方式:type var[]; 或type[] var; 声明数组时不能指定其长度(数组中元素的个数), Java中使用关键字new创建数组对象,格式为:数组名 = new 数组元素的类型 ...
- 最简单的操作 jetty IDEA 【debug】热加载
[博客园cnblogs笔者m-yb原创,转载请加本文博客链接,笔者github: https://github.com/mayangbo666,公众号aandb7,QQ群927113708] http ...
- python笔记13-文件读写
1.打开文件 f=open('a.txt','a+',encoding='utf-8')#f代表的是文件对象,叫句柄 f.seek(0)把文件指针到最前 文件打开模式有3种: 1:w写模式,它是不能读 ...
- django rest_framework 分页出现报错
1.出现: WARNINGS: ?: (rest_framework.W001) You have specified a default PAGE_SIZE pagination rest_fram ...
- method&interface
method Go中虽没有class,但依旧有method 通过显示说明receiver来实现与某个类型组合 只能为同一个包的类型定义方法 Receiver可以是类型的值或指针 不存在方法重载 可以使 ...