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 体系结构由哪几部分组成? ...
随机推荐
- png转tif
发国外的文章要求图片是tif,cmyk色彩空间的. 大小尺寸还有要求. 比如 网上大神多,找到了一段代码,感谢! https://www.jianshu.com/p/ec2af4311f56 http ...
- Annotaion——深入理解注解类型
什么是注解? 对于很多初次接触的开发者来说应该都有这个疑问?Annontation是Java5开始引入的新特征,中文名称叫注解.它提供了一种安全的类似注释的机制,用来将任何的信息或元数据(metada ...
- 『TensorFlow』读书笔记_VGGNet
VGGNet网络介绍 VGG系列结构图, 『cs231n』卷积神经网络工程实践技巧_下 1,全部使用3*3的卷积核和2*2的池化核,通过不断加深网络结构来提升性能. 所有卷积层都是同样大小的filte ...
- 『计算机视觉』感受野和anchor
原文链接:关于感受野的总结 论文链接:Understanding the Effective Receptive Field in Deep Convolutional Neural Networks ...
- 20175227张雪莹 2018-2019-2 《Java程序设计》第五周学习总结
20175227张雪莹 2018-2019-2 <Java程序设计>第五周学习总结 教材学习内容总结 第六章接口与实现 接口 接口体中所有的常量访问权限一定是public和static(可 ...
- Eclipse中Server视图加载项目之后项目名后边有带括号的名字
用习惯了eclipse工具,因为某种原因需要修改项目名称.结果选择项目,按“F2”成功修改后,使用tomcat进行web发布时,选择“Add and Remove”,发现名字还是以前那个项目名称.即使 ...
- linux 中mv命令
mv 命令是一个与cp类似的命令,但是它并非创建文件或目录的复制品/副本.不管你在使用什么版本的Linux系统,mv 都默认安装在你的Linux系统上了.来看一下 mv 命令在日常操作中的一些例子. ...
- eclipse中创建DataBase Connections
1.window --> show view --> other --> Data Management --> Data Exploerer --> ok: 2.右键单 ...
- mysql中describe关键字
今天写代码的时候,有一个类的数据始终不能插入,老是提示在You have an error in your SQL syntax; check the manual that corresponds ...
- My IELTS result has come out 我的雅思成绩出来了
Thanks to god, I finally get a score of 6.5, although my socres of listening and writing are only ...