说明:
(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
{
///
/// 神舟通用OSCAR数据库
///
public class ShenTongHelper
{
private string _strConn;
public ShenTongHelper(Common.DbInfo di)
{
_strConn = GetConnStr(di);
}
///
/// 获取连接字符串
///
///
///
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();
}
///
/// 数据库连接测试
///
///
/// 连接失败时的异常信息
///
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);
}
///
/// 执行SQL,获取数据集DataSet
///
/// SQL语句
/// 超时设置
///
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;
}
}
}
///
/// 执行SQL,获取数据集DataTable(前N行)
///
/// SQL语句
/// 返回行数
/// 超时设置
/// 数据集DataTable
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;
}
}
}
///
/// 执行SQL,返回受影响行数(插入、更新、删除)
///
/// SQL语句
/// 超时设置
/// 异常信息,为空时说明执行成功;非空执行失败
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();
}
}
}
///
/// 执行SQL(事务),返回异常信息(插入、更新、删除)
///
///
///
///
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;
}
///
/// 获取数据库名称、编码方式
///
///
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;
}
}
///
/// 获取所有表名
///
///
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;
}
}
///
/// 获取列信息
/// TABLE_NAME,COLUMN_NAME,DATA_TYPE,CHARACTER_MAXIMUM_LENGTH,ORDINAL_POSITION
///
/// 表名
///
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交互工具”,点击“帮助”,里面简介应该可以解决一般问题了。