C# 简单操作神通OSCAR数据库
Posted Jia_ShengJie
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C# 简单操作神通OSCAR数据库相关的知识,希望对你有一定的参考价值。
说明:
(1)需要引用 System.Data.OscarClient.dll ,此外此应用程序运行目录需要4个文件(如下图),文件可以在神通库安装目录下查找;
(2)代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.OscarClient;
namespace DC.DBHelp.ShenTong
/// <summary>
/// 神舟通用OSCAR数据库
/// </summary>
public class ShenTongHelper
private string _strConn;
public ShenTongHelper(Common.DbInfo di)
_strConn = GetConnStr(di);
/// <summary>
/// 获取连接字符串
/// </summary>
/// <param name="di"></param>
/// <returns></returns>
private string GetConnStr(Common.DbInfo di)
OscarConnectionStringBuilder sbConn = new OscarConnectionStringBuilder
//IP地址
Host = string.IsNullOrEmpty(di.DbAdd) ? "127.0.0.1" : di.DbAdd,
//端口号
Port = string.IsNullOrWhiteSpace(di.DbPort) ? 2003 : Convert.ToInt32(di.DbPort),
//数据库名称
Database = string.IsNullOrWhiteSpace(di.DbName) ? "OSRDB" : di.DbAdd,
//用户名
UserName = string.IsNullOrWhiteSpace(di.DbUser) ? "SYSDBA" : di.DbUser,
//密码
Password = string.IsNullOrWhiteSpace(di.DbUserPwd) ? "szoscar55" : di.DbUserPwd
;
return sbConn.ToString();
/// <summary>
/// 数据库连接测试
/// </summary>
/// <param name="di"></param>
/// <param name="errInfo">连接失败时的异常信息</param>
/// <returns></returns>
public bool ConnectTest(Common.DbInfo di, out string errInfo)
errInfo = string.Empty;
try
string str = GetConnStr(di);
OscarConnection conn = new OscarConnection(str);
conn.Open();
conn.Close();
catch (Exception ex)
errInfo = $"连接失败。失败原因如下:ex.Message";
return string.IsNullOrEmpty(errInfo);
/// <summary>
/// 执行SQL,获取数据集DataSet
/// </summary>
/// <param name="sql">SQL语句</param>
/// <param name="timeout">超时设置</param>
/// <returns></returns>
public DataSet ExecuteSqlGetDs(string sql, int timeout = 0)
using (OscarConnection conn = new OscarConnection(_strConn))
conn.Open();
using (OscarCommand cmd = new OscarCommand(sql, conn))
cmd.CommandType = CommandType.Text;
if (timeout > 0) cmd.CommandTimeout = timeout;
OscarDataAdapter ada = new OscarDataAdapter(cmd);
DataSet ds = new DataSet();
ada.Fill(ds);
return ds;
/// <summary>
/// 执行SQL,获取数据集DataTable(前N行)
/// </summary>
/// <param name="sql">SQL语句</param>
/// <param name="topRows">返回行数</param>
/// <param name="timeout">超时设置</param>
/// <returns>数据集DataTable</returns>
public DataTable ExecuteSqlGetDt(string sql, int topRows = 0, int timeout = 0)
using (OscarConnection conn = new OscarConnection(_strConn))
conn.Open();
using (OscarCommand cmd = new OscarCommand(sql, conn))
cmd.CommandType = CommandType.Text;
if (timeout > 0) cmd.CommandTimeout = timeout;
OscarDataAdapter ada = new OscarDataAdapter(cmd);
DataTable dt = new DataTable();
ada.Fill(0, topRows, dt);
return dt;
/// <summary>
/// 执行SQL,返回受影响行数(插入、更新、删除)
/// </summary>
/// <param name="sql">SQL语句</param>
/// <param name="timeout">超时设置</param>
/// <returns>异常信息,为空时说明执行成功;非空执行失败</returns>
public int ExecuteSqlNonQuery(string sql, int timeout = 0)
using (OscarConnection conn = new OscarConnection(_strConn))
conn.Open();
using (OscarCommand cmd = new OscarCommand(sql, conn))
cmd.CommandType = CommandType.Text;
if (timeout > 0) cmd.CommandTimeout = timeout;
return cmd.ExecuteNonQuery();
/// <summary>
/// 执行SQL(事务),返回异常信息(插入、更新、删除)
/// </summary>
/// <param name="timeOut"></param>
/// <param name="sqls"></param>
/// <returns></returns>
public string ExecuteSqlNonQueryTran(int timeOut = 0, params string[] sqls)
string strResult = "";
OscarConnection conn = new OscarConnection(_strConn);
conn.Open();
OscarTransaction sqlTran = conn.BeginTransaction();
try
OscarCommand cmd = conn.CreateCommand();
cmd.Transaction = sqlTran;
cmd.CommandType = CommandType.Text;
if (timeOut > 0) cmd.CommandTimeout = timeOut;
foreach (string sql in sqls)
cmd.CommandText = sql;
cmd.ExecuteNonQuery();
sqlTran.Commit();
catch (Exception ex)
strResult = ex.Message;
sqlTran.Rollback();
finally
conn.Close();
sqlTran.Dispose();
conn.Dispose();
return strResult;
/// <summary>
/// 获取数据库名称、编码方式
/// </summary>
/// <returns></returns>
public DataTable GetDbNameList()
using (OscarConnection conn = new OscarConnection(_strConn))
conn.Open();
DataTable dta = conn.GetSchema("Databases");
if (dta == null) return null;
DataTable dt = new DataTable();
dt.Columns.Add("Name");
foreach (DataRow dra in dta.Rows)
DataRow dr = dt.NewRow();
dr[0] = dra["DATABASE_NAME"].ToString().Trim();
dt.Rows.Add(dr);
return dt;
/// <summary>
/// 获取所有表名
/// </summary>
/// <returns></returns>
public DataTable GetTableNameList()
using (OscarConnection conn = new OscarConnection(_strConn))
conn.Open();
DataTable dta = conn.GetSchema("Tables");
if (dta == null) return null;
DataTable dt = new DataTable();
dt.Columns.Add("Name");
foreach (DataRow dra in dta.Rows)
if (dra["TABLE_SCHEMA"].ToString().Trim() == "STAGENT") continue;
DataRow dr = dt.NewRow();
dr[0] = dra["TABLE_NAME"].ToString().Trim();
dt.Rows.Add(dr);
return dt;
/// <summary>
/// 获取列信息
/// TABLE_NAME,COLUMN_NAME,DATA_TYPE,CHARACTER_MAXIMUM_LENGTH,ORDINAL_POSITION
/// </summary>
/// <param name="tableName">表名</param>
/// <returns></returns>
public DataTable GetColumnInfo(string tableName)
using (OscarConnection conn = new OscarConnection(_strConn))
conn.Open();
DataTable dta = string.IsNullOrWhiteSpace(tableName)
? conn.GetSchema("Columns", new string[] null, null, null)
: conn.GetSchema("Columns", new string[] null, null, tableName.ToUpper());
return dta;
(3)神舟库安装后,打开“SQL交互工具”,点击“帮助”,里面简介应该可以解决一般问题了。
以上是关于C# 简单操作神通OSCAR数据库的主要内容,如果未能解决你的问题,请参考以下文章
sqlservermysqloracle神通数据库(oscar)达梦(dm)各自的默认端口号
DataBase:sqlservermysqloracle神通数据库(oscar)达梦(dm)各自的默认端口号( 备忘记录 )