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# -- 使用ODBC连接数据库
C# -- 使用ODBC连接数据库 public class ODBCHelper { public static string conString1 = "Dsn=sqlServerDsn ...
- MATLAB通过ODBC连接数据库方法
MATLAB通过ODBC连接数据库方法 1.首先创建数据库,我在这里用到的是MySQL 8.0 2.建立ODBC数据源,参考链接: https://www.cnblogs.com/benpao1314 ...
- PowerDesigner如何连接数据库--odbc连接数据库用法
先下载msi https://dev.mysql.com/downloads/connector/odbc/ 注:如果不成功,有可能msi版本问题,可以更换一下msi 前期准备 双击odbc的ms ...
- ODBC连接数据库实例
2012-12-13 22:27 (分类:默认分类) 1.首先建立数据源,正常情况下载控制面板-管理工具-数据源,打开后有用户DSN系统DSN 两者区别在于系统级的DSN,就是对该系统的所有登录用户可 ...
- 【笔记】LR配置ODBC连接数据库进行参数化(mysql )未完待续
很多时候我们需要大量的参数数据,但是光光靠手填写是非常麻烦的,既然被测对象的数据都在数据库,那么我们直接读取数据库回来就轻松简便很多. data wizard 提供了一个从ODBC的连接获得数据转化 ...
- [C#][Database]C#通过ODBC以自定义端口连接数据库
数据库端的配置暂且不说,比较简单,新建用户并开启相应连接权限即可. 通过ODBC连接数据库,重点在于Connection String的书写,在此可以查到几乎所有类型的Data Server的Conn ...
- ADO,OLEDB,ODBC,DAO的区别
ADO NET OLEDB ODBC连接数据库的区别 http://www.doc88.com/p-976312043296.html http://blog.csdn.net/ithomer/art ...
- OLEDB和ODBC的区别(优缺点)
ODBC是一种连接数据库的开放标准,OLEDB(对象链接和嵌入数据库)位于ODBC层与应用程序之间. 在你的ASP页面里,ADO是位于OLEDB之上的应用程序. 你的ADO调用先被送到OLEDB,然后 ...
- OLEDB和ODBC的区别
ODBC(开放数据库互连):是Microsoft引进的一种早期数据库接口技术.它实际上是ADO的前身.早期的数据库连接是非常困难的. 每个数据库的格式都不一样,开发者得对他们所开发的每种数据库的底层A ...
随机推荐
- android createbitmap函数内存溢出,求解怎样进行处理out of memory溢出问题
android createbitmap函数内存溢出,求解怎样进行处理out of memory溢出问题 android createbitmap函数内存溢出,求解怎样进行处理out of memor ...
- 把Storyboard减轻的方法
把Storyboard减轻的方法 by 伍雪颖 UIViewController *secondStoryboard = [[UIStoryboard storyboardWithName:@&quo ...
- Linux Tomcat7.0安装配置实践总结
一,安装JDk 先下载jdk,链接http://www.oracle.com/technetwork/java/javase/downloads/index.html,选择相对应平台的JDK.由于笔者 ...
- 移动平台前端开发总结(针对iphone,Android等手机)
移动平台前端开发是指针对高端智能手机(如Iphone.Android)做站点适配也就是WebApp,并非是针对普通手机开发Wap 2.0,所以在阅读本篇文章以前,你需要对webkit内核的浏览器有一定 ...
- 追加addclass和removeclass
//addclass Base.prototype.addclass=function(classname){ for(var i=0;i< ...
- SCII码表 键盘常用ASCII码
ASCII码对照表 在Web开发时,如下的ASCII码只要加上&#和;就可以变成Web可以辨认的字符了在处理特殊字符的时候特别有用,如:' 单引号在数据库查询的时候是杀手,但是如果转换成' ...
- Android studio教程:[3]修改背景主题
android studio开发环境的背景主题是可以更改的,现在都流行黑色背景,这样让软件显得更高端大气的,更加赏心悦目,但最主要的还是看起来更舒适更顺眼.下面就教大家如何更改背景主题. 工具/原料 ...
- 查看DB文件的空间使用情况
可以使用如下语句获得DB文件的空间使用 use dbName SELECT DB_NAME() AS DbName, name AS FileName, size/128.0 AS CurrentSi ...
- [转]activiti5用户任务分配
用户任务分配办理人:1.用户任务可以直接分配给一个用户,这可以通过humanPerformer元素定义. humanPerformer定义需要一个 resourceAssignmentExpressi ...
- SharedPreferences基础
见归档项目:SharedPreferencesDemo.zip 1.对于数据量较小,且有明显的K-V形式的数据而言,适合用SharedPreferences保存.SharedPreferences的数 ...