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数据库的更多相关文章

  1. SQL Server connect to MySQL SQL Server通过LinkServer访问MySQL数据库,并操作mysql数据库代码

    SQL Server 中需要访问MySQL的数据,可以通过调用MySQL的ODBC驱动,在SQL Server中添加LinkServer的方式实现. 1.从MySQL网站下载最新的MySQL ODBC ...

  2. SQL Server 和 Oracle 以及 MySQL 数据库

    推荐:https://www.zhihu.com/question/19866767 三者是目前市场占有率最高(依安装量而非收入)的关系数据库,而且很有代表性.排行第四的DB2(属IBM公司),与Or ...

  3. Oracle DBLink跨数据库访问SQL server数据同步 踩坑实录

    项目需求:这里暂且叫A公司吧,A公司有一套人事管理软件,需要与我们公司的软件做人员信息同步,A公司用的是SQL server数据库,我们公司用的Oracle,接口都不会开发(一万句"fuck ...

  4. SQL Server数据转MySql

    正好用到SQL Server数据转MySql的知识,就分享一下, 准备:需要用到  Navicat Premium 百度上下载就好 1.打开连接MySQL数据库,新建数据库,双击数据库点击导入 2.导 ...

  5. SQL Server 数据导入Mysql具体教程

    SQLServer2005数据导入Mysql到具体教程(測试) SQL SERVER数据导入MYSQL文件夹   1.Navicat for MySQL 版本号10.0.9 2.创建目标数据库 3.创 ...

  6. Microsoft SQL Server Migration Assistant for MySQL(从MySQL迁移表数据到MSSQL2008R2)_3

    环境: 英文版(Windows7 64 bit + MySQL8.0.18 64 bit + MSSQL2008R2 64 bit)                           Microso ...

  7. SQL Server的链接服务器(MySQL、Oracle、Ms_sql、Access、SYBASE)

    原文:SQL Server的链接服务器(MySQL.Oracle.Ms_sql.Access.SYBASE) 一.使用 Microsoft OLE DB Provider For ODBC 链接MyS ...

  8. 数据库 --> SQL Server 和 Oracle 以及 MySQL 区别

    SQL Server 和 Oracle 以及 MySQL 区别 三者是目前市场占有率最高(依安装量而非收入)的关系数据库,而且很有代表性.排行第四的DB2(属IBM公司),与Oracle的定位和架构非 ...

  9. 浅谈SQL Server数据内部表现形式

    在上篇文章 浅谈SQL Server内部运行机制 中,与大家分享了SQL Server内部运行机制,通过上次的分享,相信大家已经能解决如下几个问题: 1.SQL Server 体系结构由哪几部分组成? ...

随机推荐

  1. chrome 调试功能使用说明

    单文件搜索 ctrl+f 全局搜索 ctrl+shift+f

  2. LeetCode--019--删除链表的倒数第N个节点(java)

    给定一个链表,删除链表的倒数第 n 个节点,并且返回链表的头结点. 示例: 给定一个链表: 1->2->3->4->5, 和 n = 2. 当删除了倒数第二个节点后,链表变为 ...

  3. Linux(CentOS 7)命令行模式安装VMware Tools 详解

    本篇文章主要介绍了如何在Linux(CentOS 7)命令行模式安装VMware Tools,具有一定的参考价值,感兴趣的小伙伴们可以参考一下. 本例中为在Linux(以CentOS 7为例)安装VM ...

  4. EL函数库及用法

    导入标签库: <%@ tablib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%&g ...

  5. PI上导入RFC

    ERP中创建函数:ZERP_GETSPAREPART 传入参数:SOLD_TO_ID SHIP_TO_ID 表:INTABLE OUTABLE 登陆PI, 后面正常做data type DT_PART ...

  6. 【转载】如何将本地文件上传到github托管

    转自:http://www.cnblogs.com/babywin/p/6073124.html 清楚明白.  

  7. Linux之expr命令详解

    expr命令: expr命令是一个手工命令行计数器,用于在UNIX/LINUX下求表达式变量的值,一般用于整数值,也可用于字符串. –格式为: expr Expression(命令读入Expressi ...

  8. 隐藏WORDPRESS账户登录错误信息

    将下面的脚本添加到当前主题的functions.php文件中. function no_wordpress_errors(){  return 'Something is wrong!';}add_f ...

  9. php加密

    域名授权函数 function allow_doamin(){    $is_allow=false;    $url=trim($_SERVER['SERVER_NAME']);    $arr_a ...

  10. 一、JAVA变量类型:②局部变量

    局部变量声明在方法.构造方法或者语句块中: public class Test{ public void pupAge(){ int age = 0; //定义age为方法pupAge中的局部变量,并 ...