C#做BarTender二次开发连接SQL-Server数据库报错

兵工厂三剑客 2016-03-02 01:11:46
因为现在在做BarTender的二次开发,想实现服务器数据库的套打。我是用的BarTender10.1的Seagull.BarTender.SystemDatabase 里面的关于访问SQL-Server数据库的一些类来弄。
相关代码如下:

SystemDatabaseConnection connection = new SystemDatabaseConnection();
connection.Method = SystemDatabaseConnectionMethod.Manual;
connection.Authentication = SystemDatabaseAuthenticationMethod.SQLServer;
connection.ServerName = @"DELL-PC";
connection.DatabaseName = "生产部自动派号数据库";
connection.UserName = "sa";
connection.Password = "sql";
connection.Connect();
string sqlcommand = "select MAC号 from " + dbs.ToString() + " where MAC号 between '" + FirstMAC + "' and '" + LastMAC + "'";
OLEDB oledb = new OLEDB("MyDataBase");
oledb.SQLStatement = sqlcommand;
LabelFormatDocument format = mybtEngine.Documents.Open(@txt_FormatPath.Text.Trim());
format.DatabaseConnections.SetDatabaseConnection(oledb);
format.Print();
format.Close(SaveOptions.DoNotSaveChanges);
mybtEngine.Stop();

这个时候会报如下的错误:

Seagull.BarTender.SystemDatabase.SystemDatabaseConnectionException”类型的未经处理的异常在
Seagull.BarTender.Print.dll 中发生
其他信息: 连接数据库时遇到问题。\n\n详细资料:\n\n找不到存储过程 'dbo.sp_GetDBVersion'。


如果我把connection.ServerName = @"DELL-PC";改成 connection.ServerName = @"DELL-PC\生产部自动派号数据库";
则会报下面的错误:

“Seagull.BarTender.SystemDatabase.SystemDatabaseConnectionException”类型的未经处理的异常在
Seagull.BarTender.Print.dll 中发生
其他信息: 连接数据库时遇到问题。\n\n详细资料:\n\n在与 SQL Server 建立连接时出现与网络相关的或特定于

实例的错误。未找到或无法访问服务器。请验证实例名称是否正确并且 SQL Server 已配置为允许远程连接。

(provider: SQL Network Interfaces, error: 26 - 定位指定的服务器/实例时出错)

数据库是装在局域网内的一台电脑上,不在本机上。我用C#的System.Data.SqlClient都能正常操作数据库,但是用BarTender的就出现上面的错误。


下面是SystemDatabaseConnection的成员变量:
Name Description
Authentication Gets or sets the authentication method to use when connecting to the system database.
ConnectionString Gets or sets the string used to open a SQL Server database.
DatabaseName Gets or sets the database name of the BarTender System Database. A typical name is "BarTender".
Method Gets or sets the connection method to use when connecting to the system database.
Password Gets or sets the password to use when connecting to the system database.
ServerName Gets or sets the server name where the system database is located on the network.
UserName Gets or sets the user name to use when connecting to the system database.
Version Returns the system database version identifier. Read-only.

下面是BarTender帮助文档的相关使用代码:
In order to use the System Database SDK, you must establish a connection with a BarTender System Database in order to access information saved by applications in the BarTender Suite. A connection to the BarTender System Database is made using the SystemDatabaseConnection class. 

The following code demonstrates how to automatically connect to the BarTender System Database using the settings specified in the BarTender System Database Setup dialog.

In C#:

try

{

SystemDatabaseConnection connection = new SystemDatabaseConnection();

connection.Connect();

}

catch (SystemDatabaseConnectionException ex)

{

// Handle can't connect or database not configured exceptions

}

To manually connect to a BarTender System Database, you must specify the following connection parameters:

The location of the SQL server database, using the ServerName property;

The name of the BarTender System Database, using the DatabaseName property;

The type of user authentication (Windows or SQL), using the Authentication property.

(Only when using SQL Server Authentication) Login credentials, using the UserName and Password properties.

The following code demonstrates how to manually configure a connection to a BarTender System Database using SQL server authentication.

In C#:

try

{

SystemDatabaseConnection connection = new SystemDatabaseConnection();

connection.Method = SystemDatabaseConnectionMethod.Manual;

connection.Authentication = SystemDatabaseAuthenticationMethod.SQLServer;

connection.ServerName = @"MyServer\BarTender";

connection.DatabaseName = "BarTender";

connection.UserName = "MyUserName";

connection.Password = "MyPassword123";

// Connect to the BarTender System Database

connection.Connect();

}

catch (SystemDatabaseConnectionException ex)

{

// Handle can't connect or database not configured exceptions

}




请教下大家,我的问题该如何解决。
...全文
1422 10 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
兵工厂三剑客 2019-06-27
  • 打赏
  • 举报
回复
引用 9 楼 fengcheung 的回复:
兄弟,你好。 请问第二步,你的模板怎么绑定数据源和设置你的字段。因为我也是需要通过代码传sql语句过去。 麻烦你指点一下,谢谢。
记得好像是在模板里面有的。可以从模块里面连接到数据库或者Excel,Access,都支持。
fengcheung 2019-06-25
  • 打赏
  • 举报
回复
兄弟,你好。
请问第二步,你的模板怎么绑定数据源和设置你的字段。因为我也是需要通过代码传sql语句过去。
麻烦你指点一下,谢谢。
bingdaoice 2016-03-10
  • 打赏
  • 举报
回复
[quote=引用 5 楼 SCGH_Fx 的回复:] Grid++Report我目前也在用,条形码,也出现有不能扫描的问题,放大了可以 另外你能把与BarTender二次开发相关的代码共享一下吗?
兵工厂三剑客 2016-03-10
  • 打赏
  • 举报
回复
引用 4 楼 bingdaoice 的回复:
[quote=引用 2 楼 SCGH_Fx 的回复:] 弄错了个地方,纠正一下。 OLEDB oledb = new OLEDB("MyDataBase"); 这OLEDB数据源名字,应该是自己链接的目标数据库名字。我的是“生产部自动派号数据库” 所以应该写成 OLEDB oledb = new OLEDB("生产部自动派号数据库");
你好,请问你的这个BarTender的二次开发,是将BarTender报表编辑器集成到系统中,让用户可以自行设计报表, 并打印出来. 是这样的吗?[/quote] 没呢。海鸥公司没提供设计引擎接口的。我看了它的帮助文档,没提到这个东西。我是在软件里面弄了个打开BarTender的进程,在BarTender里面编辑修改后,然后软件里面刷新一下,用引擎重新打开,就可以了,效果也差不多。 但是国产的Grid++Report是可以的。这2个我都用过。但是Grid++report打印出来的条形码不能扫描。如果你要设计报表,用Grid++report最好了。BarTender是专门打印条形码的,设计报表不方便。
bingdaoice 2016-03-10
  • 打赏
  • 举报
回复
引用 2 楼 SCGH_Fx 的回复:
弄错了个地方,纠正一下。 OLEDB oledb = new OLEDB("MyDataBase"); 这OLEDB数据源名字,应该是自己链接的目标数据库名字。我的是“生产部自动派号数据库” 所以应该写成 OLEDB oledb = new OLEDB("生产部自动派号数据库");
你好,请问你的这个BarTender的二次开发,是将BarTender报表编辑器集成到系统中,让用户可以自行设计报表, 并打印出来. 是这样的吗?
Qos9527 2016-03-10
  • 打赏
  • 举报
回复
另外一个需要注意的地方就是在套打的时候,打印参数的设置,具体如下截图所示:
兵工厂三剑客 2016-03-10
  • 打赏
  • 举报
回复
问题我自己解决了。我贴出解决方法吧。 第一步:在数据库下面建个存储过程,

USE [生产部自动派号数据库]
GO

/****** Object:  StoredProcedure [dbo].[sp_GetDBVersion]    Script Date: 03/10/2016 10:03:12 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


/*---------------------------------------------------------------------------*/
/* Procedure:   sp_GetDBVersion                                              */
/* Purpose:     Return the Hard-Coded Database Version via an Output variable*/
/* NOTE:        This stored procedure was kept for backwards compatibility   */
/*              with BSS 1.0, and should be left alone- the Version number   */
/*              should NOT be altered here.  Change SpGetDBVersion instead.  */
/*---------------------------------------------------------------------------*/
CREATE PROCEDURE [dbo].[sp_GetDBVersion](@Version INTEGER OUTPUT, @DbDescription NCHAR(100) OUTPUT)
AS
SELECT @Version = 150, @DbDescription = N'This is the initial version of the Database'


GO
这个地方Version=150。是跟数据库版本有关系,我用的是SQL Server2008,写成100的话,会报错,在调用BarTender的DLL时会提示数据库版本不一样。 第二步:在BarTender的标签模板中,为需要套打数据的控件(主要是条形码控件),绑定数据源,也就是数据库字段。 第三步: 打印代码实际上只需要
string sqlcommand = "select MAC号 from " + dbs.ToString() + " where MAC号 between '" + FirstMAC + "' and '" + LastMAC + "'";
                OLEDB oledb = new OLEDB("MyDataBase");
                oledb.SQLStatement = sqlcommand;
                LabelFormatDocument format = mybtEngine.Documents.Open(@txt_FormatPath.Text.Trim());
                format.DatabaseConnections.SetDatabaseConnection(oledb);                
                format.Print();
这一段就可以。
兵工厂三剑客 2016-03-10
  • 打赏
  • 举报
回复
弄错了个地方,纠正一下。 OLEDB oledb = new OLEDB("MyDataBase"); 这OLEDB数据源名字,应该是自己链接的目标数据库名字。我的是“生产部自动派号数据库” 所以应该写成 OLEDB oledb = new OLEDB("生产部自动派号数据库");
bingdaoice 2016-03-10
  • 打赏
  • 举报
回复
[quote=引用 7 楼 SCGH_Fx 的回复:] 好的,我先看看HELP,另外可以留一个你的QQ号码吗?如果有不懂之处也好请教你。 CSDN的私信功能,不太及时!
兵工厂三剑客 2016-03-10
  • 打赏
  • 举报
回复
引用 6 楼 bingdaoice 的回复:
[quote=引用 5 楼 SCGH_Fx 的回复:] Grid++Report我目前也在用,条形码,也出现有不能扫描的问题,放大了可以 另外你能把与BarTender二次开发相关的代码共享一下吗?
这个是公司用的,而且无关代码很多。关于打印部分的,实际上在Bartender的帮助文档里面都能找到。你去看看,基本上够开发用了。链接数据库套打,赋值打,都能找到的。 如果你有什么问题,可以私信我。

111,098

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧