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("―", "-");
 
        }
    }
}

odbc连接数据库的更多相关文章

  1. C# -- 使用ODBC连接数据库

    C# -- 使用ODBC连接数据库 public class ODBCHelper { public static string conString1 = "Dsn=sqlServerDsn ...

  2. MATLAB通过ODBC连接数据库方法

    MATLAB通过ODBC连接数据库方法 1.首先创建数据库,我在这里用到的是MySQL 8.0 2.建立ODBC数据源,参考链接: https://www.cnblogs.com/benpao1314 ...

  3. PowerDesigner如何连接数据库--odbc连接数据库用法

    先下载msi   https://dev.mysql.com/downloads/connector/odbc/ 注:如果不成功,有可能msi版本问题,可以更换一下msi 前期准备 双击odbc的ms ...

  4. ODBC连接数据库实例

    2012-12-13 22:27 (分类:默认分类) 1.首先建立数据源,正常情况下载控制面板-管理工具-数据源,打开后有用户DSN系统DSN 两者区别在于系统级的DSN,就是对该系统的所有登录用户可 ...

  5. 【笔记】LR配置ODBC连接数据库进行参数化(mysql )未完待续

    很多时候我们需要大量的参数数据,但是光光靠手填写是非常麻烦的,既然被测对象的数据都在数据库,那么我们直接读取数据库回来就轻松简便很多. data  wizard 提供了一个从ODBC的连接获得数据转化 ...

  6. [C#][Database]C#通过ODBC以自定义端口连接数据库

    数据库端的配置暂且不说,比较简单,新建用户并开启相应连接权限即可. 通过ODBC连接数据库,重点在于Connection String的书写,在此可以查到几乎所有类型的Data Server的Conn ...

  7. ADO,OLEDB,ODBC,DAO的区别

    ADO NET OLEDB ODBC连接数据库的区别 http://www.doc88.com/p-976312043296.html http://blog.csdn.net/ithomer/art ...

  8. OLEDB和ODBC的区别(优缺点)

    ODBC是一种连接数据库的开放标准,OLEDB(对象链接和嵌入数据库)位于ODBC层与应用程序之间. 在你的ASP页面里,ADO是位于OLEDB之上的应用程序. 你的ADO调用先被送到OLEDB,然后 ...

  9. OLEDB和ODBC的区别

    ODBC(开放数据库互连):是Microsoft引进的一种早期数据库接口技术.它实际上是ADO的前身.早期的数据库连接是非常困难的. 每个数据库的格式都不一样,开发者得对他们所开发的每种数据库的底层A ...

随机推荐

  1. Struts2(三)——数据在框架中的数据流转问题

    一款软件,无在乎对数据的处理.而B/S软件,一般都是用户通过浏览器客户端输入数据,传递到服务器,服务器进行相关处理,然后返回到指定的页面,进行相关显示,完成相关功能.这篇博客重点简述一下Struts2 ...

  2. Linux Tomcat7.0安装配置实践总结

    一,安装JDk 先下载jdk,链接http://www.oracle.com/technetwork/java/javase/downloads/index.html,选择相对应平台的JDK.由于笔者 ...

  3. WdatePicker日历控件使用方法(转)

    转自:http://www.cnblogs.com/weixing/archive/2011/08/15/2139431.html WdatePicker日历控件使用方法   1. 跨无限级框架显示 ...

  4. css3:border-radius圆角边框详解 (变圆 图片)

    转:http://www.kuqin.com/shuoit/20141014/342620.html border-radius:50% 今天来聊聊这个border-radius属性,radius的英 ...

  5. 容器vector的使用总结 容器stack(栈)

    0.头文件:#include<vector>; using namespace std; 1.定义: vector<type> vec; 2.迭代器 vector<typ ...

  6. 奇葩问题:spring+mybaits项目突然出现其中一些Mapper类找不到

    一.问题现象 1,No bean named 'bomManageMapper' found in org.springframework.beans.factory.support.DefaultL ...

  7. 在Javascript中使用String.startsWith和endsWith

    在Javascript中使用String.startsWith和endsWith 在操作字符串(String)类型的时候,startsWith(anotherString)和endsWith(anot ...

  8. HTML5 drawImage 使用问题

    使用Image遇到的问题: <!DOCTYPE html> <html> <head> <meta charset="utf-8"> ...

  9. 前端利器,如何使用fiddle拦截在线css进行先下调试

    fiddle的功能相当的强悍,用户也非常广,不过今天我就教大家用fiddle进行前端调试. 首先下载软件fiddle,点击对应的版本下载安装. 安装成功后打开看到右侧的导航栏: 点击AutoRespo ...

  10. Laravel学习笔记

    1.Laravel 5 动态设置缓存引擎 \Config::set('cache.default','redis'); var_dump( \Config::get('cache.default') ...