c#向Oracle数据库中插入数据, 出现乱码问题, oracle11g(版本11.2)
数据库语言是:SIMPLIFIED CHINESE_CHINA.ZHS16GBK, 注册表machine->software->Oracle下的NLS_LANG键;
环境变量: nls_lang=SIMPLIFIED CHINESE_CHINA.ZHS16GBK; 与注册表相统一.
Oracle SQL Developer中工具->首选项->环境->编码: GBK.
连接字符串:
//static string conStr = "Data Source=(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=WIN-COSDJDSFC4C)(PORT=1521))(CONNECT_DATA=(SERVICE_NAME=ORCL)));User Id=scott;Password=tiger;";
//static string conStr = "Provider=MSDAORA.1;Data Source=(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=WIN-COSDJDSFC4C)(PORT=1521))(CONNECT_DATA=(SERVICE_NAME=ORCL)));User Id=scott;Password=tiger;";
static string conStr = "provider='OraOleDb.Oracle';Data Source=(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=WIN-COSDJDSFC4C)(PORT=1521))(CONNECT_DATA=(SERVICE_NAME=ORCL)));User Id=scott;Password=tiger;";
前两种乱码, 后一种正常.
第一种使用的是OracleClient方式,是微软专门针对Oracle数据库开发的
引用类库:System.Data.OracleClient.dll。
命名空间:System.Data.OracleClient。
常用类:OracleConnection、OracleCommand、OracleDataAdapter、OracleTransaction、OracleDataReader等。
典型连接字符串:“data source=oratest;user id=scott;password=tiger”(注意:可不指定 provider 驱动)。
第二,三中使用的是OleDb方式,微软和Oracle公司各自提供了OleDb的驱动程序,使用方法的差别很少。
相同之处(后两种连接)
命名空间:System.Data.OleDb。
常用类:OleDbConnection、OleDbCommand、OleDbDataAdapter、OleDbTransaction、OleDbDataReader等。
不同之处
引用类库:微软的只需要System.Data.dll;若用Oracle的驱动,虽然也只要引入System.Data.dll,但前提是首先安装Oracle针对.Net的数据访问组件, 即ODAC。
测试代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.IO;
using System.Data.OracleClient;
using System.Data;
using System.Data.OleDb;
namespace ConsoleApplication3
{
class Program
{
//有乱码
//static string conStr = "Data Source=(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=WIN-COSDJDSFC4C)(PORT=1521))(CONNECT_DATA=(SERVICE_NAME=ORCL)));User Id=scott;Password=tiger;";
//static string conStr = "Provider=MSDAORA.1;Data Source=(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=WIN-COSDJDSFC4C)(PORT=1521))(CONNECT_DATA=(SERVICE_NAME=ORCL)));User Id=scott;Password=tiger;";
static string conStr = "provider='OraOleDb.Oracle';Data Source=(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=WIN-COSDJDSFC4C)(PORT=1521))(CONNECT_DATA=(SERVICE_NAME=ORCL)));User Id=scott;Password=tiger;";
static void Main(string[] args)
{
//OracleConnection sqlCon = new OracleConnection();
OleDbConnection sqlCon = new OleDbConnection();
try
{
sqlCon.ConnectionString = conStr;
sqlCon.Open();
InsertScriptOleDB(sqlCon);
//DisplayDataOleDB(sqlCon);
}
catch (OracleException ex)
{
Console.WriteLine(ex.Message);
}
finally
{
sqlCon.Close();
}
Console.Read();
}
static void DisplayDataOleDB(OleDbConnection sqlCon)
{
string strSql = "select * from panquan";
OleDbCommand comm = new OleDbCommand();
comm.CommandType = CommandType.Text;
comm.CommandText = strSql;
comm.Connection = sqlCon;
OleDbDataReader reader = comm.ExecuteReader();
while (reader.Read())
{
Console.WriteLine("ID={0}, USERNAME={1}, KEY={2}, GRANTTO={3}, REALNAME={4},TEL={5}",
reader["ID"], reader["USERNAME"], reader["KEY"], reader["GRANTTO"], reader["REALNAME"], reader["TEL"]);
}
}
static void InsertScriptOleDB(OleDbConnection sqlCon)
{
string strSql = GetScript("script.sql");
string[] subSqls = strSql.Split(';');//script.sql文件中每条语句按分号分隔, 单独执行
//strSql.Replace('\r', ' ').Replace('\n', ' ');
for (int i = 0; i < subSqls.Length; i++)
ExecuteSqlOleDB(sqlCon, subSqls[i]);
}
static void ExecuteSqlOleDB(OleDbConnection sqlconn, string Sql)
{
if (Sql == "\r\n") return;
OleDbCommand Command
补充:软件开发 , C# ,